0

I am trying to create components and implement them into my JFrame from different classes within my program.

I have created a JTabbedPane, and each tab represents a class. All of the components for each tab are placed in their respective tabs.

//creates the JTabbedPane, and the panels. object creation.
//panelx corisponds to the tab number as well. tabbs are counted from left to right. 
tabpane1 = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();

JLabel searchlabel1 = new JLabel("hey");
JLabel searchlabel2 = new JLabel("hi");

panel1.add(searchlabel1);
panel1.add(searchlabel2); 

//SearchFlight searchflightComp = new SearchFlight();

 tabpane1.addTab("Search Flight", panel1);
 tabpane1.addTab("Select Flight", panel2);
 tabpane1.addTab("Flight Price", new JLabel("This is tab 1ffff")); 
 tabpane1.addTab("Book Ticket", new JLabel("This is tab 1fff"));
 tabpane1.addTab("Book Ticket", new JLabel("This is tab fs1"));
 tabpane1.addTab("Payment", new JLabel("This is tabgf 1"));
 tabpane1.addTab("Booking Summary", new JLabel("This is tabgf 1"));
 //added the JTabbedPane to JFrame. 
 frame.getContentPane().add(tabpane1);

this works. I am only really working with the first tab right now to get the feel for how it works ect. But I dont even know how to begin. Would I create the a panel in the other class and then return it? or extend the JFrame?

thanks guys!

user3026473
  • 23
  • 1
  • 6
  • Unclear what you're asking, do you want to use only `one` tab holding all the components? – Azad Mar 10 '14 at 15:10
  • If you don't need the other tabs, why not simply change to last line to `frame.getContentPane().add(panel1);`? – Kojotak Mar 10 '14 at 15:10

2 Answers2

0

I assume you're refering to this commented line:

//SearchFlight searchflightComp = new SearchFlight();

You could either make SearchFlight a subclass of JPanel or better a controller which creates a JPanel for that tab and return it, e.g.

SearchFlight searchflightComp = new SearchFlight();
tabpane1.addTab( searchflightComp.getName(), searchflightComp.buildPanel() );

As a general tip you should read up on the MVC pattern. This might help you to some extent: The MVC pattern and SWING

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157
0

maybe you could extend from JComponent? And do it like they do here?

Community
  • 1
  • 1
SuperRetro
  • 91
  • 6