JOGL and NetBeans GroupLayout / Matisse

I use NetBeans’ visual GUI editor to create most of my GUI code. For some reason, a GLCanvas does not resize properly when used with GroupLayout. It will expand when the window is enlarged, but it will not shrink when the window shrinks. I’m not an expert with JOGL nor GroupLayout so I’m not sure where the problem lies.

Here’s the simplest test case:

  1. Create a new JFrame Form within NetBeans.
  2. Add a JPanel to the JFrame and use the resize handles to make it fill the entire JFrame.
  3. Select the JPanel and click the “Auto Resize” horizontal and vertical buttons.
  4. Right click the JPanel and select “Set Layout -> BorderLayout”
  5. Manually add the GLCanvas to the JPanel using:

glPanel.add(canvas,BorderLayout.CENTER);

Here’s the complete code example:


public class SimpleMatisseTest extends javax.swing.JFrame {
    
    public SimpleMatisseTest() {
        initComponents();
        
        GLCapabilities glCaps = new GLCapabilities();
        glCaps.setRedBits(8);
        glCaps.setBlueBits(8);
        glCaps.setGreenBits(8);
        glCaps.setAlphaBits(8);
        
        GLCanvas canvas = new GLCanvas(glCaps);
        
        glPanel.add(canvas,BorderLayout.CENTER);
        
        canvas.addGLEventListener(new TestRenderer());
        
        final Animator animator = new Animator( canvas);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                animator.stop();
                System.exit(0);
            }
        });
        animator.start();
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        glPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        glPanel.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(glPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(glPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>                        
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SimpleMatisseTest().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JPanel glPanel;
    // End of variables declaration                   
    
}

Here’s the TestRenderer from the stick thread on this forum.


public class TestRenderer implements GLEventListener {
    
    private GL              gl;
    private GLDrawable      glDrawable;
    
    public void init(GLAutoDrawable gLAutoDrawable) {
        this.gl = gLAutoDrawable.getGL();
        
        //drawable.setGL( new DebugGL(drawable.getGL() ));
        
        System.out.println("Init GL is " + gl.getClass().getName());
    }

    public void display(GLAutoDrawable gLAutoDrawable) {
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
        gl.glLoadIdentity();
        
        gl.glColor3f(1.0f, 0.0f, 0.0f );
        
        gl.glBegin( GL.GL_TRIANGLES );
        gl.glVertex3f( 0.0f, 0.0f, 0.0f );
        gl.glVertex3f( 1.0f, 0.0f, 0.0f );
        gl.glVertex3f( 1.0f, 1.0f, 0.0f );
        gl.glEnd();
    }

    public void reshape(GLAutoDrawable gLAutoDrawable, int i, int i0, int i1, int i2) {
    }

    public void displayChanged(GLAutoDrawable gLAutoDrawable, boolean b, boolean b0) {
    }
}

P.S. The thread: “How to: Getting started with JOGL” should probably be updated to reflect the recent (?) API changes. As someone new to JOGL, it kind of sucks when the code in the tutorial does not compile because certain methods no longer exist or have been changed.

(edit: looking at my jogl lib it appears I’m using a release candidate - so my apologies if the above tutorial is for the current stable version) :slight_smile:

Thanks

This is a known issue. See the section “Heavyweight and Lightweight Components” in the JOGL Users’ Guide for a description of why this is happening and workarounds.

You or other members of the community should feel free to update that thread and post newer code at the end. The tutorials linked from the JOGL home page such as the NeHe ports and Red Book examples are all up to date.

Thank you