traversing multiple jTabbedPane?
I have 3 jTabbedPane in my application which visibility switches based on other selections in the app. what I want to do is have a single set of navigation buttons (back) (next) that will traverse the visible jTabbedPane. I can do it with 3 sets of essentially the same code but am trying to keep this app as small as possible. how can i reference the visible jTabbedPane in my traversal code without writing the same code for each jTabbedPane?
Here is the code that traverses the tabs
Code :
public void switchTabs(String action) {
Integer tabCount = tabContainer.getTabCount() - 1;
Integer currentTab = tabContainer.getSelectedIndex();
if (action.equals("back")) {
if (currentTab > 0) {
nextButton.setEnabled(true);
currentTab -= 1;
while (!tabContainer.isEnabledAt(currentTab) && currentTab > 0){
currentTab -= 1;
}
tabContainer.setSelectedIndex(currentTab);
}
} else if (action.equals("next")) {
if (currentTab < tabCount) {
backButton.setEnabled(true);
currentTab += 1;
while (!tabContainer.isEnabledAt(currentTab) && currentTab < tabCount){
currentTab += 1;
}
tabContainer.setSelectedIndex(currentTab);
}
} else if (action.equals("reset")) {
tabContainer.setSelectedIndex(0);
currentTab = 0;
}
if (currentTab == tabCount){
nextButton.setEnabled(false);
backButton.setEnabled(true);
} else if (currentTab == 0){
nextButton.setEnabled(true);
backButton.setEnabled(false);
} else {
nextButton.setEnabled(true);
backButton.setEnabled(true);
}
//calculate();
}
If I could pass the jTabbedPane I want to work with into switchTabs(), it would be perfect but I do not know how to do that and haven't been able to find any information through searching. Any ideas would be helpful. Thank you.
Re: traversing multiple jTabbedPane?
If I understand your problem right then what you want to do is to update your selections once someone presses "back" or "next" buttons, and vise versa (enable / disable buttons once someone changes selection in your application), and ofcourse show the right tabs.
If so, then one possible solution to your problem would be a MVC (Model View Controller) pattern.
Take a look at it here: Model–view–controller - Wikipedia, the free encyclopedia
If you need help with it give me a shout, I will gladly help you.
If that's not the problem you are having you can try to explain it differently or wait maybe someone else can help you with that.
-Dalisra
Re: traversing multiple jTabbedPane?
dewboy3d,
One thing that you could do is move your redundant code into functions. For example, you could have a function called `setButtons', which sets the enabled state of the buttons as is appropriate in a given situation. For example, a cleaner version of your sample code would be:
Code :
public void switchTabs ( String action ) {
if ( action.equalsIgnoreCase("back") {
tabContainer.setSelectedIndex( tabContainer.getSelectedIndex() - 1 );
setButtons();
} else if ( action.equalsIgnoreCase("next) ) {
tabContainer.setSelectedIndex( tabContainer.getSelectedIndex() + 1 );
setButtons();
} else if ( action.equalsIgnoreCase("reset") ) {
tabContainer.setSelectedIndex( 0 );
setButtons();
} // End of action decision.
// Whatever else you need to do here...
} // End of switchTabs() function.
/**
* Sets the enabled state of the navigation buttons based upon the current situation.
* NOTE: This function also deals with the situation when there is only one tab on
* the TabbedPane.
*/
private void setButtons () {
if ( tabContainer.getSelectedIndex() == 0 ) {
// We know that the first index of anything in Java is 0...
backButton.setEnabled(false);
} else {
// If we are not on the first tab, we need the ability to move back.
backButton.setEnabled(true);
} // End of dealing with the back button.
if ( tabContainer.getSelectedIndex() == ( tabContainer.getTabCount() - 1) ) {
// We know that we are on the last tab.
nextButton.setEndabled(false);
} else {
// If we are not on the last tab, we need the ability to move forward.
nextButton.setEndabled(true);
} // End of dealing with the next button.
} // End of setButton() function.
This is a little cleaner and you only have to write the button state code once. Of course, you could make it a little easier on yourself, and speed up your code a bit (though it wouldn't be noticeable in this example) by setting up symbolic constants for your actions, which I will do in my next example:
Code :
private static final int BACK = 0;
private static final int NEXT = 1;
private static final int RESET = 2;
public void switchTabs ( int action ) {
switch ( action ) {
case BACK:
tabContainer.setSelectedIndex( tabContainer.getSelectedIndex() - 1 );
setButtons();
break;
case NEXT:
tabContainer.setSelectedIndex( tabContainer.getSelectedIndex() + 1 );
setButtons();
break;
case RESET:
tabContainer.setSelectedIndex( 0 );
setButtons();
break;
} // End of switch for action.
// Whatever else you need to do here...
} // End of switchTabs() function.
/**
* Sets the enabled state of the navigation buttons based upon the current situation.
* NOTE: This function also deals with the situation when there is only one tab on
* the TabbedPane.
*/
private void setButtons () {
if ( tabContainer.getSelectedIndex() == 0 ) {
// We know that the first index of anything in Java is 0...
backButton.setEnabled(false);
} else {
// If we are not on the first tab, we need the ability to move back.
backButton.setEnabled(true);
} // End of dealing with the back button.
if ( tabContainer.getSelectedIndex() == ( tabContainer.getTabCount() - 1) ) {
// We know that we are on the last tab.
nextButton.setEndabled(false);
} else {
// If we are not on the last tab, we need the ability to move forward.
nextButton.setEndabled(true);
} // End of dealing with the next button.
} // End of setButton() function.
There are other things that you could do, but I will leave them as an exercise for you to figure out. I have provided you with examples of the two best things that you could do to clean up your code a little and one of them will also speed up execution a bit, since a switch()...case is quite a bit faster than an if()...else.
Hope this reply helps you out.
Cheers,
Sean C.
PekinSOFT Systems
EDIT: I just happened to think that I didn't mention the fact that I dropped the local variables that your were using. The reason why is that they were redundant and a waste of memory. Again, with this short example, that waste of memory isn't really an issue, but when this code becomes part of a large, complex project, every little bit of memory saved is a blessing.
The reason that I call your local variables redundant is that the tabContainer object is already storing the values that you are putting in the local variables. Plus, as soon as that function completes, those variables are destroyed. You are incrementing and decrementing them in your function as if they are going to be around for a while, but they won't, so you are just introducing a potential memory leak and putting an undue burden on yourself...not to mention that it's a good way to introduce a hard to find logic error.
Instead of using local variables like you are, use the property getters and setters of the object your are manipulating. This way, you are not increasing memory usage, creating more work for yourself or creating a good way for your code to break with one slight change to the logic of the rest of the project.
0---0 Just some food for thought 0---0
Cheers
Re: traversing multiple jTabbedPane?
Sean,
Thanks for the reply. As it happens, I am still working on this project. I have already discovered some of the improvements you suggested. I still consider myself a Java beginner but have come quite a long way since my original post here. My switchTabs function now controls a set of 5 overlapping (hidden) tabContainers which may have disabled tabs and each day I'm finding ways to make my code smaller. Thanks again for the reply to such an old topic.