Reputation: 113
I've got a finished application here, but there's a ton of blank space on the screen and I've been fiddling around with things for a while, but the picture below is the most I've managed to cut.
I'm assuming I'm not using a certain method or utility that I may or may not have used at all yet.
Here's the code:
public ListWindow() {
chooser = new JFileChooser();
this.setLayout(new GridLayout(3,1,1,1));
JPanel sortPanel = new JPanel();
JPanel displayPanel = new JPanel();
JPanel btns = new JPanel();
JLabel sortLabel = new JLabel("Sort by:");
sortGame = new JRadioButton("Game");
sortScore = new JRadioButton("Score");
sortBtn = new JButton("Sort");
ButtonGroup group = new ButtonGroup();
group.add(sortGame);
group.add(sortScore);
list = new JList(reviewList.toArray());
JScrollPane reviewPane = new JScrollPane(list);
reviewPane.setPreferredSize(new Dimension(400, 150));
windowBtn = new JButton("To Review Entry");
buttonActions();
sortPanel.add(sortLabel);
sortPanel.add(sortGame);
sortPanel.add(sortScore);
sortPanel.add(sortBtn);
displayPanel.add(reviewPane);
btns.add(windowBtn);
this.add(sortPanel);
this.add(displayPanel);
this.add(btns);
}
public static void main(String[] args) {
ListWindow window = new ListWindow();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle("CriticalStrike.com - Review Database");
window.pack();
window.setVisible(true);
}
}
Thanks for the help, guys!
Upvotes: 2
Views: 642
Reputation: 366
There's another possibility too, using GridBagLayout to push all your components up, leaving all the dead space at the bottom of your window.
You would need to add this to your main.
getContentPane().setLayout(new GridBagLayout());
You would then need something like this in ListWindow()
GridBagConstraints gBC = new GridBagConstraints();
//these two lines so it will still resize horizontally
gBC.fill = GridBagConstraints.HORIZONTAL;
gBC.weightx = 1.0;
You'll then add your panels with
this.add(panel, gBC);
Now the trick is to push everything up -- you can do this by putting an empty JLabel underneath the panels that will resize vertically. Like this:
JLabel jLabel1 = new JLabel();
gBC.fill = GridBagConstraints.VERTICAL;
gBC.weightx = 0.0;
gBC.weighty = 1.0;
this.add(jLabel1, gBC);
You might also need to set the vertical position of each component in the GridBagConstraints, but I doubt it if you add them in order.
Upvotes: 0
Reputation: 5496
This is caused by using a GridLayout, which causes equal sized spaces to be used by the components you add to it. In your case with one column and three rows, if the three components don't take up an equal amount of space from one to the next, then some of the GridLayout areas will have unused extra space.
I suggest using a BorderLayout here. You can add the first and third components to the north and south positions, which will try to use the least amount of room height wise and most amount of room width wise, and you can add the large text area to the center position, which will try to use the most amount of room height and width wise.
So something along the lines of...
this.setLayout(new BorderLayout());
...
this.add(sortPanel, BorderLayout.NORTH);
this.add(displayPanel, BorderLayout.CENTER);
this.add(btns, BorderLayout.SOUTH);
Upvotes: 3