setAlignmentY not centering JLabel in BorderLayout - java

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.

Related

JFrame/BoxLayout weird size behavior

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);

Adding a label to the center south of a panel?

The code below adds the label to the left south of the panel, and when I use set location with the label, the position does not change. Is there a way to make the label be in the center south of the panel without the need for an extra panel?
EDIT: the JFrame has a BorderLayout and adds the panel to CENTER
JPanel pnl = new JPanel();
pnl.setPreferredSize(new Dimension(500,500));
JLabel lbl = new JLabel("label");
pnl.setLayout(new BorderLayout());
pnl.add(lbl, BorderLayout.SOUTH);
It seem you need to set text align of label to center panel?
If so, try this:
JPanel pnl = new JPanel();
pnl.setPreferredSize(new Dimension(500, 500));
JLabel lbl = new JLabel("label", SwingConstants.CENTER); //Set text align
pnl.setLayout(new BorderLayout());
pnl.add(lbl, BorderLayout.SOUTH);
lbl.setBackground(Color.red);
lbl.setOpaque(true); //Test background
getContentPane().add(pnl);
Result:

Adding labels to the next line of a BoxLayout

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

How can I make a layout like the attached image

I am trying to design a layout which contains a form and couple of items. but I found it too hard to put items in right places.
In the following image, the right frame is what I am aiming to design and the left on is what I could made.
And this is the code for the right frame:
public class GUI extends JFrame{
public GUI(){
JFrame frame = new JFrame("frame");
frame.setSize(600, 600);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Title"), BorderLayout.NORTH);
JPanel formPanel = new JPanel(new GridLayout(1,2));
panel.add(formPanel);
TitledBorder formPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
formPanel.setBorder(formPanelTitle);
//LEFT PANEL
JPanel labelsPanel = new JPanel(new GridLayout(4,1));
TitledBorder labelsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
labelsPanel.setBorder(labelsPanelTitle);
labelsPanel.add(new JLabel("Label 1"));
labelsPanel.add(new JLabel("Label 2"));
labelsPanel.add(new JLabel("Label 3"));
labelsPanel.add(new JLabel("Label 4"));
formPanel.add(labelsPanel);
//RIGHT PANEL
JPanel fieldsPanel = new JPanel(new GridLayout(4,1));
TitledBorder fieldsPanelTitle = BorderFactory.createTitledBorder("GridLayout(4,1)");
fieldsPanel.setBorder(fieldsPanelTitle);
fieldsPanel.add(new JTextField("Label 1"));
fieldsPanel.add(new JTextField("Label 2"));
fieldsPanel.add(new JTextField("Label 3"));
fieldsPanel.add(new JTextField("Label 4"));
formPanel.add(fieldsPanel);
//BOTTOM PANEL
JPanel bottomPanel = new JPanel(new GridLayout(2,1));
TitledBorder BottomPanelTitle = BorderFactory.createTitledBorder("GridLayout(2,1)");
bottomPanel.setBorder(BottomPanelTitle);
panel.add(bottomPanel, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(new JButton("Browse"));
buttonPanel.add(new JLabel("Label"));
TitledBorder buttonPanelTitle = BorderFactory.createTitledBorder("FlowLayout()");
buttonPanel.setBorder(buttonPanelTitle);
bottomPanel.add(buttonPanel);
JPanel secondButtonPanel = new JPanel(new GridLayout(1,2));
secondButtonPanel.add(new JButton("Back"));
secondButtonPanel.add(new JButton("Next"));
TitledBorder secondButtonPanelTitle = BorderFactory.createTitledBorder("GridLayout(1,2)");
secondButtonPanel.setBorder(secondButtonPanelTitle);
bottomPanel.add(secondButtonPanel);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
new GUI();
}
}
I am not sure if the code is really optimal, since there are a lot of inner panels and made it too complicated. Also I could not put items in the places I wanted to. Is there any suggestion or idea to make this layout look better?
Create a JPanel, using GridBagLayout and add your labels/fields to it, this forms the "center" portion of your layout.
Create a JPanel and add the Browse button a JLabel to it. Using GridBagConstraints#gridwidth set to REMAINDER, add this to your first panel
Create a JPanel, using BorderLayout, add the first panel to the CENTER position. Add the title Label to the NORTH position, you may need to adjust it's horizontalAlignment property
Create a JPanel using FlowLayout, aligned to the RIGHT and add your "Back" and "Next" buttons to it. Add this to the SOUTH position of the previous panel.
Check out Laying Out Components Within a Container for more details

JPanels: One with a JTextArea and another with a JLabel

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());

Categories

Resources