• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1599
  • Last Modified:

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.
0
javaNewbie
Asked:
javaNewbie
1 Solution
 
membershCommented:
Need to make 2 changes
1. Change Label to JLabel. Don't combine awt & swing components. 2.Add this.setLayout(null) ; as first line in TestPanel constructor
0
 
javaNewbieAuthor Commented:
thx it worked fine now.
I used the awt-components cause they look better
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now