Link to home
Start Free TrialLog in
Avatar of javaNewbie
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             tf.setSize(520, 690);
          tf.setLocation((((int)width)-tf.getWidth())/2,((int)height-tf.getHeight())/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(tabbedPane);
          getContentPane().setBackground(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.createLineBorder(Color.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.
ASKER CERTIFIED SOLUTION
Avatar of membersh
membersh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of javaNewbie
javaNewbie

ASKER

thx it worked fine now.
I used the awt-components cause they look better