user8491933
user8491933

Reputation:

Set min/max size of JFileChooser - Java

I have a JFileChooser like this:

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

And now I wnat to set the minimum/maximum size of it, because it looks bad if I decrease the size, else (That isn't even the smallest size possible):

Example

Couldn't I freeze the size of the JFileChooser if the user wants to make it smaller than for example this?:

Example

I've tried out this: fileChooser.setMinimumSize(new Dimensio(400, 400));, but it didnt work.
And I think it doesn't look good, if the size "jumps" back, when the user makes the window smaller, than for example 600p * 600p.

Upvotes: 3

Views: 393

Answers (1)

VGR
VGR

Reputation: 44293

You can subclass JFileChooser, and customize the dialog in its createDialog method:

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 this, though. Other users will have different desktop themes and different fonts than you. 600×600 pixels may look good on your computer, but there is no guarantee it will be a good minimum size for others. It’s best to just accept that users can make a window unusably small if they really want to.

Upvotes: 3

Related Questions