I have an AWT canvas that I cannot convert to a Swing component (it comes from VTK ). I want to display some of these paintings inside JSplitPane. I read about mixing heavy and light components in Java and I know that this is a pain in the butt, but I have no choice. If I wrap the AWT canvas inside a JPanel and then put it on the split panel, the split panel doesn't work at all. However, if I placed the AWT canvas inside the JPanel, and then inside the JScrollPane, and then these scrollbars on the JSplitPane, the split panel works, but the components of the AWT canvas do not change correctly. I'm lost on how to get AWT canvas components to resize correctly when the JSplitPane separator moves. I can catch the divider move operation and work on AWT canvases at that time, but I don't know what to do. I tried calling invalidate (), then validate (), and then repaint (), but that didn't work.
Any ideas?
Here is an example of a problem
import javax.swing.*; import java.awt.*; public class SwingAWTError { public static void main(String[] args) { Canvas leftCanvas = new Canvas(); Canvas rightCanvas = new Canvas(); leftCanvas.setBackground(Color.RED); rightCanvas.setBackground(Color.BLUE); JPanel leftPanel = new JPanel(); JPanel rightPanel = new JPanel(); leftPanel.setLayout(new BorderLayout()); rightPanel.setLayout(new BorderLayout()); leftPanel.add(leftCanvas, BorderLayout.CENTER); rightPanel.add(rightCanvas, BorderLayout.CENTER); JScrollPane leftScroll = new JScrollPane(); JScrollPane rightScroll = new JScrollPane(); leftScroll.getViewport().add(leftPanel); rightScroll.getViewport().add(rightPanel); JSplitPane split = new JSplitPane(); split.setLeftComponent(leftScroll); split.setRightComponent(rightScroll); split.setDividerLocation(400); JFrame frame = new JFrame(); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(split, BorderLayout.CENTER); frame.setSize(800, 800); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
source share