javaswingjtablenetbeans-10

Overlap Components When Using ComponentResizer


I want to make my jTable expandable while floating/overlay when dragging it using ComponentResizer class. I'm using ComponentResizer class from Rob Camick.

Currently as below after dragging the table:

enter image description here

I tried change my layout to null but the result still same. In my code, I just added to call the ComponentResizer class:

ComponentResizer cr = new ComponentResizer();
cr.setSnapSize(new Dimension(10, 10));
cr.registerComponent(jScrollPane2);

I expect the dragging table will float and overlay the components below it.


Solution

  • Swing is designed to paint components in 2D space, not 3D space.

    So when components are added to the same panel, Swing will paint components in the reverse order the component is added to the panel. In your case it looks like you add the components to the panel before adding the scrollpane to the panel.

    So you could:

    1. reverse the order in which you add the components to the panel
    2. use the setComponentZOrder(...) method on the scrollpane to set its value to 0, so it is painted last.

    However, this will still cause a problem because if you hover over the button the button will appear because its border is changed. This is because Swing assumes 2D layouts not 3D. If you want to make sure the table is always painted over the button you need to override the isOptimizedDrawingEnable() method of the panel. See Overlap Layout for more information on ZOrder painting.