0

Following is snap of my ChangeListner method, when i run my project, before i actually reach to tabbedPan the JOptionDialog pops out(actually along with jframe loads)! My actual purpose is i want to listen the changing of tabs so that i can load some contents on that tab from database! help me

jTabbedPane1.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent evt) {
            jtabpanChangeListner(evt);
        }
    });
private void jtabpanChangeListner(ChangeEvent evt) {                                      
    // TODO add your handling code here:

    int index = jTabbedPane1.getSelectedIndex();
    String msg =  jTabbedPane1.getTitleAt(index);
    System.out.println("Tab changed to: " +msg);
   JOptionPane.showMessageDialog(jTabbedPane1,"hello change me do you?+");}
dip
  • 3,548
  • 3
  • 24
  • 36

1 Answers1

1

JTabbedPane sends ChangeEvent's when a selected tab changes. In particular, when the JTabbedPane is empty and you add a first tab the JTabbedPane will send the ChangeEvent meaning that selected tab changed from null to something.

You need to either take into account that first change event, or to add the ChangeListener after you added the first tab into the JTabbedPane.

Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52
  • thnks for your response ! but please, make more clear, i am new to programming field. i mm using netbeans ! and some codes are not allowed to alter ! – dip Mar 14 '14 at 08:48
  • thanks ! it worked ! all was problem with autogenerated code of netbeans! i did change the sequence of declaration of method ! but can u please make clear about first idea of yours @@change event @@ thanks in advance ! – dip Mar 14 '14 at 09:13