Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2004

Tip: Looking for answers? Try searching our database.

Calling pack()

Thread view: 
Oli - 30 Jan 2004 11:48 GMT
I've been looking at the Java Look & Feel Design Guidelines, specifically at
dialogs. In this example,
http://java.sun.com/products/jlf/ed2/samcode/prefere.html, there are two
calls to the dialog's pack() method - once at the end of the constructor and
again in the main method on instance creation.

Could someone please explain why I have to call the pack method twice? I've
implemented my own dialog in a similar fashion to the example and find if I
only call pack once the dialog isn't correctly displayed (i.e., it's
cropped).
Thomas Weidenfeller - 30 Jan 2004 12:58 GMT
> Could someone please explain why I have to call the pack method twice?

Because of the same reason you posted your question twice: It's a mistake.

/Thomas
Oli - 31 Jan 2004 16:30 GMT
> > Could someone please explain why I have to call the pack method twice?
>
> Because of the same reason you posted your question twice: It's a mistake.
>
> /Thomas

Mistake in what way? A mistake of the author in calling it twice or a bug in
Java? If pack() isn't called twice the dialog doesn't render correctly.
Andrew Thompson - 31 Jan 2004 22:09 GMT
"Oli" ...
> "Thomas Weidenfeller" ...
> > > Could someone please explain why I have to call the pack method twice?
> >
> > Because of the same reason you posted your question twice: It's a mistake.
..
> Mistake in what way? A mistake of the author in calling it twice or a bug in
> Java?

pack() does not need to be called twice.

BTW - you gave the link to the immediate code,
but where is the project that wraps around it?
Where are StringGridBagLayout, IntegerComboBoxEditor,
and AlignListCellRenderer etc. classes?

>..If pack() isn't called twice the dialog doesn't render correctly.

Which pack() did you call, did you try
each separately?

--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
Oli - 01 Feb 2004 00:23 GMT
> pack() does not need to be called twice.
>
> BTW - you gave the link to the immediate code,
> but where is the project that wraps around it?
> Where are StringGridBagLayout, IntegerComboBoxEditor,
> and AlignListCellRenderer etc. classes?

Hi Andrew,

The code in the link is in it's entirety (well, that's available) as it's
just an example. My code, appended at the bottom of this message, is much
simpler and doesn't use the extra classes you mention but provides the same
example.

> >..If pack() isn't called twice the dialog doesn't render correctly.
>
> Which pack() did you call, did you try
> each separately?

I've tried each method call separately and it makes no difference - just one
call produces a squished dialog box.

> --
> Andrew Thompson
> * http://www.PhySci.org/ Open-source software suite
> * http://www.PhySci.org/codes/ Web & IT Help
> * http://www.1point1C.org/ Science & Technology

public class AboutDialog extends JDialog {

   private ResourceBundle resources = null;

   public AboutDialog(Frame owner, boolean modal) {
       super(owner, modal);
       initResources();
       initComponents();
       pack();  // 1st call
   }

   private void initResources() {
       resources = ResourceBundle.getBundle("AboutDialog",
               Locale.getDefault());
   }

   private void initComponents() {
       Container contentPane = getContentPane();
       contentPane.setLayout(new GridBagLayout());

       GridBagConstraints constraints = null;

       JTextArea txaCopyright = new JTextArea();
       txaCopyright.setBackground(new Color(
               MetalLookAndFeel.getControl().getRGB()));
       txaCopyright.setColumns(30);
       txaCopyright.setEditable(false);
       txaCopyright.setLineWrap(true);
       txaCopyright.setText(resources.getString("copyrightTextArea.text"));
       txaCopyright.setWrapStyleWord(true);

       constraints = new GridBagConstraints();
       constraints.insets = new Insets(12, 12, 0, 11);
       constraints.weightx = 1.0;
       contentPane.add(txaCopyright, constraints);

       CloseAction closeAction = new CloseAction();

       JButton btnClose = new JButton();
       btnClose.setAction(closeAction);
       btnClose.setMargin(new Insets(2, 12, 2, 12));

       constraints = new GridBagConstraints();
       constraints.anchor = GridBagConstraints.EAST;
       constraints.gridy = 1;
       constraints.insets = new Insets(17, 12, 11, 11);
       constraints.weightx = 1.0;
       contentPane.add(btnClose, constraints);

       getRootPane().setDefaultButton(btnClose);

       setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       setResizable(false);
       setTitle(resources.getString("dialog.title"));

       getRootPane().getInputMap(
               JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                       KeyStroke.getKeyStroke("ESCAPE"), "closeDialog");
       getRootPane().getActionMap().put("closeDialog", closeAction);
   }

   class CloseAction extends AbstractAction {

       public CloseAction() {
           super(resources.getString("closeButton.label"));
       }

       public void actionPerformed(ActionEvent e) {
           setVisible(false);
           dispose();
       }
   }

   public static void main(String[] args) {
       javax.swing.JFrame frame = new javax.swing.JFrame() {
           public java.awt.Dimension getPreferredSize() {
               return new java.awt.Dimension(200, 100);
           }
       };
       frame.setTitle("Debugging frame");
       frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
       frame.pack();
       frame.setVisible(false);

       AboutDialog dialog = new AboutDialog(frame, true);
       dialog.addWindowListener(new java.awt.event.WindowAdapter() {
           public void windowClosing(java.awt.event.WindowEvent e) {
               System.exit(0);
           }
           public void windowClosed(java.awt.event.WindowEvent e) {
               System.exit(0);
           }
       });
       dialog.pack(); // 2nd call
       dialog.setLocationRelativeTo(null);
       dialog.setVisible(true);
   }
}
Andrew Thompson - 01 Feb 2004 00:59 GMT
"Oli" ...
> "Andrew Thompson" ...
> > pack() does not need to be called twice.
> >
> > BTW - you gave the link to the immediate code,
...
> The code in the link is in it's entirety (well, that's available) as it's
> just an example. My code, appended at the bottom of this message, is much
> simpler and doesn't use the extra classes you mention but provides the same
> example.

You need to review this..
http://www.physci.org/codes/sscce.jsp

I tried compiling your code, the imports
are missing.

After 10 minutes chasing down the appropriate
imports, I realised it is just as easy for you
to provide a compilable example as it is
for me to _make_ yours compilable.

Further to that, it seemed there were some
classes in there that were completely unnecessary
to display the problem.

You have, for example, an 'ActionEvent' when
you do not implement an ActionListener, not
that Actions have anything to do with a rendering
problem.

Please reduce it to the smallest, self-contained
source that displays the behaviour.

Two comments before you toddle off though:

1) Even after I had battled through the imports and
got it to compile, An attempt to run it produced..
MissingResourceException.  Fix it by not making
a call to a ResourceBundle, just use dummy data,
after all ..it's a GUI problem!

2) And, you might try commenting out the change to
the L&F, changing the L&F is not as simple as it
appears, and has a few 'gotchas'.

Back to you.

[ Oh, and the acid test for 'is it compilable'?
http://www.physci.org/javac.jsp ]

--
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
Alan Moore - 01 Feb 2004 04:23 GMT
>        JTextArea txaCopyright = new JTextArea();
>        txaCopyright.setBackground(new Color(
[quoted text clipped - 4 lines]
>        txaCopyright.setText(resources.getString("copyrightTextArea.text"));
>        txaCopyright.setWrapStyleWord(true);

>        constraints = new GridBagConstraints();
>        constraints.insets = new Insets(12, 12, 0, 11);
>        constraints.weightx = 1.0;
>        contentPane.add(txaCopyright, constraints);

The problem is that, the first time pack() is called, the
LayoutManager doesn't know how tall the textarea is going to be.  It
just can't figure that out in one pass, so it assumes there will be
only one row and calculates the rest of the layout accordingly.

You need to decide in advance how many lines to display--something
less than what you think you'll really need--and make the textarea
scrollable so users will be able to read all of its contents:

       JTextArea txaCopyright = new JTextArea(20,30);

       // ... get rid of the setColumns(30) line

       JScrollPane sp = new JSCrollPane(txaCopyright);

       // ...

       contentPane.add(sp, constraints);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.