简体   繁体   中英

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

例

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

例

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM