JOGL + AWT or Swing

Have any of you had any luck inserting a GLCanvas as a pane in a Swing app? The only time I can get JOGL to work properly is when it is the only image and is taking up all of the real estate on the frame.

I would like the ability to have a portion of the Frame available for things like buttons, sliders, or other simple controls. This could work by having multiple JPanels, with the canvas covering one panel, but I can’t get this to work. Have any of you done this successfully?

Thanks,
Jeeky

I am developing a JOGL app with some swing components on it. Everything works fine! I used the border layout manager and put various Jpanels in the frame with the GLCanvas in a Jpanel in the center area.

What is the problem that you are experiencing?

The GLCanvas doesn’t appear to be present on the JPanel, or at least it isn’t getting sized right. I can call setSize(x, y) on the GLCanvas in the constructor, and then the GLCanvas is present, but not sized right.

I guess the problem is that I am unable to get the canvas to size properly when the parent panel is resized. It seems like the GLEventListener isn’t getting registered properly with a JPanel of something.

Do you attach the canvas to your panel by using JPanel.add(myGLCanvas) or do you do something else? Is there any special code you need to implement to get the sizing to work?

I add a GLCanvas to a JPanel using a BorderLayout. The GLCanvas resizes as it should.

GLCanvas c;
JPanel p = new JPanel(new BorderLayout());
p.add(c, BorderLayout.CENTER);

Thanks pep, that worked like a charm. It appears that the default flow layout has issues.

No it doesn’t have issues, it just works differently than BorderLayout. You really have to understand the default behavior of each layout and each component to get the most out of using them.

Two Component methods come into play when added to a Container - getPreferredSize() and getMinimumSize(). Some layout managers may or make not make use fo these. For example, a FlowLayout is gauranteed to respect both the preferred width and height, while a CardLayout will ignore both. In a BorderLayout, what is respected or ignored depends on the location - BorderLayout.NORTH & SOUTH respect the preferred height, but ignore the preferred width, BorderLayout.CENTER ignores both.

Knowing how a LayoutManager handles preferred size is not enough, however. You also need to know what the default preferred size of a particular component is, and what the LayoutManager will do when it ignores the preferred size. This is why your Canvas is giving you trouble.

A Canvas by default has a preferred width of 0 and a preferred height of 0. FlowLayout respects preferred width and height. Therefore, when the Canvas is added to the layout its size will be set by a FLowLayout to 0,0 - and will therefore not be visible. BorderLayout.CENTER ignores the preferred size completely, and the default behavior is to stretch the component in both dimensions to fill the remaining space in the Container. So if you have an empty Container with a BorderLayout manager, your Canvas will fill be sized to fill the container if you add it to the CENTER> Adding to NORTH or SOUTH respects the preffered height, but stretches the Component to fill the width of the Container, while adding to EAST or WEST repsects the preferred width, but stretches the Component to fill the height of the Container.

Obviously, GLCanvas did not override the Canvas getPreferredSize method. And IIRC GLCanvas is final, so you will need to know the properties of each layout manager in order to use it effectively.

Thanks for the info. I have been doing C++/OpenGL programming for a while, but am new to Java. Please pardon my ignorance.