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: My JPanel alignment is messy. How can I fix it?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default My JPanel alignment is messy. How can I fix it?

    Here is a ss of what I am talking about -



    I want that particular JPanel to fill it's empty space.

    I am using a layout manager to place 4 JPanels on a JFrame. However, I can't get the JPanel aligned WEST (with the JTextFields) to use up all of that empty space.

    Here is what my constructor for that particular JPanel looks like:

    	public UserParamsPanel() {
    		params = UserParams.instance();
     
    		pnlParams = new JPanel(new GridLayout(11, 2));
    		//Define parameter labels.
    		lblPreyLoneliness = new JLabel("Prey Loneliness");
    		lblPreyEaten = new JLabel("Prey Eaten");
    		lblPredatorIsolation = new JLabel("Predator Isolation");
    		lblPredatorStarvation = new JLabel("Predator Starvation");
    		lblPredatorLoneliness = new JLabel("Predator Loneliness");
    		lblPreyReproduction = new JLabel("Prey Reproduction");
    		lblPredatorReproduction = new JLabel("Predator Reproduction");
    		lblPreySurvivalWhenPredators = new JLabel("Prey Survival when Predators");
    		lblPredatorSurvivalWhenPrey = new JLabel("Predator Survival when Prey");
    		lblPreyInitialProbability = new JLabel("Prey Initial Probability");
    		lblPredatorInitialProbability = new JLabel("Predator Initial Probability");
     
    		//Initialize text fields.
    		txtPreyLoneliness = new JTextField(10);
    		txtPreyEaten = new JTextField(10);
    		txtPredatorIsolation = new JTextField(10);
    		txtPredatorStarvation = new JTextField(10);
    		txtPredatorLoneliness = new JTextField(10);
    		txtPreyReproduction = new JTextField(10);
    		txtPredatorReproduction = new JTextField(10);
    		txtPreySurvivalWhenPredators = new JTextField(10);
    		txtPredatorSurvivalWhenPrey = new JTextField(10);
    		txtPreyInitialProbability = new JTextField(10);
    		txtPredatorInitialProbability = new JTextField(10);
     
    		//Add elements above to Panel.
    		pnlParams.add(lblPreyLoneliness);
    		pnlParams.add(txtPreyLoneliness);
    		pnlParams.add(lblPreyEaten);
    		pnlParams.add(txtPreyEaten);
    		pnlParams.add(lblPredatorIsolation);
    		pnlParams.add(txtPredatorIsolation);
    		pnlParams.add(lblPredatorStarvation);
    		pnlParams.add(txtPredatorStarvation);
    		pnlParams.add(lblPredatorLoneliness);
    		pnlParams.add(txtPredatorLoneliness);
    		pnlParams.add(lblPreyReproduction);
    		pnlParams.add(txtPreyReproduction);
    		pnlParams.add(lblPredatorReproduction);
    		pnlParams.add(txtPredatorReproduction);
    		pnlParams.add(lblPreySurvivalWhenPredators);
    		pnlParams.add(txtPreySurvivalWhenPredators);
    		pnlParams.add(lblPredatorSurvivalWhenPrey);
    		pnlParams.add(txtPredatorSurvivalWhenPrey);
    		pnlParams.add(lblPreyInitialProbability);
    		pnlParams.add(txtPreyInitialProbability);
    		pnlParams.add(lblPredatorInitialProbability);
    		pnlParams.add(txtPredatorInitialProbability);
     
    		populateDefaultValues();
     
    		add(pnlParams);
     
    	}

    It's really simple. You can see that I just create the fields in a grid layout, add them to the panel and add the panel to the class itself (which extends JPanel).

    Is there a simple way I can modify my code so it will take up all of that empty space at the bottom of the panel? Any help is appreciated.

    Thanks!!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: My JPanel alignment is messy. How can I fix it?

    I'm confused by what exactly you're going for. Are you trying to change the size of the red outline? Are you trying to move the chart panel? Something else?

    (btw, this program looks pretty interesting!)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: My JPanel alignment is messy. How can I fix it?

    In the GridLayout class, there is the setVgap() method, which is supposed to set the vertical spacing between components. Try setting that and see what happens.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default Re: My JPanel alignment is messy. How can I fix it?

    Quote Originally Posted by KevinWorkman View Post
    I'm confused by what exactly you're going for. Are you trying to change the size of the red outline? Are you trying to move the chart panel? Something else?

    (btw, this program looks pretty interesting!)
    Sorry, I should have been more specific.

    Here is the example I am trying to mimic -



    Notice how the JTextFields on the WEST are sized to where the content aligns perfectly with the graphic on the EAST side.

    Basically, I want my JTextFields to take up all the usable space in the JPanel, without overlapping any of the other panels, so it can look just like the pic above.

    I am using a layout manager BorderLayout to place the JPanels, but I still get that result. Just want some tips.


    btw, the program is for a CS java programming class. It's one of those "evolution" simulators... seem to be popular projects among CS classes. I got it fully working, but my OCD has kicked in and I want the layout to be right too.

    --- Update ---

    Quote Originally Posted by aussiemcgr View Post
    In the GridLayout class, there is the setVgap() method, which is supposed to set the vertical spacing between components. Try setting that and see what happens.
    The issue isn't the gap, it's the width of the JTextFields. Though, I am wondering if there is an easier way to get the look I want w/o resizing all of the fields individually.

  5. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: My JPanel alignment is messy. How can I fix it?

    Though, I am wondering if there is an easier way to get the look I want w/o resizing all of the fields individually.
    The easiest way *is* to manually resize. Divide the height of that exceptionally cool simulator by 11 (minus padding) and set the height of the JTextFields. You can add a ComponentListener to the JFrame and override the componentResized() method if you need dynamic resizing.

    An alternative would be to use the GridBagLayout instead of the plain GridLayout. GridBagLayouts allow you to specify GridBagConstraints which gives the ability to define weighty = 1.0/11.0

    As a foot note, I'd give this full marks if I were marking (so long as the code is as well thought out as this question and the stuff you've already posted). No program of worth is ever 100% complete and it's a mistake to go full OCD. As a professional you will have strict time constraints so get used to signing something off even though it isn't up to you're OCD needs.

  6. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    Hisma (November 27th, 2013)

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: My JPanel alignment is messy. How can I fix it?

    I respectfully disagree that the best way is to manually set the size of each component. Instead, use nested layouts.

    The JPanel holding your JTextFields could be a BoxLayout with a Y_AXIS alignment.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following 2 Users Say Thank You to KevinWorkman For This Useful Post:

    ChristopherLowe (November 27th, 2013), Hisma (November 27th, 2013)

  9. #7
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: My JPanel alignment is messy. How can I fix it?

    No need to be respectful my friend - I learn more being wrong than right. BoxLayout makes a lot more sense than GridBags or a hard coding.

  10. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    KevinWorkman (November 27th, 2013)

  11. #8
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default Re: My JPanel alignment is messy. How can I fix it?

    Thank you Kevin and Christopher. I might actually try both methods and see which gives the best results.

    I already asked this question to my instructor, I know at this point I will get full marks anyway, but it's a matter of figuring out that last bit. I don't want to spend hours getting it right, which is why I wanted to find out a "quick" way. If I end up not easily figuring this out, I will just leave it as is, no harm no foul.

    I need to put my attention toward the JUnit test cases for now. If I have enough time left before the project is due, I will try to get the alignment right.

  12. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: My JPanel alignment is messy. How can I fix it?

    Kevin's nested layout suggestion is probably best, since it provides more flexibility from a maintenance point of view.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Alignment issues
    By fride360 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2011, 02:58 PM
  2. Output Alignment
    By Fahmi in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 22nd, 2010, 04:14 PM
  3. Output Alignment
    By xdrechsler in forum Loops & Control Statements
    Replies: 1
    Last Post: August 18th, 2010, 10:26 AM
  4. help! messy code
    By niecah in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 10th, 2010, 05:06 AM
  5. Alignment
    By angelaffxmaniac in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 08:55 AM