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 9 of 9

Thread: Very simple GUI problem

  1. #1
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Very simple GUI problem

    Hi,

    I'm having an issue with my GUI, where 2 of the buttons appear to be overlapping. I'm trying to get the buttons to span multiple columns. It works on the other 2 buttons, but not on this one; which is weird because i practially copy and pasted the code.

    I've been trying to debug this stupid button for 45min now, but with no luck. Can someone please take a look at the code and let me know what the problem is? The 'Solve' button (jSolve) overlaps with the 'EXIT' (jExit) button.

    Thank you.

    public class test extends JFrame {
     
    	JTextField jHex1;
    	JTextField jHex2;
    	JTextField jHex3;
    	JTextField jHex4;
    	JTextField jHex5;
    	JTextField jHex6;
    	JTextField jHex7;
     
    	JTextArea jResult;
    	JButton jClear;
    	JButton jSolve;
    	JButton jExit;
     
     
    	test()
    	{
    		JPanel p1 = new JPanel();
    		p1.setLayout(new GridBagLayout());
     
    		GridBagConstraints c = new GridBagConstraints();
    		c.insets = new Insets(6,6,3,3);
     
    		jHex1 = new JTextField(12);
    		jHex2 = new JTextField(12);
    		jHex3 = new JTextField(12);
    		jHex4 = new JTextField(12);
    		jHex5 = new JTextField(12);
    		jHex6 = new JTextField(12);
    		jHex7 = new JTextField(12);		
    		jResult = new JTextArea();
    		jClear = new JButton("Clear");
    		jSolve = new JButton("Solve");
    		jExit = new JButton("EXIT");
    		JLabel label1 = new JLabel("Hex 1:");
    		JLabel label2 = new JLabel("Hex 2:");
    		JLabel label3 = new JLabel("Hex 3:");
    		JLabel label4 = new JLabel("Hex 4:");
    		JLabel label5 = new JLabel("Hex 5:");
    		JLabel label6 = new JLabel("Hex 6:");
    		JLabel label7 = new JLabel("Hex 7:");
     
    		c.gridx = 0;
    		c.gridy = 3;
    		p1.add(label1, c);
    		c.gridx = 3;
    		c.gridy = 3;
    		p1.add(jHex1, c);
     
    		c.gridx = 0;
    		c.gridy = 6;
    		p1.add(label2, c);
    		c.gridx = 3;
    		c.gridy = 6;
    		p1.add(jHex2, c);
     
    		c.gridx = 0;
    		c.gridy = 9;
    		p1.add(label3, c);
    		c.gridx = 3;
    		c.gridy = 9;
    		p1.add(jHex3, c);
     
    		c.gridx = 0;
    		c.gridy = 12;
    		p1.add(label4, c);
    		c.gridx = 3;
    		c.gridy = 12;
    		p1.add(jHex4, c);
     
    // ****** Solve Button *******
    		c.weightx = 0.0;
    		c.gridwidth = GridBagConstraints.RELATIVE;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 0;
    		c.gridy = 15;
    		p1.add(jSolve, c);
     
    		c.gridx = 15;
    		c.gridy = 3;
    		p1.add(label5, c);
    		c.gridx = 18;
    		c.gridy = 3;
    		p1.add(jHex5, c);
     
    		c.gridx = 15;
    		c.gridy = 6;
    		p1.add(label6, c);
    		c.gridx = 18;
    		c.gridy = 6;
    		p1.add(jHex6, c);
     
    		c.gridx = 15;
    		c.gridy = 9;
    		p1.add(label7, c);
    		c.gridx = 18;
    		c.gridy = 9;
    		p1.add(jHex7, c);
     
    		c.weightx = 0.0;
    		c.gridwidth = GridBagConstraints.REMAINDER;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 15;
    		c.gridy = 12;		
    		p1.add(jClear, c);
     
    // ****** EXIT Button *******
     
    		c.weightx = 0.0;
    		c.gridwidth = GridBagConstraints.REMAINDER;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.gridx = 15;
    		c.gridy = 15;		
    		p1.add(jExit, c);
     
    		add(p1);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	    setTitle("Hex Puzzle");
    		setSize(800,600);
    		setVisible(true);
    	}
     
     
     
       public static void main (String[] args) {
     
       new test();
     
     
     }
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Posts
    57
    My Mood
    Fine
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Very simple GUI problem

    try some different values for gridx and gridy positions

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Very simple GUI problem

    c.gridwidth = GridBagConstraints.RELATIVE;
    Try changing this line with;
    c.gridwidth = GridBagConstraints.EAST;

    There are many other solutions too.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Very simple GUI problem

    Quote Originally Posted by Mr.777 View Post
    Try changing this line with;
    c.gridwidth = GridBagConstraints.EAST;

    There are many other solutions too.
    Complete wrong. The gridwidth property specifies how many columns on which the cell will span. So the solution is to set c.gridwidth = 2;

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Very simple GUI problem

    Quote Originally Posted by Bob_Sadarka View Post
    Complete wrong. The gridwidth property specifies how many columns on which the cell will span. So the solution is to set c.gridwidth = 2;
    Well, first read my post carefully. I've clearly mentioned that there can be many other solutions too.
    Also, will you care explaining why this solution is completely wrong?
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,330
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Very simple GUI problem

    Quote Originally Posted by Mr.777 View Post
    Well, first read my post carefully. I've clearly mentioned that there can be many other solutions too.
    Also, will you care explaining why this solution is completely wrong?
    The gridwidth property specifies the number of cells in a row a component should occupy. The EAST property should be used to specify the anchor - when a component is smaller than the display area this manages where the component should be anchored. EAST is defined as 13, so in affect setting the gridwidth to EAST sets the gridwidth to 13.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Very simple GUI problem

    Quote Originally Posted by copeg View Post
    The gridwidth property specifies the number of cells in a row a component should occupy. The EAST property should be used to specify the anchor - when a component is smaller than the display area this manages where the component should be anchored. EAST is defined as 13, so in affect setting the gridwidth to EAST sets the gridwidth to 13.
    Yeah and when i tried my given solution, it worked fine and resulted as user wanted. Also i mentioned that there might be other solutions too.
    So, you mean, my given solution is completely Wrong? I will ofcourse take your opinion seriously as you've more knowledge than me.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,330
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Very simple GUI problem

    Quote Originally Posted by Mr.777 View Post
    Yeah and when i tried my given solution, it worked fine and resulted as user wanted. Also i mentioned that there might be other solutions too.
    So, you mean, my given solution is completely Wrong? I will ofcourse take your opinion seriously as you've more knowledge than me.
    Fair enough. Wrong is often relative, as in this case (eg it compiles and fixes the problem, but may not be best practice). The position properties of GridBagLayout should not be used for width properties, as they may result in unwanted behavior (such as a gridwith of 13). Width should be defined by the width one wishes - EAST/WEST/etc... make no sense when it comes to defining width.

  9. The Following User Says Thank You to copeg For This Useful Post:

    Mr.777 (March 20th, 2012)

  10. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Very simple GUI problem

    Quote Originally Posted by copeg View Post
    Fair enough. Wrong is often relative, as in this case (eg it compiles and fixes the problem, but may not be best practice). The position properties of GridBagLayout should not be used for width properties, as they may result in unwanted behavior (such as a gridwith of 13). Width should be defined by the width one wishes - EAST/WEST/etc... make no sense when it comes to defining width.
    Great. Thanks a lot.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. please help, simple problem
    By grandmst in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2011, 05:26 PM
  2. simple arithmetic problem
    By nadrii in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 26th, 2011, 09:35 PM
  3. Very simple problem...PLEASE HELP!
    By dungeondragon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 1st, 2011, 06:19 AM
  4. Simple problem...
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 5th, 2011, 11:02 PM
  5. Simple Actionlistener problem
    By olemagro in forum AWT / Java Swing
    Replies: 5
    Last Post: October 11th, 2010, 10:46 AM