"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.
..
>..If pack() isn't called twice the dialog doesn't render correctly.
> 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);