Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: swing layout problems

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    26
    My Mood
    Aggressive
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question swing layout problems

    I wanted to make my buttons round, Untitled.png. Basically, i know how to make round buttons, it is by making a class that extends JButton and do some paint stuff - BUT, when i try to add the button to the panel, sometimes it resizes its self or, doesnt obey to my damn setLocation commad. I have an idea to what this problem is caused, i think it is by the layout. Please help me do something about layouts..


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: swing layout problems

    Can you make a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    26
    My Mood
    Aggressive
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: swing layout problems

    import java.awt.*;
    import java.awt.geom.Ellipse2D;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
     
    public class xButton extends JButton{
     
        private void initialize(){
            setContentAreaFilled(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
        }
     
        public void changeLocation(Point p){
            this.setLocation(p);
        }
     
        public xButton(){
            initialize();
        }
     
        public xButton(String _text){
            super(_text);
            initialize();
        }
     
        private Dimension size = new Dimension(100,30);
        private Color back = new Color(0, 100, 240);
        private Color border = new Color(0, 80, 220);
     
        @Override
        protected void paintBorder(Graphics g) {
     
        }
     
        @Override
        protected void paintComponent(Graphics g) {
            if(getModel().isArmed()){
                g.setColor(Color.green);
            }
            else{
                g.setColor(Color.yellow);
            }
            g.fillOval(this.getLocation().x, this.getLocation().y, 39, 39);
            g.drawImage(new ImageIcon(getClass().getResource("/xComponent/buttonRestart.png")).getImage(), this.getLocation().x, this.getLocation().y, null);
     
            super.paintComponent(g);
        }
     
        Shape s = new Ellipse2D.Float(this.getLocation().x, this.getLocation().y, 40, 40);
     
        @Override
        public boolean contains(int i, int i1) {
            return s.contains(i, i1);
        }
     
        public static void main(String args[]){
            JFrame frame = new JFrame();
            xButton xb = new xButton();
            frame.setLayout(new BorderLayout());
            frame.add(xb, BorderLayout.CENTER);
            frame.setVisible(true);
            frame.pack();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            xb.setLocation(100, 100);
        }
    }

    setLocation is not working..

  4. #4
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: swing layout problems

    Can you post the code so we can see what happens or make a SSCCE? It could possibly be the layout that you have chosen. If you want to have more control over the layout, try GridBagLayout.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    26
    My Mood
    Aggressive
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: swing layout problems

    I cant event reply... mods are preventing me to -_-


    EDIT:
    package components;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class xButton extends JButton{
     
        public static final int RECT=0;
        public static final int ELLIPSE=1;
        public static final int ROUNDRECT=2;
     
        private int cornerCurve=3;
        private int shapeState=1;
        private ImageIcon icon = null;
     
        public xButton(){
            this.setContentAreaFilled(false);
            setCursor(new Cursor(Cursor.HAND_CURSOR));
        }
     
        /**
         * Sets the Icon of the button with the desired Image
         * 
         * @param ii new Icon
         * 
         * @see xchan
         */
        public void setImageIcon(ImageIcon ii){
            icon = ii;
        }
     
        /**
         *
         * Sets the shape of the button
         * 
         * @param shape 0(Rectangle) 1(Ellipse) 2(RoundRectanlge)
         */
        public void setShape(int shape){
            shapeState = shape;
        }
     
        /**
         *
         * Changes the corner curve of the Round Rectangle shaped buttons. This will only affect
         * buttons with Round Rectangle shapes.
         * @param c Corner Curve
         * 
         */
        public void setCornerCurve(int c){
            cornerCurve = c;
        }
     
        /**
         * Returns the state of the button's shape.
         * @return an integer valuing the buttons shape state
         * @see setShape(int shape)
         */
        public int getShape(){
            return shapeState;
        }
     
        /**
         * Returns the value of the curve of the corners of the Round Rectangle of the button.
         * @return an integer valuing the curve of the Round Rectangle button's corners
         * @see setCornerCurve(int c)
         */
        public int getCornerCurve(){
            return cornerCurve;
        }
     
     
        @Override
        protected void paintComponent(Graphics g) {
            if(getModel().isArmed()){
                g.setColor(new Color(255,255,255,128));
     
            }
            else{
                g.setColor(new Color(255,255,255,0));
            }
     
            if(icon!=null){
                g.drawImage(icon.getImage(),0, 0, null);
            }
     
            switch(shapeState){
                case 0:
                    g.fillRect(0, 0, getSize().width-1, getSize().height-1);
                    break;
                case 1:
                    g.fillOval(0,0, getSize().width-1, getSize().height-1);
                    break;
                case 2:
                    g.fillRoundRect(0, 0, getSize().width-1, getSize().height, cornerCurve, cornerCurve);
                    break;
                default:
                    g.fillRect(0, 0, getSize().width-1, getSize().height-1);
                    break;
            }
     
            super.paintComponent(g);
        }
     
        @Override
        public boolean contains(int i, int i1){
            Shape s = new Ellipse2D.Float(0, 0, getSize().width-1, getSize().width-1);
            return s.contains(i, i1);
        }
     
        public static void main(String args[]){
            JFrame frame = new JFrame();
            xButton b = new xButton();
            frame.getContentPane().add(b);
            frame.setVisible(true);
            frame.pack();
        }
     
    }

    sorry if there are other code included...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: swing layout problems

    Sorry, its visible now.
    Some problems:
    1)There is a conflict between the layout manager positioning the component and the code trying to use setLocation.
    2) Loading an image every time it is drawn instead of loading it once in an init method.
    3) calling setVisible() before the GUI is ready to be seen. Call it last
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    xchan (April 21st, 2013)

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    26
    My Mood
    Aggressive
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: swing layout problems

    those steps completed the answer to my problem thank you!

Similar Threads

  1. swing applet for name problems
    By my21 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 29th, 2013, 07:05 PM
  2. Swing (Problems)
    By LoganC in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 22nd, 2012, 03:48 PM
  3. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  4. Replies: 0
    Last Post: November 27th, 2010, 10:47 PM