0

I'm trying to create a JTable containing JOGL GLCanvas or GLJPanel on table's cells.

I define a custom table cell renderer that inherits from the GLJPanel and call the addGLEventListener to define my custom draw on the display method (for the moment I'm trying to draw the same on each cell). I define the getTableCellRendererComponent that returns the custom table itself.

Here is my code of the cell renderer :

private static class GLCellRenderer extends GLJPanel implements TableCellRenderer {
    private String value;
    private Color color;

    public GLCellRenderer () {
        super(new GLCapabilities(GLProfile.getDefault()));

        addGLEventListener( new GLEventListener() {

            @Override
            public void init( GLAutoDrawable glautodrawable ) {}

            @Override
            public void dispose( GLAutoDrawable glautodrawable ) {}

            @Override
            public void display( GLAutoDrawable glautodrawable ) {
                GL2 gl = glautodrawable.getGL().getGL2();
                gl.glClearColor(1, 0, 0, 1);
                gl.glClear(GL.GL_COLOR_BUFFER_BIT);
            }
        });            
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,int column) {          
        return this;
    }
}

And here is my code to apply the cell renderer to the table :

JTable table = new JTable(model);
table.setDefaultRenderer(Data.class, new GLCellRenderer());

When launching, I have a table with white cells when I'm expecting red cells.

Is it possible to use GLCanvas or GLJpanel on table cell?

  • Why *"on table cell"* as opposed to in a `GridLayout` or similar? You might find that the flyweight pattern used for table cells is incompatible with a `GLCanvas`.. It might be fine as well, I'm not sure, ..but answer the question in any case. – Andrew Thompson May 05 '17 at 08:44
  • I can't use a GridLayout instead. The table must to be scrollable. It must to sort the items and hide some columns. I need all mechanism of the Jtable. I thought to have canvas only for visible cells and reuse them when their associated cell model changes. – kevin guirado May 05 '17 at 08:53
  • *"The table must to be scrollable."* Set a `GridLayout` to a `JPanel` and put the panel in a `JScrollPane` and it will be scrollable, but.. *"It must to sort the items and hide some columns."* .. **that** is what distinguishes a table from most other approaches. Does it work with a standard `JComponent` such as `JPanel` in place of the `GLCanvas`? – Andrew Thompson May 05 '17 at 09:10
  • Did you `setOpaque(false)`? – trashgod May 05 '17 at 09:15
  • I try setOpaque(false) but the result is the same. Yesterday I did some test extending a JPanel instead the GLJPanel. When adding a JLabel inside, it works fine (the text of the label is well displayed). When adding inside a GLJPanel or GLCanvas, cells stays blank too. – kevin guirado May 05 '17 at 09:39
  • 1
    No - as a general answer. Two reasons - 1) jolg is backed by a heavy weight component which tends to cause no end issues with Swing; 2) jolg uses it's own painting process which is not compatiable with Swing. You have to remember, a table cell is a "snap shot" of the component, it's not a live component – MadProgrammer May 05 '17 at 10:35
  • You can use some offscreen rendering to obtain a similar result by calling GLDrawableFactory.createOffscreenAutoDrawable(), by using GLReadBufferUtil to read the pixels in the drawable and then you just copy them into a Swing BufferedImage by using the data buffer of its raster (transfer from a NIO buffer in JOGL to a NIO buffer in Swing). This solution works for sure, I used it two years ago. You can call GLAutoDrawable.display() in the method JComponent.paintComponent() and then you just call Graphics.drawImage() with your buffered image. – gouessej May 11 '17 at 07:55

0 Answers0