how to have JPanel both horizontally and vertically in Java Swing - java

i am trying to make screen as in the picture using swing.
My code snipet
Label l1,l2,l3;
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
l1=new Label("Employee:);
l2=new Label("Earning");
l3=new Label("Deductions");
p1.setLayout(new GridLayout(1,1));
p1.add(l1);
p2.setLayout(new GridLayout(1,1));
p2.add(l2);
p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK)));
p2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK)));
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.add(p1);
container.add(p2);
container.add(p3);
add(container,BorderLayout.NORTH);
setVisible(true);
setSize(600,600);
with Y_AXIS it doesn't allow me to have 2 panel side by side.
how to have panels both horizontally and vertically

Related

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:

Using BoxLayout in JFrame

I am trying to use JFrame and BoxLayout to achieve a GUI similar to the one shown, but I am not sure how to center my Stop and Play buttons. Any suggestions?
Here is my code:
JFrame frame = new JFrame();
Box box = Box.createHorizontalBox();
box = Box.createHorizontalBox();
box.add(new JButton("Play"));
box.add(new JButton("Stop"));
box.add(Box.createHorizontalGlue());
frame.add(box, BorderLayout.SOUTH);
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
I have not yet coded in the text box and load button as I haven't yet been able to figure out centering.
Create a seperate panel for buttons. With horizontal glue you can center your buttons.
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
buttonPanel.add(Box.createHorizontalGlue());
buttonPanel.add(new JButton("Play"));
buttonPanel.add(new JButton("Stop"));
buttonPanel.add(Box.createHorizontalGlue());
frame.add(buttonPanel, BorderLayout.SOUTH);
Also you can do that with FlowLayout easily
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
buttonPanel.add(new JButton("Play"));
buttonPanel.add(new JButton("Stop"));
frame.add(buttonPanel, BorderLayout.SOUTH);

java.awt.AWTERROR When Creating a BoxLayout

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

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

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