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

Thread: JPanel within JPanel causing odd stretching behavior.

  1. #1
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default JPanel within JPanel causing odd stretching behavior.

    So I have this database program I've been working on, just for fun and to tinker around with using multiple files within a package and to learn about JDBC. I use a small MySQL database I made a few months ago that contains various information on the top 20 most populous U.S. cities. The program is working pretty well, but there's one thing I can't work out on the part of the program that displays search queries.

    So here is the GUI window for the search.

    searchGuiWindow.jpg

    So it's a bunch of check boxes. You check off what you want to get from the database and press the Submit button. Here's how it works.

    1) There is an array of booleans called selections[], and all elements are FALSE by default. If you check a box, a certain index (corresponding to the box that was checked) gets switched to TRUE. If you uncheck, it goes back to FALSE.
    2) Press Submit.
    3) The button listener steps through a bunch of if statements which each check an element in the selections array.
    4) If an element is selected, run a SQL select query to pull that data out of the database and load it into a string.
    5) That string is then returned to an array of strings called shownResults[].
    6) The program then calls a function called createResultsPanel(). This function builds a JPanel which adds subpanels containing information requested and only the information requested. So if you ask for Demonym, it will show there. If not, it won't be on the panel.
    7) The function returns the JPanel, which is then used in a JOptionPane.

    The problem is something of formatting. Everything looks okay unless I ask for Sports Team information. So for example, here's what I get if I ask for everything about Chicago except the sports info.

    _noSportsQuery.jpg

    Notice how it's nice and compact and such. That's how I want it. (There's a gap of empty space at the bottom but I think I know how to take care of that.)

    And here is if you include sports, but exclude population and area code.

    _SportsQuery.jpg

    It's all stretched out. The sports info is there, down at the bottom, but there's huge swaths of padding between the boxes for some reason. And this ONLY happens if you put in the sports stuff.

    Here's the code for the function that builds the panel.

     
    private JPanel createResultsPanel()
    		{
    			JPanel resultsPanel = new JPanel();
    			JPanel headerPanel = new JPanel();
    			JPanel subPanel;
     
     
    			resultsPanel.setLayout(new GridLayout(9,1));
     
    			//Some header information.
    			headerPanel.add(new JLabel("Thank you for your query.  Here are the results for " +jtfCity.getText()));
    			resultsPanel.add(headerPanel);
    			//User selected a state.
    			if(selections[STATE])
    			{
     
    				subPanel = new JPanel();
     
    				subPanel.add(new JLabel("State"));
    				subPanel.add(new JTextField(shownResults[STATE]));
    				resultsPanel.add(subPanel);
     
    			}
    			//User selected mayor name.
    			if(selections[MAYOR])
    			{
    				subPanel = new JPanel();
     
    				subPanel.add(new JLabel("Mayor Name"));
    				subPanel.add(new JTextField(shownResults[MAYOR]));
    				resultsPanel.add(subPanel);
    			}
    			//User selected demonym.
    			if(selections[DEMONYM])
    			{
    				subPanel = new JPanel();
     
    				subPanel.add(new JLabel("Demonym"));
    				subPanel.add(new JTextField(shownResults[DEMONYM]));
    				resultsPanel.add(subPanel);
    			}
    			//User selected population.
    			if(selections[POPULATION])
    			{
    				subPanel = new JPanel();
     
    				subPanel.add(new JLabel("Population"));
    				subPanel.add(new JTextField(shownResults[POPULATION]));
    				resultsPanel.add(subPanel);				
    			}
    			//User selected area.
    			if(selections[AREA])
    			{
    				subPanel = new JPanel();
     
    				subPanel.add(new JLabel("Area"));
    				subPanel.add(new JTextField(shownResults[AREA]));
    				resultsPanel.add(subPanel);
    			}
    			//User selected area codes.
    			if(selections[AREACODES])
    			{
    			       subPanel = new JPanel();
     
    				subPanel.add(new JLabel("Area Codes"));
    				subPanel.add(new JTextField(shownResults[AREACODES]));
    				resultsPanel.add(subPanel);
     
    			}
    			//User selected counties.
    			if(selections[COUNTIES])
    			{
    				subPanel = new JPanel();
     
    				subPanel.add(new JLabel("Counties"));
    				subPanel.add(new JTextField(shownResults[COUNTIES]));
    				resultsPanel.add(subPanel);
    			}
    			//User selected sports information.
    			if(selections[SPORTSTEAMS])
    			{
    				subPanel = new JPanel();
    				subPanel.add(new JLabel("Sports Teams"));
    				subPanel.add(new JTextArea(shownResults[SPORTSTEAMS]));
     
    				resultsPanel.add(subPanel);				
    			}
     
     
    			return resultsPanel;
    		}


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanel within JPanel causing odd stretching behavior.

    JPanel uses FlowLayout by default and may be adjusting the vertical gap to equally space the components, but I don't know that for sure. It's a guess based on what you've shown.

    To get control, try another layout manager or experiment with setting the gaps in FlowLayout. Here's the FlowLayout tutorial page, but I recommend you experiment with other layouts (maybe GridLayout) to get the look you're after.

  3. #3
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: JPanel within JPanel causing odd stretching behavior.

    Quote Originally Posted by GregBrannon View Post
    JPanel uses FlowLayout by default and may be adjusting the vertical gap to equally space the components, but I don't know that for sure. It's a guess based on what you've shown.

    To get control, try another layout manager or experiment with setting the gaps in FlowLayout. Here's the FlowLayout tutorial page, but I recommend you experiment with other layouts (maybe GridLayout) to get the look you're after.
    I'm already using GridLayout. I had forgotten about the horizontal and vertical gap arguments. I added those in, starting with gaps of 1 and 1. No change.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanel within JPanel causing odd stretching behavior.

    Sorry, I didn't see the GridLayout statement.

    The 'grids' in GridLayout are required to all be the same size, autosizing to fit the largest. That's what's causing the size to expand when the SportsTeams panel is added.

  5. #5
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: JPanel within JPanel causing odd stretching behavior.

    Quote Originally Posted by GregBrannon View Post
    Sorry, I didn't see the GridLayout statement.

    The 'grids' in GridLayout are required to all be the same size, autosizing to fit the largest. That's what's causing the size to expand when the SportsTeams panel is added.
    I see what you're saying. Because the Sports panel has a JText area with 5 rows (in the case of Chicago), it's causing every other grid to expand to equal it. That also explains why Memphis (which has only one team, the Memphis Grizzlies basketball team), looks normal.

    So root cause identified. Solutions? Hold on, gonna think out loud here so I don't forget it in a few minutes....

    Right now I'm adding all information into one of 9 sub-panels, one panel each for State, Mayor, Demonym, Population, Area, Area Code, Counties, and Sports, plus one header panel with that "Thank You" message. Each of these 9 sub-panels is added on to the main returned panel, and are all part of the Grid Layout. Maybe cut that down to 8, and put the sports stuff in a separate panel.

    Side-note: Is there a way to deactivate quick reply on these forums?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanel within JPanel causing odd stretching behavior.

    I think the solution you've suggested is a reasonable approach. You should know if that will accommodate the rest of the design you have in mind, and I can't be sure.

    I'm not an expert on this forum software, and I don't completely understand your question. Do you mean, make the Quick Reply box at the bottom just go away? For everybody? Why would you/we want that?

  7. #7
    Member
    Join Date
    May 2013
    Posts
    106
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: JPanel within JPanel causing odd stretching behavior.

    Quote Originally Posted by GregBrannon View Post
    I'm not an expert on this forum software, and I don't completely understand your question. Do you mean, make the Quick Reply box at the bottom just go away? For everybody? Why would you/we want that?
    Yeah that's the feature I meant, and I meant just for me. Some forums have the option to deactivate certain features on a per-user basis at the user's request.

    I don't like it because it seems to be the only option for replies. I can't seem to do a reply using the same interface as the one for new posts. The Quick-reply thing has fewer buttons and features.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JPanel within JPanel causing odd stretching behavior.

    Ahh, I see what you mean. The big blue "+Reply to Thread" button and both "Reply" and "Reply With Quote" labels jump to the Quick Reply box rather than going straight to the Advanced editor. I checked the available settings under Forum Actions, General Settings, and I don't see an option to do what you want. In fact, the option to use the Standard Editor is selected.

    I have made suggestions in the "Forum Updates and Feedback" section, but I don't get a good response there. After letting it sit there for a few days (with only jps responding), I PM "Admin" with the same request and usually get a good response that way. They can't always fix or do what I ask, but they are responsive and do seem to try.

Similar Threads

  1. Adding a Jpanel from another class to JPanel in a main class
    By saniadeyi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 22nd, 2013, 03:20 AM
  2. Replies: 5
    Last Post: February 15th, 2013, 05:01 PM
  3. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM