javaNewbie
asked on
JTabbedPane problem with multiple JPanels in JPanels
Hi
I wanted to make a nice tabbedpane program, but got some
problems here. The contents of the 2nd Tab are visible on the
first Tab, which shouldn't be. Here's my Code for my problem:
import java.awt.*;
import javax.swing.*;
public class TestFrame extends JFrame{
private TestFrame(String title) {
super(title);
initLayout();
}
private TestFrame() {
this("Title");
}
public static void main(String[] args) {
TestFrame tf = new TestFrame();
Toolkit kit = Toolkit.getDefaultToolkit( );
Dimension screenResolution = kit.getScreenSize();
double width = screenResolution.getWidth( );
double height = screenResolution.getHeight ();
tf.setDefaultCloseOperatio n(JFrame.E XIT_ON_CLO SE);
tf.setSize(520, 690);
tf.setLocation((((int)widt h)-tf.getW idth())/2, ((int)heig ht-tf.getH eight())/2 );
tf.setResizable(false);
tf.show();
}
private void initLayout(){
TestPanel tp1 = new TestPanel("page one");
TestPanel tp2 = new TestPanel("page two");
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add(tp1, "Page One");
tabbedPane.add(tp2, "Page Two");
getContentPane().add(tabbe dPane);
getContentPane().setBackgr ound(Color .gray);
}
}
class TestPanel extends JPanel{
private Font font = new Font("Arial", Font.PLAIN, 10);
private Font font2 = new Font("Arial", Font.PLAIN, 12);
public TestPanel(String testTitle){
JPanel panel1 = new JPanel();
setPanel(panel1);
Label title = new Label(testTitle);
setTitle(title);
place(panel1, title, 1, 1, 148, 15);
place(this, panel1, 5, 5, 150, 50);
}
private void setTitle(Label l){
l.setBackground(Color.blue );
l.setFont(font2);
}
private void setPanel(JPanel p){
p.setLayout(null);
p.setFont(font);
p.setBackground(Color.gray );
p.setBorder(BorderFactory. createLine Border(Col or.black, 1));
}
private void place(JPanel c, Component comp, int x, int y, int width, int height) {
comp.setLocation(x,y);
comp.setSize(width,height) ;
c.add(comp);
}
}
Please Compile and run it and you'll see that on tab one the String "page two" appears.. this happens only if i start the program. After clicking on a tab, the true content appears.
I'm desperately seeking the answer to this.
I wanted to make a nice tabbedpane program, but got some
problems here. The contents of the 2nd Tab are visible on the
first Tab, which shouldn't be. Here's my Code for my problem:
import java.awt.*;
import javax.swing.*;
public class TestFrame extends JFrame{
private TestFrame(String title) {
super(title);
initLayout();
}
private TestFrame() {
this("Title");
}
public static void main(String[] args) {
TestFrame tf = new TestFrame();
Toolkit kit = Toolkit.getDefaultToolkit(
Dimension screenResolution = kit.getScreenSize();
double width = screenResolution.getWidth(
double height = screenResolution.getHeight
tf.setDefaultCloseOperatio
tf.setSize(520, 690);
tf.setLocation((((int)widt
tf.setResizable(false);
tf.show();
}
private void initLayout(){
TestPanel tp1 = new TestPanel("page one");
TestPanel tp2 = new TestPanel("page two");
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add(tp1, "Page One");
tabbedPane.add(tp2, "Page Two");
getContentPane().add(tabbe
getContentPane().setBackgr
}
}
class TestPanel extends JPanel{
private Font font = new Font("Arial", Font.PLAIN, 10);
private Font font2 = new Font("Arial", Font.PLAIN, 12);
public TestPanel(String testTitle){
JPanel panel1 = new JPanel();
setPanel(panel1);
Label title = new Label(testTitle);
setTitle(title);
place(panel1, title, 1, 1, 148, 15);
place(this, panel1, 5, 5, 150, 50);
}
private void setTitle(Label l){
l.setBackground(Color.blue
l.setFont(font2);
}
private void setPanel(JPanel p){
p.setLayout(null);
p.setFont(font);
p.setBackground(Color.gray
p.setBorder(BorderFactory.
}
private void place(JPanel c, Component comp, int x, int y, int width, int height) {
comp.setLocation(x,y);
comp.setSize(width,height)
c.add(comp);
}
}
Please Compile and run it and you'll see that on tab one the String "page two" appears.. this happens only if i start the program. After clicking on a tab, the true content appears.
I'm desperately seeking the answer to this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I used the awt-components cause they look better