🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8253

Re: JFileChooser size problem

#8253
From: John McGrath TeamB
Date: Sat, 07 Oct 2000 00:00
123 lines
5632 bytes
On Fri, 06 Oct 2000 10:07:04 -0500, Joi Ellis wrote:

> > The components are not smaller than requested;  they
> > just do not fit in the JFileChooser.
> 
> To be pendantic, don't you mean the JFileChooser no longer
> fits within the fixed-size JDialog box the display methods
> are creating?
Actually, no.  I am not immune from saying something different than what 
I mean, but in this case I meant what I said and I said what I meant.

Each AWT component has a position and a size.  In most cases, the parent 
will lay out its child components so that they fall within the bounds of 
the parent, but that is not actually required.  If a child is all or 
partially outside of the bounds of the parent, it will be clipped.

In this case, the size of the JFileChooser, not the dialog is fixed 
(500x300), but the JFileChooser's layout code (which uses BoxLayout) lays 
out the children so that they extend outside of the bounds of the 
JFileChooser.

> I'm still unclear on one point:  The code snippet you quoted
> yesterday indicated that the dialog's getPreferredSize method
> returned the values from the static variables.  So, even if I
> setPreferredSize with my desired values, that getter is still
> going to return the static constants, right?  Setting the
> preferred size in my experiments had no effect on the
> manufactured dialog box.

The code that I quoted was from MetalFileChooserUI.  This class is the UI 
delegate for the JFileChooser when using the metal look-and-feel.  There 
are similar UI delegate classes for all look-and-feels.

JFileChooser (and all JComponents) will always return the preferred size 
set using getPreferredSize() before asking the UI delegate.  And if there 
is no delegate or the delegate returns null, it will pass through to the 
AWT implementation of getPreferredSize().

I think you may be thinking that a JFileChooser is derived from JDialog, 
but it is not.  JFileChooser is derived from JComponent, so it is a 
normal component that could be added to any container.  The various 
showXxxxxDialog() methods create a JDialog, add the JFileChooser to the 
JDialog, and then pack and show the dialog.

The Dialog size is indirectly based on the JFileChooser's preferred size.  
So setting the preferred size of the JFileChooser, before the call to 
showXxxxxDialog() should control the size of the JFileChooser and the 
dialog used to show it.

> One workaround I've played with is to not use the dialog methods
> at all, but to lay the bugger out in a JFrame.  But I encountered
> problems with it not passing the button presses back to my app.

You could do that, or place it in a JDialog yourself, but unfortunately 
JFileChooser saves which button was pressed in a private variable, and 
there is no way to get that value (more cheese).  The showXxxxDialog() 
methods return the value of the private variable.

Here is the test program I wrote when looking at this.  The Accessory 
class used is the one that you provided.  It sets the preferred size for 
the JFileChooser, and it works properly.  But as I said before, this is 
dependant on the way that the FileChooserUI lays out the JFileChooser, so 
a size that works for one look-and-feel may not work for others.

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

public class FileOpenAcc
    extends JFrame
{
    private JButton showButton = new JButton();

    public static void main(String[] args) {
        FileOpenAcc frame = new FileOpenAcc();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setLocation( 300, 300 );
        frame.pack();
        frame.show();
    }

    public FileOpenAcc() {
        try {
            jbInit();
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    private void jbInit() throws Exception {
        showButton.setText("Show file chooser");
        showButton.addActionListener(
            new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    showButton_actionPerformed(e);
                }
            }
        );
        this.getContentPane().add(showButton, BorderLayout.CENTER);
    }

    void showButton_actionPerformed(ActionEvent e) {
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode( JFileChooser.FILES_ONLY );
        fc.setDialogTitle( "File Open Accessory Test" );
        fc.setAccessory( new Accessory() );
        fc.setPreferredSize( new Dimension( 600, 400 ) );
        fc.showSaveDialog( null );
        System.exit( 0 );
    }
}
===============================================================

-- 
Regards,

John ("Horton") McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID: <MPG.1448ad268987e04f989be5@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <39D7D59D.176E4FA5@aravox.com> <MPG.1445eb857969656e989bd0@forums.borland.com> <39DCAF46.691AA246@aravox.com> <MPG.1447408799154e989bd8@forums.borland.com> <39DDEA98.8723F6FA@aravox.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,2f241fe4201eef13
X-Google-Attributes: gid111d45,public
From: John McGrath  [TeamB] <ng-b@JPMcGrath.net>
Subject: Re: JFileChooser size problem
Date: 2000/10/07
Message-ID: <MPG.1448ad268987e04f989be5@forums.borland.com>
X-Deja-AN: 678595182
References: <39D7D59D.176E4FA5@aravox.com> <MPG.1445eb857969656e989bd0@forums.borland.com> <39DCAF46.691AA246@aravox.com> <MPG.1447408799154e989bd8@forums.borland.com> <39DDEA98.8723F6FA@aravox.com>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.swing