I want the margin from the left side in Grid layout first column only. The Jlabel in the first column is LEFT aligned.
Code for the Row is
lbl1 = new JLabel("Hold");
lbl1 .setFont(new Font("Arial Black", Font.PLAIN, text));
lbl1 .setHorizontalAlignment(SwingConstants.LEFT);
lbl1 .setForeground(Color.decode(textColor));
panel1.add(lbl1 );
lbl2= new JLabel("100");
lbl2.setFont(new Font("Arial Black", Font.PLAIN, text));
lbl2.setHorizontalAlignment(SwingConstants.CENTER);
lbl2.setForeground(Color.decode(textColor));
panel1.add(lbl2);
Code for the panel is
setLayout(new GridLayout(0,2));
In your case you can set an empty border for all your "left" label.
lb1.setBorder(new EmptyBorder(0, 10, 0, 0));
This code will provide 10 points offset from the left side of the label.
Usually GridLayout is not a best choice for your purposes. I would advise you to look for another layout manager. The standard GridBagLayout is a little bit too verbouse so it would be better to learn a third-party layout like MigLayout or FormLayout.
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.
in the below code, i created a GridLayot with 3 rows and 3 columns, what i want to do is,,to add jpanel_1 into
a specifc cell of the Gridlayout, lets say in the grid cell number (2,3).
Code:
private void setUpGUI2() {
// TODO Auto-generated method stub
jFrame_2 = new JFrame("Border Demo");
GridLayout gridLayOut = new GridLayout(3,3);
jFrame_2.setLayout(gridLayOut);
jPanel_1 = new JPanel(new BorderLayout());
jPanel_2 = new JPanel(new BorderLayout());
jPanel_1.setBorder(BorderFactory.createTitledBorder("title"));
//jPanel_1.setBounds(30, 100, 110, 300);
jPanel_1.add(jLabel_Hello, BorderLayout.EAST);
jPanel_2.setBorder(BorderFactory.createLoweredBevelBorder());
//jPanel_2.setBounds(20, 50, 120, 80);
jPanel_2.add(jLabel_Hello, BorderLayout.SOUTH);
//jFrame_2.setBounds(0, 0, 600, 600);
jFrame_2.add(jPanel_1);//how to add jpanel_1 to a specific cell of Gridlayout defined above
//jPanel_1.add(jPanel_2);
jFrame_2.add(jPanel_2);
jFrame_2.pack();
jFrame_2.setVisible(true);
}
I think there is no chance. You need to add them one by one.
frame.add(...); frame.add(...);
I don't clearly understand what you want as result, but using GridLayout(3, 3) with only 2 panels is the same as use GridLayout(0, 2).
P.S. Check out GridBagLayout - it can be more useful for you.
Depending on what you want to do, you could add "dummy" components (like a JLabel) to the cells you don't want to use. If I recall correctly, GridLayout will layout in col/row order
So of you wanted to add a component to the second column on the second row, you would have to add four JLabel's first, do pad out the layout
I am trying to make a 3 column layout, in each column i want to be able to absolute position labels and textboxes.
Problem is that my label (jLabel2) never even gets displayed..
Here is my code:
/**
* Top JPanel (Top)
*/
JPanel pnlTop = new JPanel();
pnlTop.setBackground(new java.awt.Color(223, 223, 217));
pnlTop.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new java.awt.Color(173, 173, 166)));
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 5; // five rows
c.gridheight = 1; // one column
c.fill = GridBagConstraints.BOTH;
//c.weighty = 0.04;
add(pnlTop, c);
/**
* Top JPanel Content (Here is where i want to put absolute content)
*/
JPanel pnlTopContent = new JPanel();
pnlTopContent.setLayout(null);
jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 16)); // NOI18N
jLabel2.setText("Hello");
jLabel2.setLocation(150, 50);
pnlTopContent.add(jLabel2);
pnlTop.add(pnlTopContent);
Any ideas what i am doing wrong?
Then its showing but not in the right place
What does "right place" mean to you? Why are you even adding you label to a second panel? Why not just add the label directly to the pnlTopContent?
GridBagLayout has a constraint that allows you to position the component right/left/center of the column. Read the section from the Swing tutorial on How to Use GridBagLayout. You might want to start with the anchor constraint.
Use layout manager for pnlTopContent. Which one is right depends on what you want. Even the default FlowLayout might work. If you want to center the label, you can for example use FlowLayout with center alignment:
pnlTopContent.setLayout(new FlowLayout(FlowLayout.CENTER));
So I have the following screen:
And this is my code:
setLayout(new BorderLayout());
JLabel lblTitulo = new JLabel("Sistema Generador de Examenes");
lblTitulo.setFont(new Font("Tahoma", Font.BOLD, 18));
JPanel panel1 = new JPanel();
panel1.setBackground(Color.white);
panel1.add(lblTitulo);
add(panel1, BorderLayout.NORTH);
JButton btnCrear = new JButton("Crear Examen");
JButton btnRendir = new JButton("Rendir Examen");
JButton btnCorregir = new JButton("Corregir Examen");
JButton btnVerCorrecciones = new JButton("Ver Correcciones");
btnCrear.setBounds(15, 100, 450, 35);
btnRendir.setBounds(15, 150, 450, 35);
btnCorregir.setBounds(15, 200, 450, 35);
btnVerCorrecciones.setBounds(15, 250, 450, 35);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.setLayout(null);
panel2.add(btnCrear);
panel2.add(btnRendir);
panel2.add(btnCorregir);
panel2.add(btnVerCorrecciones);
add(panel2, BorderLayout.CENTER);
1 - I'm using the BorderLayout. Do I need to have 2 JPanels to separate components (JLabel and JButtons) if I want to have the JLabel in the North and the JButtons in the Center? Or is there any way to use just one JPanel?
2 - I want to take out the setBounds used in my JButtons and use some Layout in order to have my JButtons like that in the middle of the screen. How could I do that?
I'm using the BorderLayout. Do I need to have 2 JPanels to separate components (JLabel and JButtons) if I want to have the JLabel in the North and the JButtons in the Center? Or is there any way to use just one JPanel?
Yes, you could use one JPanel and a GridBagLayout with a single column and some Insets to space the buttons from the label.
However, the nested layouts will keep the buttons in the center no matter how you resize the JFrame.
I want to take out the setBounds used in my JButtons and use some Layout in order to have my JButtons like that in the middle of the screen. How could I do that?
The GridBagLayout will space out the buttons with insets.
See this article, Sudoku Solver Swing GUI, for a couple of examples of dialogs that use the GridbagLayout.
I have a Swing UI which contains 6 text fields and labels for the input and 1 button and texfield to show the output. now I want to make a border around these two.
I have read some materials regarding Titled borders but I think its only for single elements. Please suggest.
You could make a JPanel with a titled border, then put however many components you wanted in the JPanel using the content manager of your choice.
An example:
JPanel myPanel = new JPanel();
myPanel.setBorder(new TitledBorder(null, "My Title", TitledBorder.LEADING, TitledBorder.TOP, null, null));
myPanel.setLayout(new GridLayout(1, 0, 0, 0));
JButton button = new JButton("New button");
myPanel.add(button);
JLabel label = new JLabel("New label");
myPanel.add(label);
You can add that last 2 components to a JPanel and then add that panel to main frame. Now you can give border to JPanel and it will around 2 components inside it.
To give border to jPanel you can use following:
JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createLineBorder(Color.black));
If you want titled border then you can use following:
pane.setBorder(BorderFactory.createTitledBorder(BorderFactory
.createMatteBorder(5, 5, 5, 5, Color.blue), "Title",
TitledBorder.LEFT, TitledBorder.TOP));
Reference: http://download.oracle.com/javase/tutorial/uiswing/components/border.html