Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTabbedPane change tab event 1

Status
Not open for further replies.

AlexMidd

Programmer
May 14, 2001
655
NL
Is there some way of trapping the event when someone clicks on a tab to change the view in a JTabbedPane. I want the data being shown in the tab that the user is going to to be updated at this point, based on data in the other tab that they are just coming from. Have fun! :eek:)

Alex Middleton
 
You want to implement and add a ChangeListener to your tabbed pane. Or you can of course just implement the stateChanged method from ChangeAdapter and add that. In any case, in the stateChanged method, just check the index of the tabbed pane. Take a look at this code:

public void stateChanged( ChangeEvent event )
{
int index = tabbedPane.getSelectedIndex();
if( index == 0 )
// if the first tabe is selected, do this
else if( index == 1 )
// if the second tab is selected, do this
}

Hope that helps!

-gc "I don't look busy because I did it right the first time."
 
Thanks godcomplex (how do you guys think up these wonderful names?). I will try that. Do I need to implement anything in my class (it is an extension of JFrame) to do this, like ActionListener for Action events? Forgive me if it sems obvious - Java is not my first language, but it's growing on me. Have fun! :eek:)

Alex Middleton
 
You need to implement the ChangeListener in your class, just like you implement the ActionListener. However, in this case instead of adding and implementing the actionPerformed method, you will add and implement the stateChanged method from ChangeListener.

-gc "I don't look busy because I did it right the first time."
 
Hi, thanks again for your help. However, I have tried adding the ChangeListener but when I compile it says it cannot resolve symbol ChangeListener. My class is declared as:

public class HTMLEditor extends JFrame implements ActionListener, ChangeListener

Am I doing something wrong?

Cheers,
Have fun! :eek:)

Alex Middleton
 
Sorry I have know found my error, I hadn't added import javax.swing.event.*;

I will try that.

Thanx. A star for your help.
Have fun! :eek:)

Alex Middleton
 
Just to let you know, it's all working now. I forgot to add the ChangeListener to the tabbedPane as well ! Silly me. Have fun! :eek:)

Alex Middleton
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top