GroupLayout: vertical and horizontal groups

I am trying to create a small Jpanel using GroupLayout. Following the documentation as much as possible, as well as reviewing a few StackOverflow issues, I'm still stuck.

The error is as follows:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JButton [, 0,0,0x0, invalid, alignmentX = 0,0, alignmentY = 0,5, border = com.apple.laf .AquaButtonBorder $ Dynamic @ 5eef2e7c, flags = 288, MaximumSize =, MinimumSize =, PreferredSize =, DefaultIcon =, = disabledIcon, disabledSelectedIcon =, profitability = javax.swing.plaf.InsetsUIResource [top = 0, left = 2, = , right = 2], paintBorder = true, paintFocus = true, pressedIcon =, rolloverEnabled = false, rolloverIcon =, rolloverSelectedIcon =, selectedIcon =, text = Invest, defaultCapable = true] not attached to the vertical group

I know the problem is where the buttons are attached. After all, the error speaks about this explicitly. However, I just can't figure out how I should attach them. Any ideas?

JPanel panel = new JPanel(); GroupLayout layout = new GroupLayout(panel); panel.setLayout(layout); panel.setMinimumSize(new Dimension(2000,100)); panel.setBorder(BorderFactory.createTitledBorder((cdo.getTicker()) + " : (" + cdo.getCurrency() + ")")); layout.setVerticalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(new JButton("Invest"))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(new JButton("Ignore"))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(new JButton("Article"))) ); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(new JButton("Invest")) .addComponent(new JButton("Ignore")) .addComponent(new JButton("Article")) ) ); 
+4
source share
1 answer

new JButton("Invest") creates a new button that is different from the button previously created using the new JButton("Invest") .

Move the initialization of the buttons before the layout:

 JButton investButton = new JButton("Invest"); JButton articleButton = new JButton("Article"); JButton ignoreButton = new JButton("Ignore"); layout.setVerticalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(investButton)) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(ignoreButton)) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(articleButton))); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(investButton) .addComponent(ignoreButton) .addComponent(articleButton))); 
+7
source

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


All Articles