Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JtabbedPane Question  RSS feed

 
Ben Koh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi people,I wanted to develop a swing UI using JtabbedPane by adding components into it. The tabbedPane is later added into a frame called BookingFrame. The following is the constructor for the frame.
public BookingFrame() {
// create new tabbedPane object
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab(" Search Movie ",null, buildSearchPanel(), "Search Movie");
tabbedPane.addTab(" Booking ",null, buildBookingPanel(), "Book a Movie");
tabbedPane.addTab(" Feedback ",null, buildFeedbackPanel(), "Your Feedback");
// assign layout manager
setBackground(Color.black);
setLayout(new GridLayout(1,1));
add(tabbedPane);
}
I know a certain tab and its component can be displayed by using the .setSelectedIndex(int). However, it doesn't seem to work when its outside the BookingFrame() constructor as shown above (I tried toggling it from actionPerformed). What I had in mind was when a user clicks a button in the first tab, the second tab with its component will be shown. Hope someone can please enlighten me. Thank You!
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,
One problem that jumps out at me is that you have create tabbedPane as a local variable inside the constructor. You need to get some handle on it when you are outside that routine. You have 2 possibilities:
1. Make the tabbedPane an instance variable by defining it outside of the constructor.
or
2. Use BookingFrame.getComponents() to get the BookingFrame children and then find the tabbedPane.
Either way you can then use the setSelectedIndex method on the tabbedPane handle.
Regards,
Manfred.
 
Ben Koh
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot Manfred. Will give it a try.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!