Question

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?+");}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top