Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to switch between tabs in swings?

 
ragz wifer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an swing application with a JTabbedPane containing some JPannels (tabs).I have a Button in tab1(say). I coded the Button action in such a way that it generates a new tab say tab2 which contains some components inside it but the thing is when a user clicks on tab2 user is able to see the components in Tab2

My Question is instead of user activity of clicking on tab2 is it possible to load and display tab2 components automatically and show it to user. What i mean to say is Bring it to front

any help is greatly aprized

Thanks in advance
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out JTabbedPanel#setSelectedIndex()
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Click on the link that is automatically created for JTabbedPane. Scroll down to the method list. Read through them and see if one does what you want. I'll tell you two things:
1) there is at lease one such a method
2) it starts with set

Edit: Maneesh beat me to it. However, you can use the above steps to find a lot more useful methods, for all classes.
 
ragz wifer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i used the JTabbedPane.setSelectedIndex(-1); then it shows all the tabs where none of it got selected.

how to set index to a jpannel?how to bring tab2 to front???

here is my code what it does is its generating tab2 but my question is instead of user to click on tab2 tab2 should be loaded automatically when new client button is clicked

please and kindly help me
thanks in advance
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ragz wifer wrote:

i used the JTabbedPane.setSelectedIndex(-1);then it shows all the tabs where none of it got selected.


No surprises there.
From the API docs
The index must be a valid tab index or -1, which indicates that no tab should be selected


In the code you have posted, I do not see where you have used it
 
ragz wifer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maneesh

firstly i doesn't know how to set an index to a JPannel i.e.., a tab

once it is done that index value should be placed in the below line right?

JTabbedpane.setSelectedIndex();
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo code

1) Create frame
2) Create tabbed pane
3) Add required tabs to tabbed pane
4) Add tabbed pane to frame
5) Select the required tab*

*Can be done on button click too
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic