?
Solved

JTabbedPane problem with multiple JPanels in JPanels

Posted on 2003-02-25
2
Medium Priority
?
1,595 Views
Last Modified: 2013-11-23
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
Comment
Question by:javaNewbie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
membersh earned 225 total points
ID: 8015967
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
 

Author Comment

by:javaNewbie
ID: 8038143
thx it worked fine now.
I used the awt-components cause they look better
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question