Hi, I'm trying to do some component switching in
one tab of JTabbedPane.
I have a JPanel "jp1" containing another custom-painted extension of JPanel "customPanel" and a button.
Then I have another JPanel "jp2" containing only a button.
The custom-painted panel is at this stage painted with plain gray color one line at a time (top to bottom).
I add panel jp1 as a tab component with addTab(). Then after clicking on a button (in jp1) I want jp1 to exchange for panel jp2 (I do it with setComponentAt() at the tab with index of indexOfComponent(jp1) for the code to be more flexible). Then going back to jp1 when clicking on a button in jp2. This functionality seems to work fine.
The problem emerges when it comes to painting the newly exchanged panel. I use a revalidate()-repaint() combo on this new tab-panel. This works fine until the customPanel gets involved. When the customPanel is added to jp1, even though it is thoroughly painted (I didn't implement using clipping rectangle in its paintComponent() yet), it has an unpainted region (artifact?) right in the middle. When I force Java to repaint it (by moving to another tab and coming back, frame resizing or minimizing and restoring) the painting works just fine. Is there something I'm missing?
This is the code I use for changing panels in the tab.
private final JButton button = new JButton("Change to the other panel");
{
try {
SwingUtilities.invokeAndWait(new Runnable() {public void run() {
button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
jTabbedPane.setComponentAt(
jTabbedPane.indexOfComponent( jp1 ),
jp2
);
jp2.revalidate();
jp2.repaint();
} } );
} } );
} catch(Exception e) {e.printStackTrace(System.err);}
}