Making the ChangeListener only fire once on a JTabbedPane

Hi guys, I'm having a problem with the amount of times that a ChangeListener for a JTabbedPane is called. If the user clicks on a certain tab, a JOptionPane is displayed with a message. However, I can't figure out a easy work around to make it so the listener only executes the code within it only once - I don't want the JOptionPane to come up twice. I tried doing a not-so-great boolean method:

privatevoid setPass(){

if (pass)

pass =false;

else

pass =true;

}

and calling it from within the listener, but I'm having trouble with it. Anyone know how to make it so the lister only fires once, or at least only displays the dialog once? I tried searching the forums using keywords such as "JTabbedPane ChangeListener pass once twice... etc" but it didn't bring anything relevant up. Thanks!

[1096 byte] By [Jason102a] at [2007-11-27 4:09:08]
# 1

I don't see why the ChangeLIstener should be firing twice.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 9:14:32 > top of Java-index,Desktop,Core GUI APIs...
# 2

are you sure you're using a changeListener?

works OK in this

import javax.swing.*;

import java.awt.*;

import javax.swing.event.*;

class Testing

{

public void buildGUI()

{

final JTabbedPane tp = new JTabbedPane();

for(int x = 0; x < 5; x++) tp.addTab(""+(char)(x+65),new JPanel());

JFrame f = new JFrame();

f.getContentPane().add(tp);

f.setSize(300,200);

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

tp.addChangeListener(new ChangeListener(){

public void stateChanged(ChangeEvent ce){

JOptionPane.showMessageDialog(null,tp.getTitleAt(tp.getSelectedIndex()));

}

});

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

post a sample program so we can see what you're doing

(by compiling/running the sample program)

Michael_Dunna at 2007-7-12 9:14:32 > top of Java-index,Desktop,Core GUI APIs...
# 3

Huh you know what it was? What happens in my code is that right before the dialog is displayed, I set the selected tab back to index 0, calling the listener a second time ;) My if statement was actually the main reason why my silly error occured - I forgot to make the change, as I was editing the code, to instead of being if tab index >= 0 to == a certain index! Thanks you guys - I actually saw my error right after I checked your responses, so maybe you guys did help me solve my problem! Thanks again.

Jason102a at 2007-7-12 9:14:32 > top of Java-index,Desktop,Core GUI APIs...