Question

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.

Was it helpful?

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.

OTHER TIPS

If you just add the file chooser panel to a panel it will retain its preferred size because by default a panel uses a FlowLayout.

Try adding the file chooser panel to the CENTER of a panel using a BorderLayout. Then hopefully the components will resize as you size the frame (assuming the file chooser panel uses appropriate layout managers).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top