Question

Okay, so I have a BoxLayout set to Y_AXIS for a main panel I have in a JFrame. I tried to set the actual Frame to a BoxLayout set to X_AXIS then add my main panel to my JFrame, but I get an java.awt.AWTError saying: "BoxLayout can't be shared". Is there any other way of using BoxLayout that will allow me to do this?

ComponentPanel.setToolTipText("I go in the FramePanel as well as other of my types aligned up and down");

FramePanel.setLayout(new BoxLayout(FramePanel, BoxLayout.Y_AXIS));
FramePanel.add(ComponentPanel);

Frame.setLayout(new BoxLayout(Frame, BoxLayout.X_AXIS));
Frame.add(FramePanel);
Was it helpful?

Solution

Use the JFrame's content panel as the BoxLayout's target container:

frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));

Also, please try to follow Java Naming Conventions. Variable names should start with a lowercase letter; class names should start with an uppercase letter.

OTHER TIPS

BoxLayout is "static" in the "new BoxLayout". Try using a class extending Panel and placing Panel in a package folder to change the package hierarchy level to separate the "static field value" class variable Boxlayout.

e.g. class Mypanel extends Panel{..... which you would import as a package

import separateguipart.Mypanel;

Panel insertboxlayoutpanel = (Panel)new Mypanel();

or use the full classified JNDI name

Panel insertboxlayoutpanel = (Panel)new separateguipart.Mypanel();

insertboxlayoutpanel.setLayout(new BoxLayout(FramePanel, BoxLayout.Y_AXIS));

There is nothing wrong with the code. You just have to import some library which is import.javax.swing.; and import.awt.;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top