Hi all,
my brain must be mashed, otherwise i dont see whats wrong with the following
code..
<sscce>
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
public class Test extends JFrame{
private JTextArea excText;
private JButton okButton;
/** Creates a new instance of Test */
public Test() {
excText = new JTextArea("blah");
excText.setEditable(false);
initComponents();
}
private void initComponents(){
Dimension extDim = new Dimension(640, 240);
okButton = new JButton("Close");
okButton.setActionCommand("ok");
JScrollPane pane = new JScrollPane(excText);
pane.setSize(extDim);
pane.setPreferredSize(extDim);
pane.setMinimumSize(extDim);
pane.setMaximumSize(extDim);
JPanel textPanel = new JPanel();
textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.X_AXIS));
textPanel.setBorder(BorderFactory.createEtchedBorder());
textPanel.setBackground(Color.WHITE);
textPanel.add(pane);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BorderLayout());
buttonPanel.setBorder(BorderFactory.createEtchedBorder());
buttonPanel.setBackground(Color.WHITE);
buttonPanel.add(okButton, BorderLayout.EAST);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
add(textPanel);
add(buttonPanel);
pack();
}
public static void main(String[] args){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Test().setVisible(true);
}
});
}
}
</sscce>
I dont see using the same BoxLayout in more than one component, so there is
definitely no sharing going on.. am i being really stupid?
regards
Costas
Monique Y. Mudama - 07 Feb 2006 00:14 GMT
> Hi all,
>
[quoted text clipped - 29 lines]
>
> private void initComponents(){
[snip]
> setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
>
[quoted text clipped - 13 lines]
> I dont see using the same BoxLayout in more than one component, so there is
> definitely no sharing going on.. am i being really stupid?
I don't remember if you can pass BoxLayout its component while you're
still in the constructor ...

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Thomas Hawtin - 07 Feb 2006 01:51 GMT
>> public class Test extends JFrame{
> I don't remember if you can pass BoxLayout its component while you're
> still in the constructor ...
The JFrame constructor, and constructors of indirect subclasses, will
have all finished by the time the Test constructor has run. As Test does
not override any methods (so shouldn't really extend JFrame at all), it
doesn't matter. (In fact BoxLayout doesn't happen to do anything fancy
in its constructor.)
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Monique Y. Mudama - 07 Feb 2006 21:20 GMT
>>> public class Test extends JFrame{
>
[quoted text clipped - 8 lines]
>
> Tom Hawtin
Ah. Thanks for the info.

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Andrey Kuznetsov - 07 Feb 2006 21:29 GMT
> doesn't matter. (In fact BoxLayout doesn't happen to do anything fancy in
> its constructor.)
this is really strange, the reference to component is just keeped, but
unused.

Signature
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Thomas Hawtin - 07 Feb 2006 22:25 GMT
>> doesn't matter. (In fact BoxLayout doesn't happen to do anything fancy in
>> its constructor.)
>
> this is really strange, the reference to component is just keeped, but
> unused.
It's not unused. The layout methods check that they are being applied to
the correct container. Like many layout managers, it caches information
about the target, so it cannot be shared.
The problem is that the design of LayoutManager seems to have failed to
anticipate that layout managers will probably want to keep state.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Thomas Hawtin - 07 Feb 2006 00:29 GMT
> public class Test extends JFrame{
> [...]
[quoted text clipped - 3 lines]
> I dont see using the same BoxLayout in more than one component, so there is
> definitely no sharing going on.. am i being really stupid?
No you are not being stupid, but Swing is leading you into doing
something subtly wrong.
In 1.5, IIRC, JFrame, JInternalFrame and JApplet replaced the
implementation of add and setLayout the threw an exception with one that
delegate to the content pane. Delegating like this may seem to simplify
code, but it makes the implications significantly less simple.
What they appear to have failed to do was update BoxLayout similarly.
BoxLayout has a target of the JFrame, but JFrame.setLayout actually adds
it a layout manager to the content pane.
So either:
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
or
Container contentPane = getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
or
Box contentPane = new Box(BoxLayout.Y_AXIS);
contentPane.setOpaque(true);
setContentPane(contentPane)
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Konstantinos Dermitzakis - 07 Feb 2006 01:15 GMT
Thanks very much for both inputs :)
regards
Costas
Roedy Green - 07 Feb 2006 03:58 GMT
On Mon, 6 Feb 2006 23:52:53 -0000, "Konstantinos Dermitzakis"
<K.Dermitzakis@sms.ed.ac.uk> wrote, quoted or indirectly quoted
someone who said :
>I dont see using the same BoxLayout in more than one component, so there is
>definitely no sharing going on.. am i being really stupid?
Are you having a compile or runtime problem. What does it do? What
did you want it to do?

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.