Search code examples
javaswingjfilechooserfile-browser

How to resize a JFile Chooser


I am designing a file browser using Java Swing, and here is what I have so far:

I have a JFileChooser in a panel, however it stays the same size when I reshape the window. However, I want to make it look like this:

Without the items inside resizing of course.

Is it possible to make the actual Browser box resize along with the form?

EDIT: I do not want a popup JFileChooser, the JFileChooser is INSIDE the Frame.


Solution

  • You don't need to add the file chooser to a panel - if you just initialize one and set it visible, it will automatically be resizable.

    JFileChooser chooser = new JFileChooser();
    chooser.setVisible(true);
    int returnVal = chooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        //continue your code here
    

    To incorporate the file chooser into the panel, try this:

    JFrame frame = new JFrame();
    JPanel panel = new JPanel(new BorderLayout());
    JFileChooser chooser = new JFileChooser();
    panel.add(chooser);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
    

    I'm not sure how you used the BorderLayout before but this code works perfectly on my computer.