I've tried to center some components over several panels with the MigLayout (see the example here).
It suddenly worked, when I split the page into percentages that are the same in all panels.
BUT I had to use the parameter "0:0" in front of each grow and I dont understand what is happening exactly.
I went to the MigLayout CheatSheet-Page and the QuickStart-guide but only could find parameters with values > 0
The format is "min:preferred:max", however there are shorter versions since for instance it is seldom needed to specify the maximum size.
A single value (E.g. "10") sets only the preferred size and is exactly the same as "null:10:null" and ":10:" and "n:10:n".
Two values (E.g. "10:20") means minimum and preferred size and is exactly the same as "10:20:null" and "10:20:" and "10:20:n"
But it does not explain what happens, when you use the 0 as a parameter, or why you need to use X:X or X:Yat all and not for example X:Y:Z or X:X:X?
So why do I need to use X:X here to center my components?
The bit that matters here is the preferred size. In your other question, you said you used the following code to get it to work.
locationPanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
usagePanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
This would have also worked if you had wrote
locationPanel.setLayout(new MigLayout("gap rel 2", "[:0:, grow, center][grow, left]"));
usagePanel.setLayout(new MigLayout("gap rel 2", "[:0:, grow, center][grow, left]"));
structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[:0:, grow, center][grow, left]"));
//Obviously you could use any number not just 0
Options you could use are (Where X <= Y <= Z)
X:Y:Z
X:Y:
:Y:Z
:Y:
:Y
So why does it do this?
It does this because the columns grow at the same rate and when you set the preferred size of the first column in all three panels you are setting the initial size as MigLayout sets the initial column size to the preferred size.
In essensce you set columns to the same size and they grow at the same rate, so they remain equal sizes to each other. Hence they stay in line.
Working Example
Uses :Y where Y is determined by Font Metrics
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
public class MigLay extends JFrame {
private MigLay() {
super("Button Layout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new MigLayout("wrap", //Layout Constraints
"grow, fill", //Column Constraints
"grow, fill")); //Row Constraints
createPanels();
pack();
setMinimumSize(getSize()); //Sets minimum size to the preferred size. Remove or change this line if you do not want that to happen
setVisible(true);
}
private void createPanels() {
JPanel locationPanel = new JPanel();
JPanel usagePanel = new JPanel();
JPanel structuralAspectsPanel = new JPanel();
//JLabels for font metrics
JLabel one = new JLabel("This is the first of all labels");
JLabel two = new JLabel("Second Label");
JLabel three = new JLabel("This Label is fairly large and long and pushes the text around");
JLabel four = new JLabel("A label in the usage panel");
JLabel five = new JLabel("And another one and another one and another one");
//Font Metrics
FontMetrics metrics = three.getFontMetrics(three.getFont()); //Take longest label manually or dynamically (You will have to add that code)
int width = metrics.stringWidth(three.getText());
locationPanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]"));
locationPanel.setBorder(BorderFactory.createTitledBorder("Location"));
usagePanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]"));
usagePanel.setBorder(BorderFactory.createTitledBorder("Usage"));
locationPanel.add(one);
locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JSeparator(), "growx, span");
locationPanel.add(two);
locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JSeparator(), "growx, span");
locationPanel.add(three);
locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(four);
usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JSeparator(), "growx, span");
usagePanel.add(five);
usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
getContentPane().add(locationPanel);
getContentPane().add(usagePanel);
}
public static void main(String[] args) {
new MigLay();
}
}
I'm newbie in Java programming and can't deal with my Swing problem. I can't get JButtons and JLabels Centred in JPanels(BoxLayout).
Here are some photos :
Code:
PlayerLabel.setHorizontalTextPosition(JLabel.CENTER);
PlayerLabel.setVerticalTextPosition(JLabel.BOTTOM);
PlayerLabel.setText("Player Level: " + CarMain.main[5]);
AccessoriesLVL1Label.setHorizontalTextPosition(JLabel.CENTER);
AccessoriesLVL1Label.setVerticalTextPosition(JLabel.BOTTOM);
AccessoriesLVL1Label.setText("<html>Accessories LVL 1<br>" + "Count: " + Part.parts[1]);
JButton jbtnSellAccessoriesLv1 = new JButton("Sell");
jbtnSellAccessoriesLv1.addActionListener(this);
And this is where i make Jlabel :
//Upgrades Panel
GridLayout UpgradesLayout = new GridLayout(3,3);
JPanel UpgradesPanel = new JPanel();
UpgradesPanel.setLayout(UpgradesLayout);
JPanel UpgradesPanelSub = new JPanel();
UpgradesPanelSub.setLayout(new BoxLayout(UpgradesPanelSub, BoxLayout.PAGE_AXIS));
UpgradesPanelSub.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));
JPanel UpgradesPanelSub2 = new JPanel();
UpgradesPanelSub2.setLayout(new BoxLayout(UpgradesPanelSub2, BoxLayout.PAGE_AXIS));
UpgradesPanelSub2.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));
JPanel UpgradesPanelSub3 = new JPanel();
UpgradesPanelSub3.setLayout(new BoxLayout(UpgradesPanelSub3, BoxLayout.PAGE_AXIS));
UpgradesPanelSub3.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));
UpgradesPanelSub.add(Labels.PlayerLabel);
UpgradesPanelSub.add(jbtnUpgradeLevel);
UpgradesPanelSub2.add(Labels.GarageLabel);
UpgradesPanelSub2.add(jbtnUpgradeGarageLevel);
UpgradesPanelSub3.add(Labels.BoxesLVLLabel);
UpgradesPanelSub3.add(jbtnUpgradeBoxesLevel);
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(UpgradesPanelSub);
UpgradesPanel.add(UpgradesPanelSub2);
UpgradesPanel.add(UpgradesPanelSub3);
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
UpgradesPanel.add(new JLabel(""));
As for the layout problem: there's a tutorial on how to use box layout (look for the components' alignment to center them): docs.oracle.com/javase/tutorial/uiswing/layout/box.html.
Alternatively you could use one of the external layout managers like MigLayout which are easier to use IMO.
I want to display a Multiline JLabel into JPanel.
So I have this code but I'm not able to show the multiline JLabel.
public class NotificationFrame extends JFrame{
public NotificationFrame(){
JPanel panelBody = new JPanel();
panelBody.setBackground(Color.white);
GridBagConstraints GBC2 = new GridBagConstraints();
Container CR2 = new Container();
GridBagLayout GBL2 = new GridBagLayout();
CR2.setLayout(GBL2);
panelBody.add(CR2);
GBC2 = new GridBagConstraints();
CR2.add(labelTesto);
GBC2.gridx=0;
GBC2.gridy=0;
GBC2.insets.left = 10;
GBC2.insets.top=0;
GBL2.setConstraints(labelTesto,GBC2);
panelBody.setLayout(new FlowLayout(FlowLayout.CENTER));
add(panelBody,BorderLayout.CENTER);
}
}
If I change the last line of code in
add(labelTest,BorderLayout.CENTER);
I can show that I want. But it is not correct because I want to set a padding on JLabel
EDIT
I have use this code now:
JPanel panelBody = new JPanel();
panelBody.setBackground(Color.white);
SpringLayout layout = new SpringLayout();
panelBody.setLayout(layout);
panelBody.add(labelTesto);
layout.putConstraint(SpringLayout.NORTH, labelTesto, 15, SpringLayout.NORTH, panelBody);
add(panelBody,BorderLayout.CENTER);
This is the layout:
This is the all test that I should see:
"Il 31 Dicembre scadrĂ l'assistenza, ricorda di rinnovare l'assistenza per ricevere sempre assistenza ed aggiornamenti."
Multiline it's not padding. For achieve multiline text in JLabel or JButton you need to use HTML.
To wrap text (or multiline):
btn.setText("<html><p>Il 31 Dicembre scadrĂ l'assistenza, ricorda di rinnovare l'assistenza per ricevere sempre assistenza ed aggiornamenti</p></html>");
label.setText("<html>Date:<br></html>"+getDateFromSomeWhere);
To set padding:
label.setBorder(new EmptyBorder(0,10,0,0));//top,left,bottom,right
I can't figure out why this doesn't work... It hides the JTextArea.
JTextArea _area=new JTextArea();
_area.setBounds(10, 10, 465, 250);
add(_area);
JScrollPane scroller=new JScrollPane(_area);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroller);
EDIT:
Shuffle the bound values set the bounds only of ScrollPane and not of TextArea.
JTextArea _area = new JTextArea();
// _area.setBounds(10, 10, 400, 250);
add(_area);
JScrollPane scroller = new JScrollPane(_area);
scroller.setBounds(10, 10, 400, 250);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroller);
I want 3 components laid out on 2 lines, so that the bottom component and the top-right component use all available horizontal space.
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLayout(new MigLayout("debug, fill"));
Container cp = frame.getContentPane();
cp.add(new JTextField("component 1"), "");
cp.add(new JTextField("component 2"), "growx,push,wrap");
cp.add(new JTextField("component 3"), "span,growx,push");
frame.pack();
frame.setVisible(true);
Considering the above, how do i stop the space between "component 1" and "component 2" from appearing when resizing the frame?
cp.add(new JTextField("component 1"), "");
cp.add(new JTextField("component 2"), "growx,push,wrap");
cp.add(new JTextField("component 3"), "span,growx,pushy");
solves this case.