Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GroupLayout: Vertical and Horizontal Groups

I'm attempting to create a small Jpanel with a GroupLayout infront of it. Having followed the documentation as much as possible as well as looked at a number of StackOverflow questions, 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=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Invest,defaultCapable=true] is not attached to a vertical group

I know that the problem is related to where the buttons are being attached. After all the error says it explicitly. However, I just can't figure out in what manner I'm supposed to 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"))
                    )
            );
like image 251
TheMightyLlama Avatar asked Aug 10 '13 07:08

TheMightyLlama


People also ask

What is GroupLayout?

GroupLayout is a LayoutManager that hierarchically groups components in order to position them in a Container . GroupLayout is intended for use by builders, but may be hand-coded as well. Grouping is done by instances of the Group class. GroupLayout supports two types of groups.

How do you create a group layout?

We create 1 JLabel , 1 JTextField and 2 JCheckbox components. Two JButton components are also created as “FindButton“, “CancelButton” and then add them to the JFrame by using add() method. The layout is set by using setLayout() method.

What is JPanel Swing Java?

JPanel, a part of the Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components, various layouts can be set in JPanel which provide better organization of components, however, it does not have a title bar.


1 Answers

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

Move the initializations 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)));
like image 169
JB Nizet Avatar answered Sep 25 '22 05:09

JB Nizet