Adding a label to the center south of a panel? - java

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:

Related

Create tab header of JTabbedPane manually with JPanel and JLabel

I want to create a JTabbedPane, but I want to create the headers manually, I created a JPanel and JLabel where I put my Icon and background color, but I don't know how to add it to the tabbed pane.
The tabbed pane has 4 panels:
JPanel header = new JPanel();
header.setSize(100, 50);
header.setBackground(Color.red);
JLabel icon_Label = new JLabel(Icon);
icon_Label.setText("header 1");
jTabbedPane1.getComponentAt(1).*************?!
i find it
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
JLabel title = new JLabel("OK");
title.setForeground(Color.RED);
title.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/icons8-boîte-pleine-64.png")));
title.setText("GESTION A");
panel.add(title);
jTabbedPane1.setTabComponentAt(2, panel);
but there is space in the left and right of the header !

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

How do I get these two buttons on the bottom of my program

Write a program that displays two buttons labeled “Green” and “Orange”.
If the user clicks on the green button, the background of the window changes to green. If the user clicks on the orange button, the background of the window changes to Orange.
Create a JFrame for this GUI. The GUI employs the default layout manager. A JPanel is needed.
Place the two buttons inside the panel and add the panel to the south region of the border layout.
Notice the text in the title bar. The green button should have white text and a green background. The orange button should have black text with an orange background.
Below is what I have so far, it doesn't seem to work.
public class LabAssign91 extends JFrame implements ActionListener{
private JPanel loc1Panel;
private JButton greenButton, orangeButton;
public LabAssign91()
{
super("Colored Buttons");
setLayout(new GridLayout(2, 2));
setSize(300,250);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(loc1Panel);
loc1Panel = new JPanel();
add(loc1Panel, BorderLayout.SOUTH);
greenButton = new JButton("Green");
greenButton.addActionListener(this);
loc1Panel.add(greenButton, BorderLayout.WEST);
greenButton.setBackground(Color.green);;
orangeButton = new JButton("Orange");
orangeButton.addActionListener(this);
loc1Panel.add(orangeButton, BorderLayout.EAST);
orangeButton.setBackground(Color.orange);
}
public static void main(String[] args) {
LabAssign91 app = new LabAssign91();
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
I have used BorderLayout for the JFrame and FlowLayout for the ButtonPanel. ButtonPanel is the bottom panel of the frame.
frame = new JFrame();
frame.setLayout(new BorderLayout());
topPanel = new JPanel();
topPanel.add(new JLabel("Top Panel"));
middlepanel = new JPanel();
middlepanel.add(new JLabel("Middle Panel"));
bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(new JButton("Orange"));
bottomPanel.add(new JButton("Green"));
frame.add(topPanel, BorderLayout.NORTH);
frame.add(middlepanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
The default layout for a JFrame is BorderLayout which has a SOUTH constraint. So there is no need for this statement.
//setLayout(new GridLayout(2, 2));
The default layout for a JPanel is a FlowLayout. So the following statements do nothing:
loc1Panel.add(greenButton, BorderLayout.WEST);
loc1Panel.add(orangeButton, BorderLayout.EAST);
Read the section from the Swing tutorial on Using Layout Managers. There is a section on using a BorderLayout and on using a FlowLayout. I don't know if you are supposed to use just panels with a BorderLayout or panels with a combination of BorderLayout and FlowLayout. I'll let you fix the code to meet your requirement.

Jpanel size after adding jlabel component

I have a central panel. This is my parent panel. I am adding 3 panels to the parent panel.
The panels are going to be stacked vertically. Like a title panel, then a middle panel, then a bottom panel. I just want to focus on my title panel. When I create a jlabel using text. The label shows and the panel borders stretches the entire width of the parent panel, which is what I want.
private JPanel titlePanel() {
String text = "<html><b><big><font color=#5C8C5C>Help Dialog</font></big></b></html>";
JLabel textLabel = new JLabel(text, JLabel.CENTER);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
p.add(textLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
I am actually wanting to use a icon as the label and not html text. So make the changes to the code.
private JPanel titlePanel() {
Registry appReg = Registry.getRegistry(this);
ImageIcon ediLabelIcon = appReg.getImageIcon("ToolLabel.ICON");
JLabel textLabel = new JLabel(ediLabelIcon, JLabel.CENTER);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
p.add(textLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
Now the label shows, but the border of the panel is only as wide as the label and not stretched out the width of the parent panel.
I am trying to figure out to extend the panel border the width of the parent panel and not just as wide as the label. This is the code for the parent panel.
private void createDialog() {
Component titlePanel = titlePanel();
Component verbiagePanel = verbiagePanel();
Component closeButtonPanel = closeButton();
setTitle("HELP Dialog");
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
centerPanel.setPreferredSize(new Dimension(600, 300));
centerPanel.add(titlePanel);
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(verbiagePanel);
centerPanel.add(Box.createHorizontalGlue());
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(closeButtonPanel);
getContentPane().add(centerPanel);
this.pack();
}
Using HTML in JLabel text switched the mechanism which calculate preferred size fot JLabel.
Now I can't explain it in detail, but if you change creating title label to
JLabel textLabel = new JLabel("<html></html>", ediLabelIcon, JLabel.CENTER);
your label will be stretched out to parent panel width.
Or you may choose another layout manager such as GridBagLayout. With GridBagLayout you can force stretch any component to its parent width.

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