6 Replies Latest reply on Jan 11, 2007 7:23 AM by 843805

    JFileChooser setMinimunSize not working?

    843805
      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();
           }});      

           }
                
      }
        • 1. Re: JFileChooser setMinimunSize not working?
          794342
          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();
          }});
          }
          • 2. Re: JFileChooser setMinimunSize not working?
            843805
            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 :(
            • 3. Re: JFileChooser setMinimunSize not working?
              794342
              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
              • 4. Re: JFileChooser setMinimunSize not working?
                843805
                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.
                • 5. Re: JFileChooser setMinimunSize not working?
                  843805
                  ...and I see now that the OP mentions he is already using Java 6, so never mind.
                  • 6. Re: JFileChooser setMinimunSize not working?
                    843805
                    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;
                                        }
                                    };