javajframegrouplayout

Java GroupLayout positioning


I am having complications trying to setup my GroupLayout. I am trying to go off a tutorial from oracle's website but for the life of me cannot figure it out. Below is my current layout design followed by the code, not sure what I am doing wrong.

enter image description here

GroupLayout layout = new GroupLayout(mainPanel);
        mainPanel.setLayout(layout);

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(
                layout.createSequentialGroup()
                .addComponent(launchButton)
                .addComponent(modeComboBox)
                .addComponent(difficultyComboBox)
                .addComponent(mapComboBox)
                .addComponent(startButton)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))    
        );

        layout.setVerticalGroup(
                layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(launchButton)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
                            .addComponent(modeComboBox)
                            .addComponent(difficultyComboBox)
                            .addComponent(mapComboBox)
                            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
                            .addComponent(startButton))
        );

My Goal is to have it end up looking something like the picture below...

enter image description here


Solution

  • I started up NetBeans and laid out something like your picture showed. Try this and see if it's what you're looking for.

    package quicktest;
    
    public class ChaosLayout extends javax.swing.JPanel {
    
       /**
        * Creates new form ChaosLayout
        */
       public ChaosLayout() {
          initComponents();
       }
    
       /**
        * This method is called from within the constructor to initialize the form.
        * WARNING: Do NOT modify this code. The content of this method is always
        * regenerated by the Form Editor.
        */
       @SuppressWarnings( "unchecked" )
       // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
       private void initComponents() {
    
          jButton1 = new javax.swing.JButton();
          jButton2 = new javax.swing.JButton();
          jButton3 = new javax.swing.JButton();
          jButton4 = new javax.swing.JButton();
          jButton5 = new javax.swing.JButton();
    
          jButton1.setText("jButton1");
    
          jButton2.setText("jButton2");
    
          jButton3.setText("jButton3");
    
          jButton4.setText("jButton4");
    
          jButton5.setText("jButton5");
    
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
          this.setLayout(layout);
          layout.setHorizontalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                   .addComponent(jButton1)
                   .addComponent(jButton2)
                   .addComponent(jButton5))
                .addGap(18, 18, 18)
                .addComponent(jButton4)
                .addContainerGap(84, Short.MAX_VALUE))
          );
          layout.setVerticalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(jButton2)
                   .addComponent(jButton3)
                   .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 180, Short.MAX_VALUE)
                .addComponent(jButton5)
                .addContainerGap())
          );
       }// </editor-fold>                        
    
    
       // Variables declaration - do not modify                     
       private javax.swing.JButton jButton1;
       private javax.swing.JButton jButton2;
       private javax.swing.JButton jButton3;
       private javax.swing.JButton jButton4;
       private javax.swing.JButton jButton5;
       // End of variables declaration                   
    }
    

    Same layout, with sensible variable names, and I added a constraint to make the buttons all the same width, which I thought looked a little better.

    package quicktest;
    
    public class ChaosLayout extends javax.swing.JPanel {
    
       /**
        * Creates new form ChaosLayout
        */
       public ChaosLayout() {
          initComponents();
       }
    
       /**
        * This method is called from within the constructor to initialize the form.
        * WARNING: Do NOT modify this code. The content of this method is always
        * regenerated by the Form Editor.
        */
       @SuppressWarnings( "unchecked" )
       // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
       private void initComponents() {
    
          launch = new javax.swing.JButton();
          chaos = new javax.swing.JButton();
          trials = new javax.swing.JButton();
          map = new javax.swing.JButton();
          start = new javax.swing.JButton();
    
          launch.setText("Launch");
    
          chaos.setText("Chaos");
    
          trials.setText("Trials");
    
          map.setText("Map");
    
          start.setText("Start");
    
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
          this.setLayout(layout);
          layout.setHorizontalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(trials)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                   .addComponent(launch)
                   .addComponent(chaos)
                   .addComponent(start))
                .addGap(18, 18, 18)
                .addComponent(map)
                .addContainerGap(99, Short.MAX_VALUE))
          );
    
          layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {chaos, launch, map, start, trials});
    
          layout.setVerticalGroup(
             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(launch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(chaos)
                   .addComponent(trials)
                   .addComponent(map))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 180, Short.MAX_VALUE)
                .addComponent(start)
                .addContainerGap())
          );
       }// </editor-fold>                        
    
    
       // Variables declaration - do not modify                     
       private javax.swing.JButton chaos;
       private javax.swing.JButton launch;
       private javax.swing.JButton map;
       private javax.swing.JButton start;
       private javax.swing.JButton trials;
       // End of variables declaration                   
    }