Reduce Empty Space in JFrame

CSRadical

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.

enter image description here

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!

NESPowerGlove

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related