user1253201
user1253201

Reputation: 101

Size of JFilechooser

I am trying to put a JFileChooser box on my GUI but if I just do this

JFileChooser filechooser = new JFileChooser ();

then it will just show a huge file selection window on the panel (I do not want that), so I want to make a small box filechooser (with a name for example "choose file") that when I click it, a window will popup, so then I can choose the file.

Upvotes: 1

Views: 2169

Answers (2)

user unknown
user unknown

Reputation: 36229

Call

filechooser.setPreferredSize (new java.awt.Dimension (800, 800));

before calling showOpenDialog with whatever Dimension you like.

But I would suggest to either maximize the Dialog, because in the moment I like to open a File, I don't like to watch something else - find a file, and close the dialog, without much scrolling, because somebody thought it looks more nice.

If you like to prevent wasting space, you can precalculate the needed size for the window, which might be a lot of work, but could pay off, if you use the Component frequently.

Upvotes: 1

Juvanis
Juvanis

Reputation: 25950

Use a button to open your file chooser and use setPreferredSize() method to make the file chooser smaller in size:

JButton button = new JButton("Choose a file!");
button.addActionListener(new ActionListener()
{ 
  public void actionPerformed(ActionEvent e)
  { 
     JFileChooser fileChooser = new JFileChooser();
     fileChooser.setDialogTitle( "Choose a file" );
     fileChooser.setVisible( true );
     fileChooser.setPreferredSize( new Dimension(100, 100) );
  } 
});

Upvotes: 2

Related Questions