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; }