Forum Stats

  • 3,751,319 Users
  • 2,250,340 Discussions
  • 7,867,382 Comments

Discussions

JFileChooser setMinimunSize not working?

843805
843805 Member Posts: 49,999
edited Jan 11, 2007 2:23AM in Swing
I'm using Windows Xp service pack 2 and Java 6 build 104
maybe I'm doing something wrong, but setMinimumSize(Dimension) on JFileChooser is not working for me.

can someone help me?


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class FileChooserSizeTest {


JFrame frame;
////////////////////////////////////////////////////////////////////////////////
public FileChooserSizeTest(){

frame = new JFrame("File Chooser Test");
frame.setLayout(new BorderLayout());
JButton clicMe = new JButton("click me");
clicMe.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

JFileChooser fc = new JFileChooser();
fc.setMinimumSize(new Dimension(200,200)); // ????
fc.setDialogTitle("Save file As");

final int rVal = fc.showSaveDialog(frame);
if(rVal == JFileChooser.APPROVE_OPTION)
{

}

}
});
frame.setSize(300,300);
frame.add(clicMe, BorderLayout.CENTER);
frame.setVisible(true);
}
////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args){

SwingUtilities.invokeLater(new Runnable(){
public void run(){

new FileChooserSizeTest();
}});

}

}

Comments

  • 794342
    794342 Member Posts: 6,422
    add the indicated line and it should work
    (but you may get other problems, look you don't want, color etc)
    public static void main(String[] args){
      SwingUtilities.invokeLater(new Runnable(){
        public void run(){
          JDialog.setDefaultLookAndFeelDecorated(true);//<-----------
          new FileChooserSizeTest();
    }});
    }
  • 843805
    843805 Member Posts: 49,999
    Michael_Dunn
    yes it works! if I add JDialog.setDefaultLookAndFeelDecorated(true);

    but that's the only way you know?
    because yes it give me looks and feel problems my project :(
  • 794342
    794342 Member Posts: 6,422
    the only other way I've seen is to add a componenLlistener,
    and in componentResized() check the dimension - if under the min, reset to min
    but I've not seen anyone happy with this solution - the component can be
    dragged to small size, then 'snaps back' to the minimum
  • 843805
    843805 Member Posts: 49,999
    the only other way I've seen is to add a
    componenLlistener,
    and in componentResized() check the dimension - if
    under the min, reset to min
    but I've not seen anyone happy with this solution -
    the component can be
    dragged to small size, then 'snaps back' to the
    minimum
    I think one of the changes introduced in Java 6 is that setMinimumSize() on a Window will actually enforce the set size, meaning that the window size will "freeze" if the user tries to make it smaller. I haven't tried this myself though, so I won't vouch for it.
  • 843805
    843805 Member Posts: 49,999
    ...and I see now that the OP mentions he is already using Java 6, so never mind.
  • 843805
    843805 Member Posts: 49,999
    Try creating the file chooser like this
                    JFileChooser fc = new JFileChooser(){
                        protected JDialog createDialog(Component parent) throws HeadlessException {
                            JDialog dialog = super.createDialog(parent);
                            dialog.setMinimumSize(new Dimension(200,200));
                            return dialog;
                        }
                    };
This discussion has been closed.