Problem with Split Pane, JOGL, GLCanvas

When I create a simple app with a split pane that contains a tree on the left and a glcanvas on the right everything seems to work ok. Then, when I go to move the split, as soon as I click on the splitter to move it, the init method gets called on the gllistener. This seems to imply that the GLContext has changed or gone away?? In order to compensate for this I will have to reload all my textures and display lists everytime someone changes the split. This is not very good. Is there a way to avoid this behavior?

I’m doing this in Jython…so to ‘prove’ that it happens with a normal java project I made the below program starting with the default JOGL GLCanvas project in Netbeans. The same behavior happens. Is there a way to initialize and ‘hold’ a GLContext so that I can make this work? (I have not included the code for the generated GLRenderer.java because I did not change anything in it).

Thanks

/*
 * SimpleGLCanvas.java
 *
 * Created on 30. Juli 2008, 16:18
 */

package org.yourorghere;

import com.sun.opengl.util.Animator;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

/**
 *
 * @author cylab
 * @author mbien
 */
public class SimpleGLCanvas extends JFrame {

    static {
        // When using a GLCanvas, we have to set the Popup-Menues to be HeavyWeight,
        // so they display properly on top of the GLCanvas
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    }

    //private Animator animator;

    /** Creates new form MainFrame */
    public SimpleGLCanvas() {
        initComponents();
        setTitle("Simple JOGL Application");

        canvas.addGLEventListener(new GLRenderer());
        //animator = new Animator(canvas);

        // This is a workaround for the GLCanvas not adjusting its size, when the frame is resized.
        canvas.setMinimumSize(new Dimension());

        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                // Run this on another thread than the AWT event queue to
                // make sure the call to Animator.stop() completes before
                // exiting
                new Thread(new Runnable() {

                    public void run() {
                        //animator.stop();
                        System.exit(0);
                    }
                }).start();
            }
        });
    }

    @Override
    public void setVisible(boolean show){
        //if(!show)
            //animator.stop();
        super.setVisible(show);
        //if(!show)
            //animator.start();
    }

    /** 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() {
        JLabel label = new JLabel();
        jSplitPane1 = new JSplitPane();
        canvas = new GLCanvas(createGLCapabilites());
        jScrollPane1 = new JScrollPane();
        jTree1 = new JTree();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        label.setText("Below you see a GLCanvas");

        jSplitPane1.setRightComponent(canvas);

        jScrollPane1.setViewportView(jTree1);

        jSplitPane1.setLeftComponent(jScrollPane1);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(Alignment.LEADING)
                    .addComponent(label)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jSplitPane1, GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    /**
     * Called from within initComponents().
     * hint: to customize the generated code choose 'Customize Code' in the contextmenu
     * of the selected UI Component you wish to cutomize in design mode.
     * @return Returns customized GLCapabilities.
     */
    private GLCapabilities createGLCapabilites() {

        GLCapabilities capabilities = new GLCapabilities();
        capabilities.setHardwareAccelerated(true);

        // try to enable 2x anti aliasing - should be supported on most hardware
        capabilities.setNumSamples(2);
        capabilities.setSampleBuffers(true);

        return capabilities;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        // Run this in the AWT event thread to prevent deadlocks and race conditions
        System.setProperty("sun.awt.noerasebackground", "true");
        EventQueue.invokeLater(new Runnable() {
            public void run() {

                // switch to system l&f for native font rendering etc.
                try{
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }catch(Exception ex) {
                    Logger.getLogger(getClass().getName()).log(Level.INFO, "can not enable system look and feel", ex);
                }
                SimpleGLCanvas frame = new SimpleGLCanvas();
                frame.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private GLCanvas canvas;
    private JScrollPane jScrollPane1;
    private JSplitPane jSplitPane1;
    private JTree jTree1;
    // End of variables declaration

}

David

DOH! Changed my search and found an answer in another section…please disregard or delete!

Sorry,

Daivd

For anyone else who comes across this…the clue was in this discussion: http://www.java-gaming.org/index.php/topic,19147.0.html

I was able to add the GLCanvas to a JScrollPane, change the horizontal and vertical ScrollBarPolicies to Never. Then I had to change the Listener for the JScrollPane to resize the GLCanvas whenever the JScrollPane is resized.

After that I got the exact behivior that I was hoping for, with init() only called at the appropriate time, but with a fully functional GLCanvas.

Regards,

David