🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: JFileChooser size problem

#8252
From: Joi Ellis
Date: Sat, 07 Oct 2000 00:00
46 lines
2463 bytes
Okay, I found an even simpler method, though it's a bit of a hack.
It occured to me that some of the layout managers don't honor or even
use the values returned by getPreferredSize().  Usually this is just
annoying but this time it was useful.

So, I'm now doing this:
   
  JFileChooser fc=new JFileChooser();
  ...// stuff to configure and add the accessory goes here
  fc.invalidate();
  JFrame bogus = new JFrame();
  if ( computedSize == null ) {
    bogus.getContentPane().add(fc);
    bogus.pack();
    //System.out.println(bogus.getPreferredSize());
    computedSize = bogus.getPreferredSize();
    fc.setPreferredSize(computedSize);
    bogus.getContentPane().remove(fc);
    bogus.dispose();
    bogus=null;
  } else {
    fc.setPreferredSize(computedSize);
  }
  int option=fc.showSaveDialog(null);

The default layout manager in a JFrame's content pane is BorderLayout,
and BorderLayout doesn't honor preferred sizes.  The pack() on the
frame causes BorderLayout to resize everything to fit nicely in the
CENTER position, and sinces the JFileChooser is the only component,
it occupies the whole thing.  Suck the preferred size from this and
send directly to the JFileChooser to override the UI's default value.
Do this only the first time we instantiate this JFileChooser variant, 
and save the value in a static Dimension so future instances may reuse it.

I think this will even work cross-platform.


--
Joi Ellis                    Software Engineer
Aravox Technologies          joi@aravox.com, gyles19@visi.com

No matter what we think of Linux versus FreeBSD, etc., the one thing I
really like about Linux is that it has Microsoft worried.  Anything
that kicks a monopoly in the pants has got to be good for something.
           - Chris Johnson
Technical Details
Message-ID: <39DEC08B.C01F5739@aravox.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <39D7D59D.176E4FA5@aravox.com> <MPG.1445eb857969656e989bd0@forums.borland.com> <39DE8A64.819A20AF@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: Joi Ellis <joi@aravox.com>
Subject: Re: JFileChooser size problem
Date: 2000/10/07
Message-ID: <39DEC08B.C01F5739@aravox.com>
X-Deja-AN: 678566667
Content-Transfer-Encoding: 7bit
References: <39D7D59D.176E4FA5@aravox.com> <MPG.1445eb857969656e989bd0@forums.borland.com> <39DE8A64.819A20AF@aravox.com>
To: joi@aravox.com
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: Aravox Technologies
Mime-Version: 1.0
Reply-To: joi@aravox.com
Newsgroups: borland.public.jbuilder.java.swing