I have this Java JFrame class, in which I want to use a boxlayout, but I get an error saying java.awt.AWTError: BoxLayout can't be shared. I've seen others with this problem, but they solved it by creating the boxlayout on the contentpane, but that is what I'm doing here. Here's my code:
class EditDialog extends JFrame {
JTextField title = new JTextField();
public editDialog() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("New entity");
getContentPane().setLayout(
new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(title);
pack();
setVisible(true);
}
}
Your problem is that you're creating a BoxLayout for a JFrame (this), but setting it as the layout for a JPanel (getContentPane()). Try:
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)
);
I've also found this error making this:
JPanel panel = new JPanel(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
The JPanel isn't initialized yet when passing it to the BoxLayout. So split this line like this:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
This will work.
I think that one important thing to highlight from the previous answers is that the BoxLayout's target (the first parameter) should be the same Container that the setLayout method is being called upon as in the following example:
JPanel XXXXXXXXX = new JPanel();
XXXXXXXXX.setLayout(new BoxLayout(XXXXXXXXX, BoxLayout.Y_AXIS));
If you're using the layout on a JFrame like:
JFrame frame = new JFrame();
frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
frame.add(new JLabel("Hello World!"));
The control is actually being added to the ContentPane so it will look like it's 'shared' between the JFrame and the ContentPane
Do this instead:
JFrame frame = new JFrame();
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(new JLabel("Hello World!"));
Related
I am trying to set the sizes of my JButtons in a JPanel with BoxLayout correctly, but the behavior is beyond weird.
It will take the height from JButton.setPreferredSize, but completely ignore the width. This also only works when all buttons are set to the same height. As soon as one is smaller, it will revert all of them to some random minimum size (which isn't even the same for all buttons)
My code is this:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500);
JPanel rightPanel = new JPanel();
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
JButton bBookmarks = new JButton("Bookmarks");
bBookmarks.setPreferredSize(new Dimension(200, 100));
//more buttons with same size
leftPanel.add(bBookmarks);
//more buttons
JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
mainPanel.setDividerLocation(200);
frame.add(mainPanel);
frame.setResizable(false);
frame.setVisible(true);
This creates this image.
The middle button is always wider than the rest as well. Using frame.pack() doesn't do anything except resizing the frame because the right panel is empty.
What am I doing wrong?
Edit: Should look like this:
Divide and conquer: break the design into small, easy to layout containers. In this case do not place the buttons directly in the left (BoxLayout) container but in a nested JPanel using GridLayout manager.
This ensures that all buttons have the same size:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//add all buttons to a panel using a GridLayout which shows all components having the same size
JPanel buttons = new JPanel(new GridLayout(0,1));
JButton bBookmarks = new JButton("Bookmarks"); buttons.add(bBookmarks);
JButton bPlotter = new JButton("Plotter"); buttons.add(bPlotter);
JButton bShips = new JButton("Ships"); buttons.add(bShips);
//add buttons and text area to a panel using BoxLayout
JPanel leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(100,400));
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
leftPanel.add(buttons);
leftPanel.add(new TextArea(10,30));
JPanel rightPanel = new JPanel();
rightPanel.setPreferredSize(new Dimension(600,400));
rightPanel.add(new JLabel("right pane"));
JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, leftPanel, rightPanel);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
I am trying to make a boxlayout filled with a Jlabel and 3 radio buttons in descending order. The program compiles fine but then errors out with the error BoxLayout cant be shared. I have seen people saying this error is because they are trying to attach it to a jframe, but in this case it is the jpanel being given the layout not a frame. This is the segment of code that compiles the window.
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel effortButtons = new JPanel();
JPanel skillButtons = new JPanel();
effortButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
skillButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
effortButtons.add(effortHeader);//this is what gives the error
effortButtons.add(oneEffort);
effortButtons.add(twoEffort);
effortButtons.add(threeEffort);
skillButtons.add(skillHeader);
skillButtons.add(oneSkill);
skillButtons.add(twoSkill);
skillButtons.add(threeSkill);
mainPanel.add(effortButtons, BorderLayout.WEST);
mainPanel.add(skillButtons, BorderLayout.EAST);
mainPanel.add(studentName, BorderLayout.NORTH);
mainPanel.add(next, BorderLayout.SOUTH);
add(mainPanel);
pack();
// xxxxxxxxxxxxx xxxxxxxxx
effortButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
You have to pass into the BoxLayout constructor the component that is getting the layout. So this should be:
effortButtons.setLayout(new BoxLayout(efforButtons, BoxLayout.Y_AXIS));
Likewise for our other JPanel -- change it to:
skillButtons.setLayout(new BoxLayout(skillButtons, BoxLayout.Y_AXIS));
Per the BoxLayout API:
public BoxLayout(Container target, int axis)
target - the container that needs to be laid out
I have a box layout that has a JLabel and a button next to it, as such:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
panel.add(new JLabel("Reference "));
panel.add(new JButton("HI"));
frame.add(panel);
frame.setSize(500,300);
frame.setVisible(true);
This correctly presents a label called reference and a button right next to it. But if I want to present the same thing right below it (a new label, and another button), how would I do that?
Because simply creating another panel, and emulating what I did before, doesn't seem to work.
I.e
JPanel newPanel = new JPanel();
newPanel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
Or using HTML in a new label like
JLabel s = new JLabel("<html> <br>newLaberl </html>");
adding this to the panel still would print it on the same line, after the button, not the next, any ideas?
You just have to set the layout of the JFrame get it working the way you said.
I used your example setting the frame layout to use a GridLayout
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
frame.setLayout(new GridLayout(2,1));
panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
panel.add(new JLabel("Reference "));
panel.add(new JButton("HI"));
panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
panel2.add(new JLabel("Reference2 "));
panel2.add(new JButton("HI2"));
frame.add(panel);
frame.add(panel2);
frame.setSize(500,300);
frame.setVisible(true);
boxlayout isn't maybe the best layout for such thing, i suggest you to go to
https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html
and take a look on the various layout and tutorial documented on Oracle's docs website.
this one could be usefull for your application
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
I've been at this for a good while now, but I cannot seem to get a hand of it. I'm trying to produce a JPanel that has a JTextArea above and two JLabels below, but my JLabel ends up on the left side of my JTextArea and I cannot make the other appear.
Here's my code (sorry for the display stuff- just filler really):
public JPanel contentPane() {
JPanel something = new JPanel();
String information = "Please";
info = new JTextArea(information, 4, 30);
info.setEditable(false);
info.setLineWrap(true);
info.setWrapStyleWord(true);
JPanel one = new JPanel(new BorderLayout());
one.setBackground(Color.WHITE);
one.setLocation(10, 10);
one.setSize(50, 50);
one.add(info, BorderLayout.CENTER);
something.add(one, BorderLayout.NORTH);
JPanel two = new JPanel(new BorderLayout());
two.setBackground(null);
two.setLocation(220, 10);
two.setSize(50, 50);
two.add(new JLabel("Please work"), BorderLayout.EAST);
two.add(new JLabel("Oh gosh, please"), BorderLayout.WEST);
something.add(two, BorderLayout.SOUTH);
something.setOpaque(true);
return something;
}
public static void GUI() {
JFrame frame = new JFrame("You Guessed It!");
DisplayStudent panel = new DisplayStudent();
frame.setContentPane(panel.contentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
frame.setVisible(true);
}
Please and thank you to anyone who takes the time to help.
When you create you something, you don't specify any layout manager, but later on you attempt to add one to something using BorderLayout constants -- which will not work, since the default layout manager for a JPanel is FlowLayout.
Try this instead;
JPanel something = new JPanel(new BorderLayout());
new to java and brand new to the site. I have a JLabel added to the center panel of a BorderLayout. I would like the JLabel to be centered in the panel; setAlignmentX appears to work, but setAlignmentY does not (the label appears at the top of the panel). Here is the code:
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel,BoxLayout.Y_AXIS));
JLabel label = new JLabel("This should be centered");
label.setAlignmentX(Component.CENTER_ALIGNMENT);
label.setAlignmentY(Component.CENTER_ALIGNMENT);
centerPanel.add(label);
contentPane.add(centerPanel, BorderLayout.CENTER);
I have also tried label.setVerticalAlignment(CENTER);, to no avail. I've looked for an answer in the API and in the Java Tutorials, on this site, and through a google search. Thanks!
You were close, try this:
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
JLabel label = new JLabel("This should be centered");
label.setHorizontalAlignment(SwingConstants.CENTER);
centerPanel.add(label, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.pack();
contentPane.setVisible(true);
}
One of the many joys of GUI programming in Java. I'd rather poke my eye out if I'm being honest.
I tried to vertically center align JButton but I had problem it was stretched. After fiddling I found this works:
JPanel jpTop = new JPanel(new BorderLayout());
jbStop = new JButton("Cancel");
JPanel extraPanel = new JPanel();
extraPanel.setLayout(new BoxLayout(extraPanel, BoxLayout.X_AXIS));
extraPanel.setAlignmentY(Component.CENTER_ALIGNMENT);
extraPanel.add(jbStop);
jpTop .add(extraPanel, BorderLayout.EAST);
Of course it works as well for JLabel.