0

So for the sake of keeping it simple, lets say I want to have 3 separate programs all display in a JTabbedPane. Suppose they are all Hello World simple file in a JFrame.

Lets use this simple file and create HelloWorld1.java, HelloWorld2.java and HelloWorld3.java. They exist separately this way. BUT say I want to combine them into ONE JTabbedPane - still keeping them separate. I am guessing I would need to create a project (lets say in Eclipse) and set up a JTabbedPane in a forth file and add these? Can someone give me a demonstration of this so I can understand clearer how to go about this. Appreciate any help. Just really interested in what the main (forth) file might look like.

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Marc S.
  • 5
  • 3

2 Answers2

4
public static void main(String[] args) {
    JFrame child = new HelloWorld1("child1");
    child.add(new JLabel("Child1"));

    JTabbedPane jTabbedPane = new JTabbedPane();
    jabbedPane.addTab("Child", child.getContentPane());

    child = new HelloWorld2("child2");
    jabbedPane.addTab("Child2", child.getContentPane());
    //and so on with other applications/frames

    JFrame main = new JFrame("Main");
    main.setSize(600,600);
    main.add(jTabbedPane);      
    main.setVisible(true);
}
Pawel Solarski
  • 1,028
  • 8
  • 7
2

Solution is very simple:

1) Instead of saying public class HelloWorld1 extends JFrame{....} use public class HelloWorld1 extends JPanel{...}

2) Instantiate your classes in main class (HellowWorld1,HellowWorld2 and HellowWorld3) and add them to JTabbedPane like this:

HelloWorld1 hw = new HelloWorld1();
....
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("Title of first panel",hw);
Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85