Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: traversing multiple jTabbedPane?

  1. #1
    Junior Member
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
        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.
    Last edited by dewboy3d; May 30th, 2009 at 01:54 PM. Reason: code typo
    --
    DewBoy3d
    Rochester, NY


  2. #2
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default 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

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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:

    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
    Last edited by PekinSOFT.Systems; October 1st, 2009 at 01:57 PM. Reason: A Little More Info...

  4. #4
    Junior Member
    Join Date
    May 2009
    Location
    Rochester, NY
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by dewboy3d; October 2nd, 2009 at 08:17 PM.
    --
    DewBoy3d
    Rochester, NY

Similar Threads

  1. Which interface gives more control on serialization of an object?
    By abhishekraok2003 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 16th, 2009, 10:17 AM
  2. Replies: 2
    Last Post: May 16th, 2009, 05:23 AM
  3. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM
  4. [SOLVED] Need to display multiple images from database on a webpage
    By raghuprasad in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: May 13th, 2009, 03:15 AM