Java – Set the minimum/maximum size of JFileChooser – Java

Set the minimum/maximum size of JFileChooser – Java… here is a solution to the problem.

Set the minimum/maximum size of JFileChooser – Java

I have a JFileChooser:

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
    do something
}

Now I want to set its min/max size because if I reduce the size it looks bad, otherwise (which is not even the minimum size possible):

Example

If the user wants to make it smaller than this, can’t I jam the size of JFileChooser? :

Example

I’ve tried this: fileChooser.setMinimumSize(new Dimensio(400, 400)); , but it didn’t work.
And I don’t think it looks very good if the size “jumps” back when the user makes the window smaller, e.g. 600p * 600p.

Solution

You can subclass JFileChooser and use it in it createDialog Customize dialog box method in

JFileChooser fileChooser = new JFileChooser() {
    private static final long serialVersionUID = 1;

@Override
    public JDialog createDialog(Component parent) {
        JDialog dialog = super.createDialog(parent);
        dialog.setMinimumSize(new Dimension(600, 600));
        return dialog;
    }
};

You won’t gain much by doing so, though. Other users will have a different desktop theme and different fonts than you. 600×600 pixels may look good on your computer, but there is no guarantee that it will be the appropriate minimum size for others. It’s better to just accept that users can shrink the window to the point where it’s unusable, if they really want to.

Related Problems and Solutions