GridLayout the wrong number of columns

I am trying to create a panel using GridLayout (7.2)

membersPanel = new JPanel(new GridLayout(7,2)); 

However, when I add components (shortcuts, Combobox, text fields, etc.), the components are displayed in three columns, for example:

Image

I tried changing the number of columns to 1 or even 0, but the panel remains unchanged. What can I do?

EDIT:

Here is more code:

  p1 = new JPanel(); membersPanel = new JPanel(new GridLayout(7,0)); resourcesLabel = new JLabel("Resources"); membersPanel.add(resourcesLabel); emptyLabel5 = new JLabel(""); membersPanel.add(emptyLabel5); emptyLabel6 = new JLabel(""); membersPanel.add(emptyLabel6); comboBoxResource = new JComboBox(configs.XMLreaderDOM4J.readResourceID()); membersPanel.add(comboBoxResource); slider1 = new SliderWithTextField(1,10); textSli1 = new TextFieldFromSlider(this, slider1); slider1.setTextField(textSli1); slider1.setValue(1); membersPanel.add(slider1); membersPanel.add(textSli1); emptyLabel2 = new JLabel(); membersPanel.add(emptyLabel2); addButton1 = new JButton("Add"); addButton1.addActionListener(new TrataEvento()); membersPanel.add(addButton1); agregator1Label = new JLabel("Agretagor1"); membersPanel.add(agregator1Label); comboBoxAgregator1 = new JComboBox(configs.XMLreaderDOM4J.readAgregator1ID()); membersPanel.add(comboBoxAgregator1); slider2 = new SliderWithTextField(1,10); textSli2 = new TextFieldFromSlider(this, slider2); slider2.setTextField(textSli2); slider2.setValue(1); membersPanel.add(slider2); membersPanel.add(textSli2); addButton2 = new JButton("Add"); addButton2.addActionListener(new TrataEvento()); membersPanel.add(addButton2); emptyLabel3 = new JLabel(); membersPanel.add(emptyLabel3); agregator0Label = new JLabel("Agregator0"); membersPanel.add(agregator0Label); comboBoxAgregator0 = new JComboBox(configs.XMLreaderDOM4J.readAgregator0ID()); membersPanel.add(comboBoxAgregator0); slider3 = new SliderWithTextField(1,10); textSli3 = new TextFieldFromSlider(this, slider3); slider3.setTextField(textSli3); slider3.setValue(1); membersPanel.add(slider3); membersPanel.add(textSli3); addButton3 = new JButton("Add"); addButton3.addActionListener(new TrataEvento()); membersPanel.add(addButton3); emptyLabel4 = new JLabel(); membersPanel.add(emptyLabel4); p1.add(membersPanel); 
+4
source share
2 answers

Swing adjusts the number of columns used for the GridLayout if the number of added components exceeds the specified source number. Use 0 to specify a custom number of rows:

 membersPanel = new JPanel(new GridLayout(0, 2)); 

This will facilitate future re-factoring.

+5
source
  • the number of JComponent layerd from GridLayout (7.2) does not match the JComponents in one container,

  • there should be more than 21 JComponents in one container

output from super(new GridLayout(8, 8));

enter image description here

output from super(new GridLayout(N, N));

enter image description here

from code

 import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.EventHandler; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; /** based on @see http://stackoverflow.com/questions/7702697 */ public class GridButtonPanel extends JPanel { private static final int N = 4; private final List<GridButton> list = new ArrayList<GridButton>(); public GridButtonPanel() { //super(new GridLayout(8, 8)); super(new GridLayout(N, N)); for (int i = 0; i < N * N; i++) { int row = i / N; int col = i % N; GridButton gb = new GridButton(row, col); gb.addActionListener((ActionListener) EventHandler.create(ActionListener.class, this, "actionName" + row + "A" + col)); list.add(gb); this.add(gb); } } public void actionName0A0() { System.out.println(" Grid at Row 0, Column 0 "); } public void actionName0A1() { System.out.println(" Grid at Row 0, Column 1 "); } public void actionName1A0() { System.out.println(" Grid at Row 1, Column 0 "); } public void actionName1A1() { System.out.println(" Grid at Row 1, Column 1 "); } private GridButton getGridButton(int r, int c) { int index = r * N + c; return list.get(index); } private class GridButton extends JButton { private int row; private int col; public GridButton(int row, int col) { super("Row - " + row + ", Col - " + col); this.row = row; this.col = col; this.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int r = GridButton.this.row; int c = GridButton.this.col; GridButton gb = GridButtonPanel.this.getGridButton(r, c); System.out.println("r" + r + ",c" + c + " " + (GridButton.this == gb) + " " + (GridButton.this.equals(gb))); } }); } } private void display() { JFrame f = new JFrame("GridButton"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new GridButtonPanel().display(); } }); } } 
+5
source

Source: https://habr.com/ru/post/1487346/


All Articles