I'm trying to center labels using MigLayout and I'm having a difficult time. Here's what I'd like the labels to look like:
| [label1] [label2] [label3] [label4] [label5] |
| ............ [label6][label7][label8] ............| |
I'd like to:
Limit the number of labels to five per row and I think I can use wrap 5.
Center the labels inside the JPanels
I have labeled sections on the main JPanel with subpanels, like this:
Section 1
[subpanel with labels]
Section 2
[subpanel with labels]
There's no problem with centering if there's only a single label because I do this:
myPanel.add(label, "align center, wrap");
Unfortunately, if I add more than one they just don't center. I've gone through the MigLayout Cheat Sheet but it's like going through an API without fully understanding how it all works and I haven't been able to find any good tutorials out there. I'd like to become proficient with MigLayout, so a good tutorial would probably do the trick.
I'd be grateful for your help.
I am not exactly sure what you want but this is my guess:
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel.add(label5, "wrap");
panel.add(label6, "al 100%");
panel.add(label7, "align center");
panel.add(label8);
frame.pack();
you can make miglayout with center column constraint and you can add it by cell no.
JPanel panel = new JPanel(new MigLayout("","[center][center][center][center][center]","[]"));
JLabel label1 = new JLabel("label1");
JLabel label2 = new JLabel("label2");
JLabel label3 = new JLabel("label3");
JLabel label4 = new JLabel("label4");
JLabel label5 = new JLabel("label5");
panel.add(label1, "cell 0 0");
panel.add(label2, "cell 1 0");
panel.add(label3, "cell 2 0");
panel.add(label4, "cell 3 0");
panel.add(label5, "cell 4 0");
f.getContentPane().add(panel);
Related
I am attempting to design a panel with MiGFormat that has a label at the top, and two buttons at the bottom - a yes/no prompt.
I achieve this closely, but the label yesOrNoText (text is "TEST") is not fully centered:
I initialize the panel containing this prompt like so:
private JPanel createYesNoPrompt() {
JPanel panel = new JPanel(new MigLayout());
panel.setBorder(BorderFactory.createLineBorder(Color.red));
JButton yesButton = new JButton("Yes");
JButton noButton = new JButton("No");
yesOrNoText = new JLabel();
yesOrNoText.setText("TEST");
yesOrNoText.setFont(panel.getFont().deriveFont(Font.BOLD, 30f));
yesOrNoText.setHorizontalAlignment(SwingConstants.CENTER);
Dimension dimension = new Dimension(500, 125);
Font font = panel.getFont().deriveFont(Font.BOLD, 20f);
yesButton.setFont(font);
yesButton.setBackground(new Color(35, 138, 35));
yesButton.setPreferredSize(dimension);
noButton.setFont(font);
noButton.setBackground(new Color(183, 19, 19));
noButton.setPreferredSize(dimension);
yesButton.addActionListener(e -> isYes = true);
noButton.addActionListener(e -> isYes = false);
panel.add(yesOrNoText, "wrap, dock center");
panel.add(yesButton);
panel.add(noButton);
return panel;
}
Then, I add it to gamePanel, then gamePanel to mainPanel, then mainPanel to the frame.
gamePanel.add(YesOrNoPanel, "align center");
mainPanel.add(gamePanel);
add(mainPanel);
I'm unsure of what would be causing yesOrNoText to not become fully centered within the YesNoPanel. Please let me know if I need to clarify anything!
Thank you.
I needed to make the add call for the yesNo label span 2 cells. By adding one component in the first row, then adding two in the next, I essentially created a 2x2 grid.
panel.add(yesOrNoText, "wrap, align center, span 2 1");
panel.add(yesButton);
panel.add(noButton);
Notice that on the first component I add yesOrNoText I use span to tell MiGFormat to take up two cells for this component. I can then center that with the remaining two components because it becomes the only component in the row.
MigLayout supports adding multiple components to a dock. I want to add multiple components to the west dock, from top to bottom. However, it seems as if MigLayout can only manage a horizontal layout inside a dock. I tried many parameters (e.g., wrap, growy, flowy) without success.
So, is there any possibility to wrap or set a vertical flow inside a dock? Or is this not possible with MigLayout itself, but only by using an extra sidepanel-component?
Here an example of the unwanted horizontal layout inside the west dock:
How to get the red, green, blue components below each other? Here is the code:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigTest extends JFrame {
MigTest() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(800, 600);
setLayout(new MigLayout("fill"));
JTextField dockW1 = new JTextField("West 1"); dockW1.setBackground(Color.red);
JTextField dockW2 = new JTextField("West 2"); dockW2.setBackground(Color.green);
JTextField dockW3 = new JTextField("West 3"); dockW3.setBackground(Color.blue);
JTextField center = new JTextField("Center"); center.setBackground(Color.lightGray);
add(center, "grow");
// HOW TO LAYOUT THESE COMPONENTS VERTICALLY INSIDE WEST DOCK ?
add(dockW1, "dock west, wrap, growy, flowy");
add(dockW2, "dock west, wrap, growy, flowy");
add(dockW3, "dock west, wrap, growy, flowy");
setVisible(true);
}
public static void main(String[] args) {
new MigTest();
}
}
[edit]: Note that I do not want to put dockW1, dockW2,dockW3, and center into a single grid, since I plan to apply a complex layout in the center area, independently of the side-area, which is the reason why the docking feature was invented :)
My first suggestion is to change the constructor of the MigLayout to
new MigLayout("fill","[][grow]","[][][]")
Then change your add statement to :
add(center, "cell 1 0 1 3, grow");
add(dockW1, "cell 0 0");
add(dockW2, "cell 0 1");
add(dockW3, "cell 0 2");
Edit
After you edited the question, I would suggest you to create a new JPanel object say dockWest and add the components dockW1, dockW2 and dockW3 to dockWest and finally dock the dockWest to the west of the current JFrame like:
JPanel dockWest = new JPanel();
dockWest.setLayout(new MigLayout("fill", "[]", "[grow][grow][grow]");
dockWest.add(dockW1, "cell 0 0");
dockWest.add(dockW2, "cell 0 1");
dockWest.add(dockW3, "cell 0 2");
add(dockWest, "dock west, growy");
IMHO the side-panel is easier option with the same result.
You can also try using cell coordinates as written on page 2 in Quick guide.
I searched a lot on google on this subject, but just can't come out with right solution. I tried "painting" with Graphics paintComponent and everything seems fine, but lines just doesn't appear on my JPanel.
Part of my code with JLabels created:
frame = new JFrame();
frame.setTitle("New family tree");
...
JPanel panel = new JPanel();
panel.setBackground(new Color(30, 144, 255));
frame.getContentPane().add(panel, BorderLayout.EAST);
panel.setLayout(new MigLayout("", "[]", "[][][][][][][][]"));
JButton newPersonButton = new JButton("New Person");
panel.add(newPersonButton, "cell 0 5");
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
scrollPane = new JScrollPane();
tabbedPane.addTab("Tree", null, scrollPane, null);
panel_1 = new JPanel();
scrollPane.setViewportView(panel_1);
panel_1.setLayout(new MigLayout("",
"[][][][][][][][][][][][][][][][][]",
"[][][][][][][][][][][][][][][][][][][][][]"));
final JLabel lblAddGreatgrandmother = new JLabel("Add Great-grandmother");
panel_1.add(lblAddGreatgrandmother, "cell 3 4,growx");
final JLabel lblAddGrandmother_1 = new JLabel("Add Grandmother");
panel_1.add(lblAddGrandmother_1, "cell 2 5");
Should I use painting? Or put JLabels in array list and use Point? I'll appriciate any help.
EDIT: Runnable example - http://pastebin.com/NFug1QA1
The problem with the java-sl.com/connector solution is that the connection itself becomes a component- ie needs layout, accepts events etc. I put together a solution for this years ago, which you can find in my sourceforge project. Specifically, see ConnectionPanel and Connection.
... Also, just in my experience, they kinds of connections are most useable when the layout manager is set to null and components are not restricted to being locked into position. But you will know what's best for your case.
This is my code:
frame2 = new JFrame("Confirmation");
frame2.setLayout(new BorderLayout());
JRadioButton y,n,c;
panel = new JPanel();
ButtonGroup buttonGroup = new ButtonGroup();
y = new JRadioButton("Add");
buttonGroup.add(y);
panel.add(y);
n = new JRadioButton("Update");
buttonGroup.add(n);
panel.add(n);
c = new JRadioButton("Delete");
buttonGroup.add(c);
panel.add(c);
y.setSelected(true);
b1=new JButton();
b1.setBounds(300,100,2,2);
b1.setIcon(new ImageIcon(searchresult.class.getResource("/images/yes.png")));
b2=new JButton();
b2.setBounds(100,10,2,2);
b2.setIcon(new ImageIcon(searchresult.class.getResource("/images/no.png")));
panel.add(b1);
panel.add(b2);
frame2.add(panel);
frame2.setSize(182,150);
frame2.setVisible(true);
Right now this gives me the following output
whereas I want this
with an increased width but I am not able to do it..Could anyone provide me with further details that could help me
JPanel uses a FlowLayout by default, which, as the name suggests, layouts out components one after the after, in a flow...
Two choices. Use a compound layout, using BorderLayout as the base, create JPanel that uses a GridLayout for the radio buttons (using 0 rows and 1 column), add this to the CENTER position of the base panel.
Create a second JPanel using a FlowLayout and your buttons to it. Add this to the SOUTH position of the base pane.
Second choice is to use a GridBagLayout
Take a look at Laying out Components within a Container for more details
I have a JPanel (extended by my GeneralOptions class) implemented as:
public GeneralOptions() {
setLayout(new MigLayout("", "[grow]", "[][][][]"));
JLabel lblWyzywienie = new JLabel("Food");
add(lblWyzywienie, "cell 0 0");
JCheckBox chckbxHb = new JCheckBox("HB");
add(chckbxHb, "cell 0 1");
JCheckBox chckbxBb = new JCheckBox("BB");
add(chckbxBb, "cell 0 1,alignx trailing");
JCheckBox chckbxAll = new JCheckBox("All Inclusive");
add(chckbxAll, "cell 0 1,alignx trailing");
}
As you can see, there is a list of checkboxes in one cell of MigLayout. This JPanel in placed as left panel of SplitPanel component, so its width is resizable.
What I want to achieve is to force this list of checkboxes to act like "inline" html list of checkboxes. This means, that they should break line when width of panel is not enough to show them in single line.
Now I can't resize this panel below width of whole list and if init width is less than this list of checkboxes, some of them are just hidden.
Example html code
http://jsfiddle.net/
You can try to resize right panel to see what I'm talking about.
Take a look on the following discussion: http://migcalendar.com/forums/viewtopic.php?f=8&t=2393
Scroll down to see the code. He actually implemented his own layout manager that does exactly what you want.