Results 1 to 13 of 13
Thread: Traversing a JTabbedPane
- 03-04-2009, 10:01 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 6
- Rep Power
- 0
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); }
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.
- 03-06-2009, 08:43 AM #2
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
I dont get it if you must have 2 buttons why you have 4 methods :(
- 03-06-2009, 08:23 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 6
- Rep Power
- 0
Thanks for the reply, Webuser!
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,
InksLast edited by Inks; 03-06-2009 at 09:29 PM.
- 03-08-2009, 10:00 AM #4
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
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);
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...
- 03-08-2009, 11:46 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 6
- Rep Power
- 0
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); }
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 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
- 03-09-2009, 07:04 AM #6
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
You may use
b1.setName("b1");
b2.setName("b2");
b1.addMouseListener(new ML());
b2.addMouseListener(new ML());
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--;
...
}
}
-
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?
-
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; } }
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; } }
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(); } }); } }
- 03-10-2009, 05:23 AM #9
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
- It is more comfortable if you plan to use the method code in many situations but the only one code version )
- 03-11-2009, 01:05 AM #11
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
-
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.
- 03-11-2009, 06:15 AM #13
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
Similar Threads
-
How to use JTabbedPane
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 08:50 PM -
Traversing through a stack of objects, and puttin them info in an array
By szimme101 in forum New To JavaReplies: 1Last Post: 03-25-2008, 06:06 AM -
Traversing a directory
By Java Tip in forum Java TipReplies: 0Last Post: 01-14-2008, 10:33 AM -
Help with JTabbedPane
By lenny in forum SWT / JFaceReplies: 1Last Post: 08-07-2007, 07:18 AM -
Same component on all JTabbedPane
By java_novice in forum AWT / SwingReplies: 4Last Post: 08-06-2007, 10:09 AM
Bookmarks