Java - Center a BoxLayout in a JPanel - java

I'm working with JPanel and BoxLayout and I want to Center it on the window, I have this code:
JPanel jorder = new JPanel();
jorder.setLayout(new BoxLayout(jorder, BoxLayout.Y_AXIS));
jorder.setBorder(BorderFactory.createTitledBorder("Order"));
JLabel jusuari = new JLabel("User name: edetorres");
JLabel jproduct = new JLabel("Product: Tallat");
JButton jserve = new JButton("Serve");
jorder.add(jusuari);
jorder.add(jproduct);
jorder.add(jserve);
And it returns this (only the left panel of the photo):

Related

Why is BorderLayout not displaying properly with multiple layers?

I am setting up a UI for a blackjack helper program and while I know its not the most beautiful way to do things, it makes sense.
The layering for what seems to be the upper layers is not working properly. Any suggestions?
The left should have four layers, as should the middle and the right side should have two layers between the keypad and the enter buttons. Image is attached below.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
// main method, runs the program
class BlackjackHelper
{
public static void main(String\[\] args)
{
Frame21 game = new Frame21();
//pop window with rules of game
}
}
// JFrame Construction
class Frame21 extends JFrame
{
// create needed components of program
JLabel questionDisplay = new JLabel("What is your first card?");
JLabel actionDisplay = new JLabel("Enter your first card");
JLabel dealerCardText = new JLabel("Dealer's Card:");
JLabel dealerCardDisplay = new JLabel("N/A");
JLabel handOneText = new JLabel("Hand One:");
JLabel handOneDisplay = new JLabel("N/A");
JLabel handTwoText = new JLabel("Hand Two:");
JLabel handTwoDisplay = new JLabel("N/A");
JLabel statsText = new JLabel("Win %:");
JLabel statsDisplay = new JLabel("N/A");
JButton aceButton = new JButton("A");
JButton twoButton = new JButton("2");
JButton threeButton = new JButton("3");
JButton fourButton = new JButton("4");
JButton fiveButton = new JButton("5");
JButton sixButton = new JButton("6");
JButton sevenButton = new JButton("7");
JButton eightButton = new JButton("8");
JButton nineButton = new JButton("9");
JButton tenButton = new JButton("10");
JButton faceButton = new JButton("F");
JButton clearButton = new JButton("C");
JButton standButton = new JButton("Stand");
JButton hitButton = new JButton("Hit");
JButton doubleButton = new JButton("Double");
JButton splitButton = new JButton("Split");
JButton winButton = new JButton("Win");
JButton loseButton = new JButton("Lose");
JButton resetButton = new JButton("Reset All");
JButton enterButton = new JButton("Enter");
public Frame21()
{
// JFrame - the main area of the program
JFrame frame = new JFrame("Blackjack Helper");
// JPanel right - the rightside of the program
JPanel rightSide = new JPanel();
JPanel rightNorthSide = new JPanel();
rightNorthSide.setLayout(new GridLayout(3,4));
rightNorthSide.add(aceButton);
rightNorthSide.add(twoButton);
rightNorthSide.add(threeButton);
rightNorthSide.add(fourButton);
rightNorthSide.add(fiveButton);
rightNorthSide.add(sixButton);
rightNorthSide.add(sevenButton);
rightNorthSide.add(eightButton);
rightNorthSide.add(nineButton);
rightNorthSide.add(tenButton);
rightNorthSide.add(faceButton);
rightNorthSide.add(clearButton);
JPanel rightSouthSide = new JPanel();
rightSouthSide.add(resetButton, BorderLayout.WEST);
rightSouthSide.add(enterButton, BorderLayout.EAST);
rightSide.add(rightNorthSide, BorderLayout.NORTH);
rightSide.add(rightSouthSide, BorderLayout.SOUTH);
frame.add(rightSide, BorderLayout.EAST);
// JPanel Center - the center of the program
JPanel center = new JPanel();
JPanel centerNorth = new JPanel();
centerNorth.add(questionDisplay, BorderLayout.NORTH);
centerNorth.add(actionDisplay, BorderLayout.SOUTH);
JPanel centerSouth = new JPanel();
JPanel centerSouthNorth = new JPanel();
centerSouthNorth.add(dealerCardText, BorderLayout.WEST);
centerSouthNorth.add(dealerCardDisplay, BorderLayout.EAST);
JPanel centerSouthSouth = new JPanel();
JPanel centerSouthSouthWest = new JPanel();
centerSouthSouthWest.add(handOneText, BorderLayout.NORTH);
centerSouthSouthWest.add(handOneDisplay, BorderLayout.SOUTH);
JPanel centerSouthSouthEast = new JPanel();
centerSouthSouthEast.add(handTwoText, BorderLayout.NORTH);
centerSouthSouthEast.add(handTwoDisplay, BorderLayout.SOUTH);
centerSouthSouth.add(centerSouthSouthWest, BorderLayout.WEST);
centerSouthSouth.add(centerSouthSouthEast, BorderLayout.EAST);
centerSouth.add(centerSouthNorth, BorderLayout.NORTH);
centerSouth.add(centerSouthSouth, BorderLayout.SOUTH);
center.add(centerNorth, BorderLayout.NORTH);
center.add(centerSouth, BorderLayout.SOUTH);
frame.add(center, BorderLayout.CENTER);
// JPanel left - the center of the program
JPanel left = new JPanel();
JPanel leftNorth = new JPanel();
JPanel leftNorthNorth = new JPanel();
JPanel leftNorthSouth = new JPanel();
JPanel leftSouth = new JPanel();
JPanel leftSouthNorth = new JPanel();
JPanel leftSouthSouth = new JPanel();
leftNorthNorth.add(standButton, BorderLayout.WEST);
leftNorthNorth.add(hitButton, BorderLayout.EAST);
leftNorthSouth.add(doubleButton, BorderLayout.WEST);
leftNorthSouth.add(splitButton, BorderLayout.EAST);
leftNorth.add(leftNorthNorth, BorderLayout.NORTH);
leftNorth.add(leftNorthSouth, BorderLayout.SOUTH);
leftSouthNorth.add(statsText, BorderLayout.WEST);
leftSouthNorth.add(statsDisplay, BorderLayout.EAST);
leftSouthSouth.add(winButton, BorderLayout.WEST);
leftSouthSouth.add(loseButton, BorderLayout.EAST);
leftSouth.add(leftSouthNorth, BorderLayout.NORTH);
leftSouth.add(leftSouthSouth, BorderLayout.SOUTH);
left.add(leftNorth, BorderLayout.NORTH);
left.add(leftSouth, BorderLayout.SOUTH);
frame.add(left, BorderLayout.WEST);
frame.setSize(1600, 200);
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
The layering for what seems to be the upper layers is not working properly. Any suggestions? The left should have four layers.
A BorderLayout does not "layer".
Only a single component can be added to each of the 5 areas of the BorderLayout.
So, yes, you can create a JPanel and add multiple components to that panel, and then add the panel to an area of the BorderLayout.
JPanel rightSouthSide = new JPanel();
rightSouthSide.add(resetButton, BorderLayout.WEST);
rightSouthSide.add(enterButton, BorderLayout.EAST);
However, the above code is incorrect. The default layout manager of a JPanel is the FlowLayout. So specifying BorderLayout constraints does nothing (and is very confusing).
If by "layers" you mean "rows", then you need to use a panel with a different layout manage. Maybe you can use a BoxLayout to add rows of panels.
Overall, your approach to creating panels with different components is valid, the problem is you also need to use the appropriate layout manager for each of your child panels.
Read the Swing tutorial on Layout Managers for more information about how each of the layout managers work.

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:

Nested tabs in Java

i'm trying to make nested tabs in java . It works but how can i resize my nested tabs ? ( addStudent, addTeacher ) because when it runs, they are very narrow. Thx for help
JPanel students = new JPanel();
JPanel teachers = new JPanel();
JPanel lessons = new JPanel();
JPanel courses = new JPanel();
JPanel addPanel = new JPanel();
JPanel translations = new JPanel();
JPanel addStudent = new JPanel();
JPanel addTeacher = new JPanel();
JButton bAddStudent = new JButton();
//addStudent.setBounds(x, y, width, height);
tp=new JTabbedPane();
tp2 = new JTabbedPane();
Container pane = this.getContentPane();
pane.add(tp);
tp.addTab("Uczniowie",students);
tp.addTab("Nauczyciele",teachers);
tp.addTab("Harmonogram zajec",lessons);
tp.addTab("Kursy",courses);
tp.addTab("Tlumaczenia", translations);
tp.addTab("Panel administratora", addPanel);
tp2.add("Nowy uczen",addStudent);
tp2.add("Nowy nauczyciel",addTeacher);
addPanel.add(tp2);
Change the layout manager of addPanel to a BorderLayout
This will allow tp2 to occupy the entire available space provided by addPanel and tp
I'd also be careful with nested tabs, it becomes very messy and confusing to user very quickly

Java: How do I move a JLabel to the center of a JPanel?

So in my GUI, I have a JFrame that's a borderlayout. There's a menubar, and some GUI stuff in NORTH and WEST. In CENTER, there is one JLabel. I want it to move to the center (both horizontally and vertically) of the JPanel. How do I do that correctly? I tried box layout and grid layout. One requirement is that I cannot use gridbag layout.
public class NewClass extends JFrame{
public NewClass () {
setVisible(true);
setSize(500,500);
setDefaultCloseOperation (EXIT_ON_CLOSE);
//menubar
JMenuBar bar = new JMenuBar();
JMenu editMenu = new JMenu("Edit");
JMenuItem mItem = new JMenuItem("Cut"); // edit->cut
editMenu.add(mItem);
mItem = new JMenuItem("Copy"); // edit->copy
editMenu.add(mItem);
mItem = new JMenuItem("Paste"); // edit->paste
editMenu.add(mItem);
bar.add(editMenu);
this.setJMenuBar(bar);
//north panel
JPanel topPanel = new JPanel();
this.add(topPanel,BorderLayout.NORTH);
JLabel myLabel = new JLabel ("Label:") ;
topPanel.add(myLabel);
JButton mytopButton = new JButton ("Push Me");
topPanel.add(mytopButton);
//left panel
JPanel leftPanel = new JPanel();
leftPanel.setBorder (new TitledBorder("Commands:"));
leftPanel.setLayout (new GridLayout (10,1));
this.add(leftPanel,BorderLayout.WEST);
JButton myLeftButton1 = new JButton ("Button 1");
leftPanel.add(myLeftButton1);
JButton myLeftButton2 = new JButton ("Button 2");
leftPanel.add(myLeftButton2);
JButton myLeftButton3 = new JButton ("Button3");
leftPanel.add(myLeftButton3);
//center panel
JPanel centerPanel = new JPanel();
this.add(centerPanel,BorderLayout.CENTER);
JLabel mapLabel = new JLabel("Test_String"); //move this to center of JPanel
centerPanel.add(mapLabel);
centerPanel.setBorder (new EtchedBorder(Color.black,Color.black));
centerPanel.setBackground (Color.white);
}
}
Check the API for methods that affect the alignment of the component.
There are methods that affect the alignment of the component within the layout manager and others that affect the alignment of the text within the label itself.
Start by taking a look at the JavaDocs for JLabel
Specifically, JLabel#setHorizontalAlignment and JLabel#setVerticalAlignment
Answered. Thanks everyone.
JPanel centerPanel = new JPanel();
centerPanel.setLayout (new GridLayout ()); //added
this.add(centerPanel,BorderLayout.CENTER);
JLabel mapLabel = new JLabel("Test_String");
mapLabel.setHorizontalAlignment(SwingConstants.CENTER); //added
mapLabel.setVerticalAlignment(SwingConstants.CENTER); //added
centerPanel.add(mapLabel);
centerPanel.setBorder (new EtchedBorder(Color.black,Color.black));
centerPanel.setBackground (Color.white);

setAlignmentY not centering JLabel in 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.

Categories

Resources