How to check if the tab selected is changed in java

五迷三道 提交于 2019-12-10 11:55:39

问题


As far as i have seen the event:

(1)     private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {}

Checks whether a new tab is added or an exiting tab is deleted or not.

On googling , i found this code:

(2)     ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
        // my code       
        }
        };
        jTabbedPane1.addChangeListener(changeListener);

I guess since it uses stateChanged event , it should do what the same a my first code. By t way even after using both the codes i could not get the required resuts(ie An event that could be invoked when user changes the tab).

Can anyone suggest me a good event [i am using netbeans GUI environment] for effective action. (I dont want any mouseEvents)

Edit:

I want the following code to be excecuted if the tab changes:

String send3=( jTabbedPane1.getSelectedComponent().getComponentAt(0,0)).getName(); 

The above code dynamically gets the name of jTextarea (in the current tab) which is created dynamically in the jTabbedPanel.


回答1:


I just checked my own source code where addChangeListener() works fine. The event is fired whenever the tab is changed by the user or programatically. In stateChanged() itself, the now selected tab is determined by

 JTabbedPane p = (JTabbedPane)e.getSource();
 int idx = p.getSelectedIndex();


来源:https://stackoverflow.com/questions/17427583/how-to-check-if-the-tab-selected-is-changed-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!