Results 1 to 13 of 13
  1. #1
    Inks is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default Traversing a JTabbedPane

    I have some JButtons on my GUI, to which I have assigned actions to traverse to the next tab within the JTabbedPanes (multiple) which they are contained within.

    I wrote some code to traverse each tab:

    Java Code:
        @Action
        public void NextBookingTab() {
            int index  = newBookingTabbedPane.getSelectedIndex();
            newBookingTabbedPane.setSelectedIndex(index+1);
        }
    
        @Action
        public void PrevBookingTab() {
            int index = newBookingTabbedPane.getSelectedIndex();
            newBookingTabbedPane.setSelectedIndex(index-1);
        }
    
        @Action
        public void NextScheduleTab() {
            int index = newScheduleTabbedPane.getSelectedIndex();
            newScheduleTabbedPane.setSelectedIndex(index+1);
        }
    
        @Action
        public void PrevScheduleTab() {
            int index = newScheduleTabbedPane.getSelectedIndex();
            newScheduleTabbedPane.setSelectedIndex(index-1);
        }
    However, even I can tell that this is pretty damn ugly.

    Can anyone suggest to me a better/more elegant way of moving to the next/previous tab using JButtons, perhaps using some kind of JButton.getParent() method?
    Last edited by Inks; 03-04-2009 at 10:13 PM.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    I dont get it if you must have 2 buttons why you have 4 methods :(

  3. #3
    Inks is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the reply, Webuser!
    Quote Originally Posted by Webuser View Post
    I dont get it if you must have 2 buttons why you have 4 methods :(
    Exactly! Why do I have 4 methods? It's unelegant and bloated!

    I have 4 buttons in all, two in one tabbed pane (newBookingTabbedPane), which traverse that pane, and two in another tabbed pane (newScheduleTabbedPane), which traverse that pane.

    I don't know how to tell the action which jTabbedPane the button is initiating the action on. Therefore, I am currently using an additional set of actions which have the tab's variable name hard-coded. A modified duplicate, if you will.

    What I am trying to do is to condense the "NextBookingTab" and "NextScheduleTab" actions into one "NextTab" action, which will replace them. Likewise, the "Previous[...]" actions need to be combined.

    Thanks again,
    Inks
    Last edited by Inks; 03-06-2009 at 09:29 PM.

  4. #4
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    well you can add a JPanel right in the bottom of your JTabbedPane
    I mean...
    JPanel mainpanel=new JPanel();
    JPanel buttonpanel=new JPanel();
    JTabbedPane tp=new JTabbedPane();
    JButton b1=new JButton("Back"),b2=new JButton("Next");
    buttonpanel.add(b1);
    buttonpanel.add(b2);
    mainpanel.add(tp);
    mainpanel.add(buttonpanel);
    ... so you may have your buttons fixed on unchanged panel. It is more comfortable )))
    By the way, if you have 4 buttons there is no need to have 4 methods. you can heve even 1 method which references button actionCommand or name...

  5. #5
    Inks is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Webuser View Post
    well you can add a JPanel right in the bottom of your JTabbedPane
    I mean...

    ... so you may have your buttons fixed on unchanged panel. It is more comfortable )))
    By the way, if you have 4 buttons there is no need to have 4 methods. you can heve even 1 method which references button actionCommand or name...
    I think I'm starting to see where I'm going with this now.

    I originally tried to do that, but the only way I could think to do it was with parameters- but my problem was I have no idea how to pass such a parameter from the button telling the action which jTabbedPane to increment the index of.

    I'll show you what I mean:
    Java Code:
    @Action
    public void NextBookingTab() {
        int index  = newBookingTabbedPane.getSelectedIndex();
        newBookingTabbedPane.setSelectedIndex(index+1);
    }
    Here, the reference to newBookingTabbedPane is absolute- like I said above, "hard-coded". Also, it is incrementing (index+1) with no way to know if it needs to decrement.

    However, if I wrote something like the following:
    Java Code:
    @action
    public void Tab(jTabbedPane j, boolean a, int t) {
    
    /*
     * j is the jTabbedPane on which I want to perform the action.
     * a states whether to increment or decrement the tab index.
     * t states the number of tabs to jump.
     */
        int index = j.getSelectedIndex();
        if(a = true) {
            j.setSelectedIndex(index+t);    //Increment by t.
        } else {
            j.setSelectedIndex(index-t);    //Decrement by t.
        }
    }
    (I wrote that in the browser, it's probably all wrong!)

    I wouldn't know how to set those parameters so that a command button could pass them to the action. Is this possible?

    Thanks again,
    Inks

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    You may use
    b1.setName("b1");
    b2.setName("b2");
    b1.addMouseListener(new ML());
    b2.addMouseListener(new ML());

    Then you can use JComponent to get buttons names...

    class ML extends MouseAdapter{
    public ML(JComponent c){...}
    public void mouseClicked(MouseEvent e){String name=e.getComponent().getName();
    if(name.equals("b1")){setIndex++;}
    else setIndex--;
    ...
    }
    }
    Figure something out next. I provide only the basic things. How think to develope it on?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you suggesting that the OP add a MouseListener to a JButton in lieu of an ActionListener and to use component name rather than the button's actionCommand String? If so, why?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I know I shouldn't post solutions, but I couldn't help myself here.

    the key to solve this is to get and use the proper references. Here I use a class called MyTabbedPanel that holds a jtabbedpane and has public methods that allow selection of the next or previous tabs (and circles around if at the end).

    I have another class that holds two JButtons labeled "Next" and "Previous". These buttons don't do anything, but the class allows the addition of an ActionListener that gives the buttons their proper actions.

    Finally, I add both the JTabbedPane and the button panel into another JPanel held by a Control class and in this class create the actionlistener that will tie everything together:

    MyTabbedPanel.java
    Java Code:
    import java.awt.Dimension;
    import java.awt.Font;
    
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JTabbedPane;
    import javax.swing.SwingConstants;
    
    public class MyTabbedPanel
    {
      private static final Dimension PREFERRED_SIZE = new Dimension(400, 300);
      private JTabbedPane tabbedpane = new JTabbedPane();
      
      public MyTabbedPanel(int tabCount)
      {
        for (int i = 0; i < tabCount; i++)
        {
          JLabel label = new JLabel("In Tab Number "  + i, 
              SwingConstants.CENTER);
          label.setFont(label.getFont().deriveFont(Font.BOLD, 24));
          label.setOpaque(true);
          tabbedpane.add("TAB " + i, label);
        }
        
        tabbedpane.setPreferredSize(PREFERRED_SIZE);
      }
      
      public int getTabCount()
      {
        return tabbedpane.getTabCount();
      }
      
      public void setSelectedIndex(int index)
      {
        if (index < 0 || index >= getTabCount())
        {
          throw new IndexOutOfBoundsException("No Tab exists for index: " + index);
        }
        tabbedpane.setSelectedIndex(index);
      }
      
      public int getSelectedIndex()
      {
        return tabbedpane.getSelectedIndex();
      }
      
      public void setNextIndex()
      {
        int index = (getSelectedIndex() + 1) % getTabCount();
        setSelectedIndex(index);
      }
      
      public void setPreviousIndex()
      {
        // add an extra tab count to avoid a negative result here
        // if original index was 0
        int index = (getSelectedIndex() - 1 + getTabCount()) % getTabCount();
        setSelectedIndex(index);
      }
    
      public JComponent getMainComponent()
      {
        return tabbedpane;
      }
    }
    MyTraversal.java
    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    
    public class MyTraversal
    {
      public static final String NEXT = "Next";
      public static final String PREVIOUS = "Previous";
      private static final String[] BUTTON_STRINGS = 
      {
        PREVIOUS, NEXT
      };
      private JPanel mainPanel = new JPanel();
      private List<ActionListener> listenerList = 
        new ArrayList<ActionListener>();
    
      public MyTraversal()
      {
        mainPanel.setLayout(new GridLayout(1, 0, 20, 10));
        for (String btnString : BUTTON_STRINGS)
        {
          JButton button = new JButton(btnString);
          mainPanel.add(button);
          button.addActionListener(new ActionListener()
          {
            public void actionPerformed(ActionEvent e)
            {
              for (ActionListener al : listenerList)
              {
                al.actionPerformed(e);
              }
            }
          });
        }
      }
      
      public void addActionListener(ActionListener al)
      {
        listenerList.add(al);
      }
    
      public JComponent getMainComponent()
      {
        return mainPanel;
      }
    }
    MyTraversalControl.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    
    public class MyTraversalControl
    {
      private static final int TABBED_COUNT = 6;
      private JPanel mainPanel = new JPanel();
      private MyTabbedPanel mytab = new MyTabbedPanel(TABBED_COUNT);
      private MyTraversal traversalPane = new MyTraversal();
    
      public MyTraversalControl()
      {
        traversalPane.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            String command = e.getActionCommand();
            if (command.equals(MyTraversal.NEXT))
            {
              mytab.setNextIndex();
            }
            else if (command.equals(MyTraversal.PREVIOUS))
            {
              mytab.setPreviousIndex();
            }
          }
        });
        
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(mytab.getMainComponent(), BorderLayout.CENTER);
        mainPanel.add(traversalPane.getMainComponent(), BorderLayout.SOUTH);
      }
    
      public JComponent getMainComponent()
      {
        return mainPanel;
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("MyControl");
        frame.getContentPane().add(new MyTraversalControl().getMainComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  9. #9
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Are you suggesting that the OP add a MouseListener to a JButton in lieu of an ActionListener and to use component name rather than the button's actionCommand String? If so, why?
    It is more comfortable if you plan to use the method code in many situations but the only one code version )

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It is more comfortable if you plan to use the method code in many situations but the only one code version )
    ??? I'm not quite sure I understand this statement

  11. #11
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    ??? I'm not quite sure I understand this statement
    I am not sure you should so )

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Webuser: I'm going to conclude this discussion with the assumption that English is not your primary language, and that this explains your difficulties in communicating. Best of luck.

  13. #13
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    I am not suggesting a whole code tutorials ) It is not a shop but forum ...
    I am writing as
    I am not sure you should so )
    cause I am talking to Inks here only ))) At least it is Inks' topic...

Similar Threads

  1. How to use JTabbedPane
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 08:50 PM
  2. Replies: 1
    Last Post: 03-25-2008, 06:06 AM
  3. Traversing a directory
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 10:33 AM
  4. Help with JTabbedPane
    By lenny in forum SWT / JFace
    Replies: 1
    Last Post: 08-07-2007, 07:18 AM
  5. Same component on all JTabbedPane
    By java_novice in forum AWT / Swing
    Replies: 4
    Last Post: 08-06-2007, 10:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •