I have created a gui, this gui uses a jtabbed pane. I have an array which contains the names of each tab. I use
for (String locale : langList){
LocaleTab tab = new LocaleTab();
tab.buildTab(locale);
localeTabs.add(locale, tab.getTabPanel());
}
in my main class to create the tabs. On each tab I have a jtextarea and a jtable as well as two buttons, one for running an action on that specific tab and another for clearing the contents in the table and textarea of that locale. This works perfectly.
My problem is, I can run and clear all the contents on each tab individually, I can’t figure out how to do this for all tabs at once. I have a main clear and run button, which when pressed will clear all textarea and table data on all locales or run the action on all locales at once.
I thought I could do the following in an action listener for my clear button:
for (String locale : langList){
LocaleTab tab = new LocaleTab();
tab.clearTab();
}
but it didn’t work. Do I need to pass the object tab to my method to work on the tabs all at once?
Your suggested solution does not work since you create new
LocaleTabobjects in your action listener. So you will be clearing the tab ofLocaleTabinstances which are not present in your UI (and which no longer exists as soon as you leave that method due to the scope of that variable).Instead your listener should have access to the
LocaleTabinstances which you originally created.Note
Small remark on seeing this code. Why would you create a UI for all supported languages at the same time. An end-user typically uses the application in one language, so why do you need that extra UI. Why not only creating UI for the appropriate language, hence bypassing the problem you currently face