beemer
asked on
JTabbedPane painting question?
I am using JTabbedPane and I am having some painting problems. Whenever I switch tabs, there is residual painting from the previous tab. How can I get rid of this behavior? I have tried the following:
//Trying to fix bad painting when switching tabs...
tabPane.addChangeListener( new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
JComponent comp = (JComponent)tabPane.getCom ponentAt(t abPane.get SelectedIn dex());
comp.paintImmediately(comp .getVisibl eRect());
}
});
This did not correct the problem though. Has anyone ran across this same problem before, and if so how did you correct it?
//Trying to fix bad painting when switching tabs...
tabPane.addChangeListener(
{
public void stateChanged(ChangeEvent e)
{
JComponent comp = (JComponent)tabPane.getCom
comp.paintImmediately(comp
}
});
This did not correct the problem though. Has anyone ran across this same problem before, and if so how did you correct it?
please send sample code
ASKER
package Contract;
import com.sun.java.swing.*;
import com.sun.java.swing.event.* ;
import java.beans.*;
import java.awt.*;
import com.sun.java.swing.JSplitP ane;
import com.sun.java.swing.JPanel;
public class ContractSummary extends com.sun.java.swing.JPanel
{
public ContractSummary()
{
super();
//{{INIT_CONTROLS
setOpaque(false);
GridBagLayout gridBagLayout;
gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);
setSize(708,509);
setFont(new Font("Dialog", Font.PLAIN, 11));
setForeground(new Color(0));
setBackground(new Color(-3355444));
topPnl = new com.sun.java.swing.JPanel( );
topPnl.setOpaque(false);
topPnl.setLayout(new BorderLayout(0,0));
topPnl.setBounds(5,5,669,3 73);
topPnl.setFont(new Font("Dialog", Font.PLAIN, 12));
topPnl.setForeground(new Color(0));
topPnl.setBackground(new Color(-3355444));
GridBagConstraints gbc;
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.7;
gbc.weighty = 0.7;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,5,5);
((GridBagLayout)getLayout( )).setCons traints(to pPnl, gbc);
add(topPnl);
bottomPnl = new com.sun.java.swing.JPanel( );
bottomPnl.setOpaque(false) ;
bottomPnl.setLayout(new BorderLayout(0,0));
bottomPnl.setBounds(5,388, 669,159);
bottomPnl.setFont(new Font("Dialog", Font.PLAIN, 12));
bottomPnl.setForeground(ne w Color(0));
bottomPnl.setBackground(ne w Color(-3355444));
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0.3;
gbc.weighty = 0.3;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,5,5);
((GridBagLayout)getLayout( )).setCons traints(bo ttomPnl, gbc);
add(bottomPnl);
//}}
//{{REGISTER_LISTENERS
//}}
contractPnl = new Contracts();
topPnl.add(contractPnl, BorderLayout.CENTER);
profilePnl = new ContractProfile();
attachmentsPnl = new ContractAttachments();
correspondencePnl = new ContractCorrespondence();
coveragePnl = new ContractCoverage();
tabPane = new JTabbedPane();
tabPane.addTab("Profile", profilePnl);
tabPane.addTab("Attachment s", attachmentsPnl);
tabPane.addTab("Correspond ence", correspondencePnl);
tabPane.addTab("Coverages" , coveragePnl);
bottomPnl.add(tabPane, BorderLayout.CENTER);
/*
//Trying to fix bad painting when switching tabs...
tabPane.addChangeListener( new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
JComponent comp = (JComponent)tabPane.getCom ponentAt(t abPane.get SelectedIn dex());
comp.paintImmediately(comp .getVisibl eRect());
}
});
*/
}
//{{DECLARE_CONTROLS
com.sun.java.swing.JPanel topPnl;
com.sun.java.swing.JPanel bottomPnl;
//}}
Contracts contractPnl;
ContractAttachments attachmentsPnl;
ContractProfile profilePnl;
ContractCorrespondence correspondencePnl;
ContractCoverage coveragePnl;
JTabbedPane tabPane;
}
import com.sun.java.swing.*;
import com.sun.java.swing.event.*
import java.beans.*;
import java.awt.*;
import com.sun.java.swing.JSplitP
import com.sun.java.swing.JPanel;
public class ContractSummary extends com.sun.java.swing.JPanel
{
public ContractSummary()
{
super();
//{{INIT_CONTROLS
setOpaque(false);
GridBagLayout gridBagLayout;
gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);
setSize(708,509);
setFont(new Font("Dialog", Font.PLAIN, 11));
setForeground(new Color(0));
setBackground(new Color(-3355444));
topPnl = new com.sun.java.swing.JPanel(
topPnl.setOpaque(false);
topPnl.setLayout(new BorderLayout(0,0));
topPnl.setBounds(5,5,669,3
topPnl.setFont(new Font("Dialog", Font.PLAIN, 12));
topPnl.setForeground(new Color(0));
topPnl.setBackground(new Color(-3355444));
GridBagConstraints gbc;
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.7;
gbc.weighty = 0.7;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,5,5);
((GridBagLayout)getLayout(
add(topPnl);
bottomPnl = new com.sun.java.swing.JPanel(
bottomPnl.setOpaque(false)
bottomPnl.setLayout(new BorderLayout(0,0));
bottomPnl.setBounds(5,388,
bottomPnl.setFont(new Font("Dialog", Font.PLAIN, 12));
bottomPnl.setForeground(ne
bottomPnl.setBackground(ne
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0.3;
gbc.weighty = 0.3;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,5,5);
((GridBagLayout)getLayout(
add(bottomPnl);
//}}
//{{REGISTER_LISTENERS
//}}
contractPnl = new Contracts();
topPnl.add(contractPnl, BorderLayout.CENTER);
profilePnl = new ContractProfile();
attachmentsPnl = new ContractAttachments();
correspondencePnl = new ContractCorrespondence();
coveragePnl = new ContractCoverage();
tabPane = new JTabbedPane();
tabPane.addTab("Profile", profilePnl);
tabPane.addTab("Attachment
tabPane.addTab("Correspond
tabPane.addTab("Coverages"
bottomPnl.add(tabPane, BorderLayout.CENTER);
/*
//Trying to fix bad painting when switching tabs...
tabPane.addChangeListener(
{
public void stateChanged(ChangeEvent e)
{
JComponent comp = (JComponent)tabPane.getCom
comp.paintImmediately(comp
}
});
*/
}
//{{DECLARE_CONTROLS
com.sun.java.swing.JPanel topPnl;
com.sun.java.swing.JPanel bottomPnl;
//}}
Contracts contractPnl;
ContractAttachments attachmentsPnl;
ContractProfile profilePnl;
ContractCorrespondence correspondencePnl;
ContractCoverage coveragePnl;
JTabbedPane tabPane;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That seemed to correct the problem I was experiencing, but now, the first time my panel with the tabs is displayed, the first tab is showing, but the tab buttons and raised border does not appear, but if I resize the app, it does appear.
package Contract;
import com.sun.java.swing.*;
import com.sun.java.swing.event.* ;
import java.beans.*;
import java.awt.*;
import com.sun.java.swing.JSplitP ane;
import com.sun.java.swing.JPanel;
public class ContractSummary extends com.sun.java.swing.JPanel
{
public ContractSummary()
{
super();
//{{INIT_CONTROLS
GridBagLayout gridBagLayout;
gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);
setSize(708,509);
setFont(new Font("Dialog", Font.PLAIN, 11));
setForeground(new Color(0));
setBackground(new Color(-3355444));
topPnl = new com.sun.java.swing.JPanel( );
topPnl.setLayout(new BorderLayout(0,0));
topPnl.setBounds(5,5,698,3 42);
topPnl.setFont(new Font("Dialog", Font.PLAIN, 12));
topPnl.setForeground(new Color(0));
topPnl.setBackground(new Color(-3355444));
GridBagConstraints gbc;
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.7;
gbc.weighty = 0.7;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,5,5);
((GridBagLayout)getLayout( )).setCons traints(to pPnl, gbc);
add(topPnl);
bottomPnl = new com.sun.java.swing.JPanel( );
bottomPnl.setLayout(new BorderLayout(0,0));
bottomPnl.setBounds(5,357, 698,146);
bottomPnl.setFont(new Font("Dialog", Font.PLAIN, 12));
bottomPnl.setForeground(ne w Color(0));
bottomPnl.setBackground(ne w Color(-3355444));
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0.3;
gbc.weighty = 0.3;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,5,5);
((GridBagLayout)getLayout( )).setCons traints(bo ttomPnl, gbc);
add(bottomPnl);
//}}
//{{REGISTER_LISTENERS
//}}
contractPnl = new Contracts();
topPnl.add(contractPnl, BorderLayout.CENTER);
profilePnl = new ContractProfile();
attachmentsPnl = new ContractAttachments();
correspondencePnl = new ContractCorrespondence();
coveragePnl = new ContractCoverage();
tabPane = new JTabbedPane();
tabPane.addTab("Profile", profilePnl);
tabPane.addTab("Attachment s", attachmentsPnl);
tabPane.addTab("Correspond ence", correspondencePnl);
tabPane.addTab("Coverages" , coveragePnl);
bottomPnl.add(tabPane, BorderLayout.CENTER);
}
//{{DECLARE_CONTROLS
com.sun.java.swing.JPanel topPnl;
com.sun.java.swing.JPanel bottomPnl;
//}}
Contracts contractPnl;
ContractAttachments attachmentsPnl;
ContractProfile profilePnl;
ContractCorrespondence correspondencePnl;
ContractCoverage coveragePnl;
JTabbedPane tabPane;
}
package Contract;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*
import java.beans.*;
import java.awt.*;
import com.sun.java.swing.JSplitP
import com.sun.java.swing.JPanel;
public class ContractSummary extends com.sun.java.swing.JPanel
{
public ContractSummary()
{
super();
//{{INIT_CONTROLS
GridBagLayout gridBagLayout;
gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);
setSize(708,509);
setFont(new Font("Dialog", Font.PLAIN, 11));
setForeground(new Color(0));
setBackground(new Color(-3355444));
topPnl = new com.sun.java.swing.JPanel(
topPnl.setLayout(new BorderLayout(0,0));
topPnl.setBounds(5,5,698,3
topPnl.setFont(new Font("Dialog", Font.PLAIN, 12));
topPnl.setForeground(new Color(0));
topPnl.setBackground(new Color(-3355444));
GridBagConstraints gbc;
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.7;
gbc.weighty = 0.7;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,5,5);
((GridBagLayout)getLayout(
add(topPnl);
bottomPnl = new com.sun.java.swing.JPanel(
bottomPnl.setLayout(new BorderLayout(0,0));
bottomPnl.setBounds(5,357,
bottomPnl.setFont(new Font("Dialog", Font.PLAIN, 12));
bottomPnl.setForeground(ne
bottomPnl.setBackground(ne
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0.3;
gbc.weighty = 0.3;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5,5,5,5);
((GridBagLayout)getLayout(
add(bottomPnl);
//}}
//{{REGISTER_LISTENERS
//}}
contractPnl = new Contracts();
topPnl.add(contractPnl, BorderLayout.CENTER);
profilePnl = new ContractProfile();
attachmentsPnl = new ContractAttachments();
correspondencePnl = new ContractCorrespondence();
coveragePnl = new ContractCoverage();
tabPane = new JTabbedPane();
tabPane.addTab("Profile", profilePnl);
tabPane.addTab("Attachment
tabPane.addTab("Correspond
tabPane.addTab("Coverages"
bottomPnl.add(tabPane, BorderLayout.CENTER);
}
//{{DECLARE_CONTROLS
com.sun.java.swing.JPanel topPnl;
com.sun.java.swing.JPanel bottomPnl;
//}}
Contracts contractPnl;
ContractAttachments attachmentsPnl;
ContractProfile profilePnl;
ContractCorrespondence correspondencePnl;
ContractCoverage coveragePnl;
JTabbedPane tabPane;
}
Not knowing the details of the sub-panels:
Contracts contractPnl;
ContractAttachments attachmentsPnl;
ContractProfile profilePnl;
ContractCorrespondence correspondencePnl;
ContractCoverage coveragePnl;
JTabbedPane tabPane;
Nor how you are displaying the whole thing, I can only guess.
I would first try calling tabPane.invalidate() right before you setVisible(true); That should cause the BorderLayout to re-layout the whole thing. As an alternative, you can call getSize() on the ContractSummary and then setSize( size.width+1, size.height+1 ) -- this is an old trick from the 1.0.2 days to cause the panel to re-layout after it's been shown.
If neither of these work, post the code you use to create and display the ContractSummary and I'll fix it for you.
Contracts contractPnl;
ContractAttachments attachmentsPnl;
ContractProfile profilePnl;
ContractCorrespondence correspondencePnl;
ContractCoverage coveragePnl;
JTabbedPane tabPane;
Nor how you are displaying the whole thing, I can only guess.
I would first try calling tabPane.invalidate() right before you setVisible(true); That should cause the BorderLayout to re-layout the whole thing. As an alternative, you can call getSize() on the ContractSummary and then setSize( size.width+1, size.height+1 ) -- this is an old trick from the 1.0.2 days to cause the panel to re-layout after it's been shown.
If neither of these work, post the code you use to create and display the ContractSummary and I'll fix it for you.