* javax/swing/JToggleButton.java (ToggleButtonModel):
[official-gcc.git] / libjava / javax / swing / JTabbedPane.java
blob3c75461f01001a8de5acea2b16665a783f6afab4
1 /* JTabbedPane.java --
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing;
40 import java.awt.Color;
41 import java.awt.Component;
42 import java.awt.Point;
43 import java.awt.Rectangle;
44 import java.awt.event.MouseEvent;
45 import java.io.Serializable;
46 import java.util.Vector;
47 import javax.accessibility.Accessible;
48 import javax.accessibility.AccessibleContext;
49 import javax.accessibility.AccessibleRole;
50 import javax.accessibility.AccessibleSelection;
51 import javax.accessibility.AccessibleStateSet;
52 import javax.accessibility.AccessibleValue;
53 import javax.swing.event.ChangeEvent;
54 import javax.swing.event.ChangeListener;
55 import javax.swing.event.EventListenerList;
56 import javax.swing.plaf.TabbedPaneUI;
57 import javax.swing.plaf.UIResource;
60 /**
61 * <p>
62 * This is a container for components. One component is displayed at a time.
63 * Users can switch between components by clicking on tabs.
64 * </p>
66 * <p>
67 * Tabs can be oriented in several ways. They can be above, below, left and
68 * right of the component. Tabs can either wrap around (by creating multiple
69 * rows of tabs) or they can be scrolled (where only a subset of the tabs
70 * can be seen at once). More tabs can be added by calling the
71 * add/addTab/insertTab methods.
72 * </p>
74 public class JTabbedPane extends JComponent implements Serializable,
75 Accessible,
76 SwingConstants
78 /**
79 * DOCUMENT ME!
81 protected class AccessibleJTabbedPane extends JComponent.AccessibleJComponent
82 implements AccessibleSelection, ChangeListener
84 /**
85 * Creates a new AccessibleJTabbedPane object.
87 * @param c DOCUMENT ME!
89 public AccessibleJTabbedPane(JTabbedPane c)
91 super(c);
94 /**
95 * DOCUMENT ME!
97 * @param e DOCUMENT ME!
99 public void stateChanged(ChangeEvent e)
104 * DOCUMENT ME!
106 * @return DOCUMENT ME!
108 public AccessibleRole getAccessibleRole()
110 return null;
114 * DOCUMENT ME!
116 * @return DOCUMENT ME!
118 public int getAccessibleChildrenCount()
120 return 0;
124 * DOCUMENT ME!
126 * @param i DOCUMENT ME!
128 * @return DOCUMENT ME!
130 public Accessible getAccessibleChild(int i)
132 return null;
136 * DOCUMENT ME!
138 * @return DOCUMENT ME!
140 public AccessibleSelection getAccessibleSelection()
142 return null;
146 * DOCUMENT ME!
148 * @param p DOCUMENT ME!
150 * @return DOCUMENT ME!
152 public Accessible getAccessibleAt(Point p)
154 return null;
158 * DOCUMENT ME!
160 * @return DOCUMENT ME!
162 public int getAccessibleSelectionCount()
164 return 0;
168 * DOCUMENT ME!
170 * @param i DOCUMENT ME!
172 * @return DOCUMENT ME!
174 public Accessible getAccessibleSelection(int i)
176 return null;
180 * DOCUMENT ME!
182 * @param i DOCUMENT ME!
184 * @return DOCUMENT ME!
186 public boolean isAccessibleChildSelected(int i)
188 return false;
192 * DOCUMENT ME!
194 * @param i DOCUMENT ME!
196 public void addAccessibleSelection(int i)
201 * DOCUMENT ME!
203 * @param i DOCUMENT ME!
205 public void removeAccessibleSelection(int i)
210 * DOCUMENT ME!
212 public void clearAccessibleSelection()
217 * DOCUMENT ME!
219 public void selectAllAccessibleSelection()
225 * A helper class that listens for changes to the model.
227 protected class ModelListener implements ChangeListener, Serializable
230 * This method is called whenever the model is changed.
232 * @param e The ChangeEvent that is passed from the model.
234 public void stateChanged(ChangeEvent e)
236 // Propagate to our listeners.
237 fireStateChanged();
242 * A private class that holds all the information for each tab.
244 private class Page
246 /** The tooltip string. */
247 private String tip;
249 /** The component associated with the tab. */
250 private Component component;
252 /** The active icon associated with the tab. */
253 private transient Icon icon;
255 /** The disabled icon associated with the tab. */
256 private transient Icon disabledIcon;
258 /** The tab's enabled status. */
259 private transient boolean enabled = true;
261 /** The string painted on the tab. */
262 private transient String title;
264 /** The background color of the tab. */
265 private transient Color bg;
267 /** The foreground color of the tab. */
268 private transient Color fg;
270 /** The mnemonic associated with the tab. */
271 private transient int mnemonicKey;
273 /** The index of the underlined character in the string. */
274 private transient int underlinedChar = -1;
277 * Creates a new data storage for the tab.
279 * @param title The string displayed on the tab.
280 * @param icon The active icon displayed on the tab.
281 * @param component The component associated with the tab.
282 * @param tip The tooltip associated with the tab.
284 protected Page(String title, Icon icon, Component component, String tip)
286 this.title = title;
287 this.icon = icon;
288 this.component = component;
289 this.tip = tip;
293 * This method returns the component associated with the tab.
295 * @return The component associated with the tab.
297 public Component getComponent()
299 return component;
303 * This method sets the component associated with the tab.
305 * @param c The component associated with the tab.
307 public void setComponent(Component c)
309 this.component = c;
313 * This method returns the tooltip string.
315 * @return The tooltip string.
317 public String getTip()
319 return tip;
323 * This method sets the tooltip string.
325 * @param tip The tooltip string.
327 public void setTip(String tip)
329 this.tip = tip;
333 * This method returns the background color.
335 * @return The background color.
337 public Color getBackground()
339 return bg;
343 * This method sets the background color.
345 * @param background The background color.
347 public void setBackground(Color background)
349 bg = background;
353 * This method returns the foreground color.
355 * @return The foreground color.
357 public Color getForeground()
359 return fg;
363 * This method sets the foreground color.
365 * @param foreground The foreground color.
367 public void setForeground(Color foreground)
369 fg = foreground;
373 * This method returns the title associated with the tab.
375 * @return The title of the tab.
377 public String getTitle()
379 return title;
382 private static final long serialVersionUID = 1614381073220130939L;
385 * This method sets the title of the tab.
387 * @param text The title of the tab.
389 public void setTitle(String text)
391 title = text;
392 if (title != null && title.length() <= underlinedChar)
393 setDisplayedMnemonicIndex(title.length() - 1);
397 * This method returns the active icon.
399 * @return The active icon.
401 public Icon getIcon()
403 return icon;
407 * This method sets the active icon.
409 * @param icon The active icon.
411 public void setIcon(Icon icon)
413 this.icon = icon;
417 * This method returns the disabled icon.
419 * @return The disabled icon.
421 public Icon getDisabledIcon()
423 if (disabledIcon == null && icon instanceof ImageIcon)
424 setDisabledIcon(icon);
425 return disabledIcon;
429 * This method sets the disabled icon.
431 * @param disabledIcon The disabled icon.
433 public void setDisabledIcon(Icon disabledIcon)
435 this.disabledIcon = disabledIcon;
439 * This method returns whether the tab is enabled.
441 * @return Whether the tab is enabled.
443 public boolean isEnabled()
445 return enabled;
449 * This method sets whether the tab is enabled.
451 * @param enabled Whether this tab is enabled.
453 public void setEnabled(boolean enabled)
455 this.enabled = enabled;
459 * This method returns the mnemonic.
461 * @return The mnemonic.
463 public int getMnemonic()
465 return (int) mnemonicKey;
469 * This method sets the mnemonic. If the title is set, it will update the
470 * mnemonicIndex.
472 * @param key The mnemonic.
474 public void setMnemonic(int key)
476 setMnemonic((char) key);
480 * This method sets the mnemonic. If the title is set, it will update the
481 * mnemonicIndex.
483 * @param aChar The mnemonic.
485 public void setMnemonic(char aChar)
487 mnemonicKey = aChar;
488 if (title != null)
489 setDisplayedMnemonicIndex(title.indexOf(mnemonicKey));
493 * This method returns the mnemonicIndex.
495 * @return The mnemonicIndex.
497 public int getDisplayedMnemonicIndex()
499 return underlinedChar;
503 * This method sets the mnemonicIndex.
505 * @param index The mnemonicIndex.
507 * @throws IllegalArgumentException If index less than -1 || index greater
508 * or equal to title.length.
510 public void setDisplayedMnemonicIndex(int index)
511 throws IllegalArgumentException
513 if (index < -1 || title != null && index >= title.length())
514 throw new IllegalArgumentException();
516 if (title == null || title.charAt(index) != mnemonicKey)
517 index = -1;
519 underlinedChar = index;
523 /** Fired in a PropertyChangeEvent when the "model" property changes. */
524 public static final String MODEL_CHANGED_PROPERTY = "model";
527 * Fired in a PropertyChangeEvent when the "tabPlacement" property changes.
529 public static final String TAB_PLACEMENT_CHANGED_PROPERTY = "tabPlacement";
532 * Fired in a PropertyChangeEvent when the "tabLayoutPolicy" property
533 * changes.
535 public static final String TAB_LAYOUT_POLICY_CHANGED_PROPERTY = "tabLayoutPolicy";
537 /** The changeEvent used to fire changes to listeners. */
538 protected ChangeEvent changeEvent;
540 /** The listener that listens to the model. */
541 protected ChangeListener changeListener;
543 /** The model that describes this JTabbedPane. */
544 protected SingleSelectionModel model;
546 /** Indicates that the TabbedPane is in scrolling mode. */
547 public static int SCROLL_TAB_LAYOUT = 1;
549 /** Indicates that the TabbedPane is in wrap mode. */
550 public static int WRAP_TAB_LAYOUT = 0;
552 /** The current tabPlacement of the TabbedPane. */
553 protected int tabPlacement = SwingConstants.TOP;
555 /** The current tabLayoutPolicy of the TabbedPane. */
556 private transient int layoutPolicy;
558 /** The list of tabs associated with the TabbedPane. */
559 transient Vector tabs = new Vector();
562 * Creates a new JTabbedPane object with tabs on top and using wrap tab
563 * layout.
565 public JTabbedPane()
567 this(SwingConstants.TOP, WRAP_TAB_LAYOUT);
571 * Creates a new JTabbedPane object using wrap tab layout and the given
572 * tabPlacement.
574 * @param tabPlacement Where the tabs will be placed.
576 public JTabbedPane(int tabPlacement)
578 this(tabPlacement, WRAP_TAB_LAYOUT);
582 * Creates a new JTabbedPane object with the given tabPlacement and
583 * tabLayoutPolicy.
585 * @param tabPlacement Where the tabs will be placed.
586 * @param tabLayoutPolicy The way tabs will be placed.
588 * @throws IllegalArgumentException If tabLayoutPolicy or tabPlacement are
589 * not valid.
591 public JTabbedPane(int tabPlacement, int tabLayoutPolicy)
593 if (tabPlacement != TOP && tabPlacement != BOTTOM && tabPlacement != RIGHT
594 && tabPlacement != LEFT)
595 throw new IllegalArgumentException("tabPlacement is not valid.");
596 if (tabLayoutPolicy != SCROLL_TAB_LAYOUT
597 && tabLayoutPolicy != WRAP_TAB_LAYOUT)
598 throw new IllegalArgumentException("tabLayoutPolicy is not valid.");
599 this.tabPlacement = tabPlacement;
600 layoutPolicy = tabLayoutPolicy;
602 changeEvent = new ChangeEvent(this);
603 changeListener = createChangeListener();
605 model = new DefaultSingleSelectionModel();
606 model.addChangeListener(changeListener);
608 updateUI();
612 * This method returns the UI used to display the JTabbedPane.
614 * @return The UI used to display the JTabbedPane.
616 public TabbedPaneUI getUI()
618 return (TabbedPaneUI) ui;
622 * This method sets the UI used to display the JTabbedPane.
624 * @param ui The UI used to display the JTabbedPane.
626 public void setUI(TabbedPaneUI ui)
628 super.setUI(ui);
632 * This method restores the UI to the defaults given by the UIManager.
634 public void updateUI()
636 setUI((TabbedPaneUI) UIManager.getUI(this));
637 invalidate();
641 * This method returns a string identifier that is used to determine which
642 * UI will be used with the JTabbedPane.
644 * @return A string identifier for the UI.
646 public String getUIClassID()
648 return "TabbedPaneUI";
652 * This method creates a ChangeListener that is used to listen to the model
653 * for events.
655 * @return A ChangeListener to listen to the model.
657 protected ChangeListener createChangeListener()
659 return new ModelListener();
663 * This method adds a ChangeListener to the JTabbedPane.
665 * @param l The ChangeListener to add.
667 public void addChangeListener(ChangeListener l)
669 listenerList.add(ChangeListener.class, l);
673 * This method removes a ChangeListener to the JTabbedPane.
675 * @param l The ChangeListener to remove.
677 public void removeChangeListener(ChangeListener l)
679 listenerList.remove(ChangeListener.class, l);
683 * This method fires a ChangeEvent to all the JTabbedPane's ChangeListeners.
685 protected void fireStateChanged()
687 Object[] changeListeners = listenerList.getListenerList();
688 if (changeEvent == null)
689 changeEvent = new ChangeEvent(this);
690 for (int i = changeListeners.length - 2; i >= 0; i -= 2)
692 if (changeListeners[i] == ChangeListener.class)
693 ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent);
698 * This method returns all ChangeListeners registered with the JTabbedPane.
700 * @return The ChangeListeners registered with the JTabbedPane.
702 public ChangeListener[] getChangeListeners()
704 return (ChangeListener[]) super.getListeners(ChangeListener.class);
708 * This method returns the model used with the JTabbedPane.
710 * @return The JTabbedPane's model.
712 public SingleSelectionModel getModel()
714 return model;
718 * This method changes the model property of the JTabbedPane.
720 * @param model The new model to use with the JTabbedPane.
722 public void setModel(SingleSelectionModel model)
724 if (model != this.model)
726 SingleSelectionModel oldModel = this.model;
727 this.model.removeChangeListener(changeListener);
728 this.model = model;
729 this.model.addChangeListener(changeListener);
730 firePropertyChange(MODEL_CHANGED_PROPERTY, oldModel, this.model);
735 * This method returns the tabPlacement.
737 * @return The tabPlacement used with the JTabbedPane.
739 public int getTabPlacement()
741 return tabPlacement;
745 * This method changes the tabPlacement property of the JTabbedPane.
747 * @param tabPlacement The tabPlacement to use.
749 * @throws IllegalArgumentException If tabPlacement is not one of TOP,
750 * BOTTOM, LEFT, or RIGHT.
752 public void setTabPlacement(int tabPlacement)
754 if (tabPlacement != TOP && tabPlacement != BOTTOM && tabPlacement != RIGHT
755 && tabPlacement != LEFT)
756 throw new IllegalArgumentException("tabPlacement is not valid.");
757 if (tabPlacement != this.tabPlacement)
759 int oldPlacement = this.tabPlacement;
760 this.tabPlacement = tabPlacement;
761 firePropertyChange(TAB_PLACEMENT_CHANGED_PROPERTY, oldPlacement,
762 this.tabPlacement);
767 * This method returns the tabLayoutPolicy.
769 * @return The tabLayoutPolicy.
771 public int getTabLayoutPolicy()
773 return layoutPolicy;
777 * This method changes the tabLayoutPolicy property of the JTabbedPane.
779 * @param tabLayoutPolicy The tabLayoutPolicy to use.
781 * @throws IllegalArgumentException If tabLayoutPolicy is not one of
782 * SCROLL_TAB_LAYOUT or WRAP_TAB_LAYOUT.
784 public void setTabLayoutPolicy(int tabLayoutPolicy)
786 if (tabLayoutPolicy != SCROLL_TAB_LAYOUT
787 && tabLayoutPolicy != WRAP_TAB_LAYOUT)
788 throw new IllegalArgumentException("tabLayoutPolicy is not valid.");
789 if (tabLayoutPolicy != layoutPolicy)
791 int oldPolicy = layoutPolicy;
792 layoutPolicy = tabLayoutPolicy;
793 firePropertyChange(TAB_LAYOUT_POLICY_CHANGED_PROPERTY, oldPolicy,
794 layoutPolicy);
799 * This method returns the index of the tab that is currently selected.
801 * @return The index of the selected tab.
803 public int getSelectedIndex()
805 return model.getSelectedIndex();
809 * This method checks the index.
811 * @param index The index to check.
813 private void checkIndex(int index, int start, int end)
815 if (index < start || index >= end)
816 throw new IndexOutOfBoundsException("Index < " + start + " || Index >= " + end);
820 * This method sets the selected index. This method
821 * will hide the old component and show the new component.
823 * @param index The index to set it at.
825 public void setSelectedIndex(int index)
827 checkIndex(index, -1, tabs.size());
828 if (index != getSelectedIndex())
830 if (getSelectedIndex() != -1)
831 getSelectedComponent().hide();
832 if (index != -1)
833 getComponentAt(index).show();
835 model.setSelectedIndex(index);
839 * This method returns the component at the selected index.
841 * @return The component at the selected index.
843 public Component getSelectedComponent()
845 return getComponentAt(getSelectedIndex());
849 * This method sets the component at the selected index.
851 * @param c The component associated with the selected index.
853 public void setSelectedComponent(Component c)
855 if (c.getParent() == this)
856 setSelectedIndex(indexOfComponent(c));
857 else
858 setComponentAt(getSelectedIndex(), c);
862 * This method inserts tabs into JTabbedPane. This includes
863 * adding the component to the JTabbedPane and hiding it.
865 * @param title The title of the tab.
866 * @param icon The tab's icon.
867 * @param component The component associated with the tab.
868 * @param tip The tooltip for the tab.
869 * @param index The index to insert the tab at.
871 public void insertTab(String title, Icon icon, Component component,
872 String tip, int index)
874 Page p = new Page(title, icon, component, tip);
875 tabs.insertElementAt(p, index);
877 // Hide the component so we don't see it. Do it before we parent it
878 // so we don't trigger a repaint.
879 component.hide();
880 super.add(component);
882 if (getSelectedIndex() == -1)
883 setSelectedIndex(0);
885 layout();
889 * This method adds a tab to the JTabbedPane.
891 * @param title The title of the tab.
892 * @param icon The icon for the tab.
893 * @param component The associated component.
894 * @param tip The associated tooltip.
896 public void addTab(String title, Icon icon, Component component, String tip)
898 insertTab(title, icon, component, tip, tabs.size());
902 * This method adds a tab to the JTabbedPane.
904 * @param title The title of the tab.
905 * @param icon The icon for the tab.
906 * @param component The associated component.
908 public void addTab(String title, Icon icon, Component component)
910 insertTab(title, icon, component, null, tabs.size());
914 * This method adds a tab to the JTabbedPane.
916 * @param title The title of the tab.
917 * @param component The associated component.
919 public void addTab(String title, Component component)
921 insertTab(title, null, component, null, tabs.size());
925 * This method adds a tab to the JTabbedPane.
926 * The title of the tab is the Component's name.
927 * If the Component is an instance of UIResource, it doesn't
928 * add the tab and instead add the component directly to the
929 * JTabbedPane.
931 * @param component The associated component.
933 * @return The Component that was added.
935 public Component add(Component component)
937 if (component instanceof UIResource)
938 super.add(component);
939 else
940 insertTab(component.getName(), null, component, null, tabs.size());
941 return component;
945 * This method adds a tab to the JTabbedPane.
946 * If the Component is an instance of UIResource, it doesn't
947 * add the tab and instead add the component directly to the
948 * JTabbedPane.
950 * @param title The title of the tab.
951 * @param component The associated component.
953 * @return The Component that was added.
955 public Component add(String title, Component component)
957 if (component instanceof UIResource)
958 super.add(component);
959 else
960 insertTab(title, null, component, null, tabs.size());
961 return component;
965 * This method adds a tab to the JTabbedPane.
966 * If the Component is an instance of UIResource, it doesn't
967 * add the tab and instead add the component directly to the
968 * JTabbedPane.
970 * @param component The associated component.
971 * @param index The index to insert the tab at.
973 * @return The Component that was added.
975 public Component add(Component component, int index)
977 if (component instanceof UIResource)
978 super.add(component);
979 else
980 insertTab(component.getName(), null, component, null, index);
981 return component;
985 * This method adds a tab to the JTabbedPane.
986 * If the Component is an instance of UIResource, it doesn't
987 * add the tab and instead add the component directly to the
988 * JTabbedPane. If the constraints object is an icon, it
989 * will be used as the tab's icon. If the constraints object
990 * is a string, we will use it as the title.
992 * @param component The associated component.
993 * @param constraints The constraints object.
995 public void add(Component component, Object constraints)
997 add(component, constraints, tabs.size());
1001 * This method adds a tab to the JTabbedPane.
1002 * If the Component is an instance of UIResource, it doesn't
1003 * add the tab and instead add the component directly to the
1004 * JTabbedPane. If the constraints object is an icon, it
1005 * will be used as the tab's icon. If the constraints object
1006 * is a string, we will use it as the title.
1008 * @param component The associated component.
1009 * @param constraints The constraints object.
1010 * @param index The index to insert the tab at.
1012 public void add(Component component, Object constraints, int index)
1014 if (component instanceof UIResource)
1015 super.add(component);
1016 else
1018 if (constraints instanceof String)
1019 insertTab((String) constraints, null, component, null, index);
1020 else
1021 insertTab(component.getName(),
1022 (constraints instanceof Icon) ? (Icon) constraints : null,
1023 component, null, index);
1028 * The tab and it's associated component are removed. After
1029 * the component has been removed from the JTabbedPane, it's
1030 * set visible to ensure that it can be seen.
1032 * @param index The index of the tab to remove.
1034 * @throws IndexOutOfBoundsException If the index is not in range.
1036 public void removeTabAt(int index)
1038 checkIndex(index, 0, tabs.size());
1039 Component c = getComponentAt(index);
1040 super.remove(c);
1041 c.show();
1042 tabs.remove(index);
1046 * This method removes the component from the JTabbedPane. After
1047 * the component has been removed from the JTabbedPane, it's
1048 * set visible to ensure that it can be seen.
1050 * @param component The Component to remove.
1052 public void remove(Component component)
1054 // This simply removes the component.
1055 int index = indexOfComponent(component);
1056 super.remove(component);
1057 component.show();
1058 setComponentAt(index, null);
1062 * This method removes the tab and component from the JTabbedPane.
1063 * It simply calls removeTabAt(int index).
1065 * @param index The index of the tab to remove.
1067 public void remove(int index)
1069 removeTabAt(index);
1073 * This method removes all tabs and associated components
1074 * from the JTabbedPane.
1076 public void removeAll()
1078 for (int i = tabs.size() - 1; i >= 0; i--)
1079 removeTabAt(i);
1083 * This method returns how many tabs are in the JTabbedPane.
1085 * @return The number of tabs in the JTabbedPane.
1087 public int getTabCount()
1089 return tabs.size();
1093 * This method returns the number of runs used
1094 * to paint the JTabbedPane.
1096 * @return The number of runs.
1098 public int getTabRunCount()
1100 return ((TabbedPaneUI) ui).getTabRunCount(this);
1104 * This method returns the tab title given the index.
1106 * @param index The index of the tab.
1108 * @return The title for the tab.
1110 public String getTitleAt(int index)
1112 checkIndex(index, 0, tabs.size());
1113 return ((Page) tabs.elementAt(index)).getTitle();
1117 * This method returns the active icon given the index.
1119 * @param index The index of the tab.
1121 * @return The active icon for the tab.
1123 public Icon getIconAt(int index)
1125 checkIndex(index, 0, tabs.size());
1126 return ((Page) tabs.elementAt(index)).getIcon();
1130 * This method returns the disabled icon given the index.
1132 * @param index The index of the tab.
1134 * @return The disabled icon for the tab.
1136 public Icon getDisabledIconAt(int index)
1138 checkIndex(index, 0, tabs.size());
1139 return ((Page) tabs.elementAt(index)).getDisabledIcon();
1143 * This method returns the tooltip string for the tab.
1145 * @param index The index of the tab.
1147 * @return The tooltip string for the tab.
1149 public String getToolTipTextAt(int index)
1151 checkIndex(index, 0, tabs.size());
1152 return ((Page) tabs.elementAt(index)).getTip();
1156 * This method returns the foreground color for the tab.
1158 * @param index The index of the tab.
1160 * @return The foreground color for the tab.
1162 public Color getForegroundAt(int index)
1164 checkIndex(index, 0, tabs.size());
1165 return ((Page) tabs.elementAt(index)).getForeground();
1169 * This method returns the background color for the tab.
1171 * @param index The index of the tab.
1173 * @return The background color for the tab.
1175 public Color getBackgroundAt(int index)
1177 checkIndex(index, 0, tabs.size());
1178 return ((Page) tabs.elementAt(index)).getBackground();
1182 * This method returns the component associated with the tab.
1184 * @param index The index of the tab.
1186 * @return The component associated with the tab.
1188 public Component getComponentAt(int index)
1190 checkIndex(index, 0, tabs.size());
1191 return ((Page) tabs.elementAt(index)).getComponent();
1195 * This method returns whether this tab is enabled.
1196 * Disabled tabs cannot be selected.
1198 * @param index The index of the tab.
1200 * @return Whether the tab is enabled.
1202 public boolean isEnabledAt(int index)
1204 checkIndex(index, 0, tabs.size());
1205 return ((Page) tabs.elementAt(index)).isEnabled();
1209 * This method returns the mnemonic for the tab.
1211 * @param tabIndex The index of the tab.
1213 * @return The mnemonic for the tab.
1215 public int getMnemonicAt(int tabIndex)
1217 checkIndex(tabIndex, 0, tabs.size());
1218 return ((Page) tabs.elementAt(tabIndex)).getMnemonic();
1222 * This method returns the mnemonic index for the tab.
1224 * @param tabIndex The index of the tab.
1226 * @return The mnemonic index for the tab.
1228 public int getDisplayedMnemonicIndexAt(int tabIndex)
1230 checkIndex(tabIndex, 0, tabs.size());
1231 return ((Page) tabs.elementAt(tabIndex)).getDisplayedMnemonicIndex();
1235 * This method returns the bounds of the tab given
1236 * the index.
1238 * @param index The index of the tab.
1240 * @return A rectangle describing the bounds of the tab.
1242 public Rectangle getBoundsAt(int index)
1244 checkIndex(index, 0, tabs.size());
1245 return ((TabbedPaneUI) ui).getTabBounds(this, index);
1249 * This method sets the title of the tab.
1251 * @param index The index of the tab.
1252 * @param title The new title.
1254 public void setTitleAt(int index, String title)
1256 checkIndex(index, 0, tabs.size());
1257 ((Page) tabs.elementAt(index)).setTitle(title);
1261 * This method sets the icon of the tab.
1263 * @param index The index of the tab.
1264 * @param icon The new icon.
1266 public void setIconAt(int index, Icon icon)
1268 checkIndex(index, 0, tabs.size());
1269 ((Page) tabs.elementAt(index)).setIcon(icon);
1273 * This method sets the disabled icon of the tab.
1275 * @param index The index of the tab.
1276 * @param disabledIcon The new disabled icon.
1278 public void setDisabledIconAt(int index, Icon disabledIcon)
1280 checkIndex(index, 0, tabs.size());
1281 ((Page) tabs.elementAt(index)).setDisabledIcon(disabledIcon);
1285 * This method sets the tooltip text of the tab.
1287 * @param index The index of the tab.
1288 * @param toolTipText The tooltip text.
1290 public void setToolTipTextAt(int index, String toolTipText)
1292 checkIndex(index, 0, tabs.size());
1293 ((Page) tabs.elementAt(index)).setTip(toolTipText);
1297 * This method sets the background color of the tab.
1299 * @param index The index of the tab.
1300 * @param background The background color of the tab.
1302 public void setBackgroundAt(int index, Color background)
1304 checkIndex(index, 0, tabs.size());
1305 ((Page) tabs.elementAt(index)).setBackground(background);
1309 * This method sets the foreground color of the tab.
1311 * @param index The index of the tab.
1312 * @param foreground The foreground color of the tab.
1314 public void setForegroundAt(int index, Color foreground)
1316 checkIndex(index, 0, tabs.size());
1317 ((Page) tabs.elementAt(index)).setForeground(foreground);
1321 * This method sets whether the tab is enabled.
1323 * @param index The index of the tab.
1324 * @param enabled Whether the tab is enabled.
1326 public void setEnabledAt(int index, boolean enabled)
1328 checkIndex(index, 0, tabs.size());
1329 ((Page) tabs.elementAt(index)).setEnabled(enabled);
1333 * This method sets the component associated with the tab.
1335 * @param index The index of the tab.
1336 * @param component The component associated with the tab.
1338 public void setComponentAt(int index, Component component)
1340 checkIndex(index, 0, tabs.size());
1341 ((Page) tabs.elementAt(index)).setComponent(component);
1345 * This method sets the displayed mnemonic index of the tab.
1347 * @param tabIndex The index of the tab.
1348 * @param mnemonicIndex The mnemonic index.
1350 public void setDisplayedMnemonicIndexAt(int tabIndex, int mnemonicIndex)
1352 checkIndex(tabIndex, 0, tabs.size());
1353 ((Page) tabs.elementAt(tabIndex)).setDisplayedMnemonicIndex(mnemonicIndex);
1357 * This method sets the mnemonic for the tab.
1359 * @param tabIndex The index of the tab.
1360 * @param mnemonic The mnemonic.
1362 public void setMnemonicAt(int tabIndex, int mnemonic)
1364 checkIndex(tabIndex, 0, tabs.size());
1365 ((Page) tabs.elementAt(tabIndex)).setMnemonic(mnemonic);
1369 * This method finds the index of a tab given the title.
1371 * @param title The title that belongs to a tab.
1373 * @return The index of the tab that has the title or -1 if not found.
1375 public int indexOfTab(String title)
1377 int index = -1;
1378 for (int i = 0; i < tabs.size(); i++)
1380 if (((Page) tabs.elementAt(i)).getTitle().equals(title))
1382 index = i;
1383 break;
1386 return index;
1390 * This method finds the index of a tab given the icon.
1392 * @param icon The icon that belongs to a tab.
1394 * @return The index of the tab that has the icon or -1 if not found.
1396 public int indexOfTab(Icon icon)
1398 int index = -1;
1399 for (int i = 0; i < tabs.size(); i++)
1401 if (((Page) tabs.elementAt(i)).getIcon() == icon)
1403 index = i;
1404 break;
1407 return index;
1411 * This method finds the index of a tab given the component.
1413 * @param component A component associated with a tab.
1415 * @return The index of the tab that has this component or -1 if not found.
1417 public int indexOfComponent(Component component)
1419 int index = -1;
1420 for (int i = 0; i < tabs.size(); i++)
1422 if (((Page) tabs.elementAt(i)).getComponent() == component)
1424 index = i;
1425 break;
1428 return index;
1432 * This method returns a tab index given an (x,y) location. The origin
1433 * of the (x,y) pair will be the JTabbedPane's top left position. The
1434 * tab returned will be the one that contains the point. This method is
1435 * delegated to the UI.
1437 * @param x The x coordinate of the point.
1438 * @param y The y coordinate of the point.
1440 * @return The index of the tab that contains the point.
1442 public int indexAtLocation(int x, int y)
1444 return ((TabbedPaneUI) ui).tabForCoordinate(this, x, y);
1448 * This method returns the tooltip text given a mouse event.
1450 * @param event The mouse event.
1452 * @return The tool tip text that is associated with this mouse event.
1454 public String getToolTipText(MouseEvent event)
1456 int index = indexAtLocation(event.getX(), event.getY());
1457 return ((Page) tabs.elementAt(index)).getTip();
1461 * This method returns a string representation of this JTabbedPane. It
1462 * is mainly used for debugging purposes.
1464 * @return A string representation of this JTabbedPane.
1466 protected String paramString()
1468 return "JTabbedPane";
1472 * DOCUMENT ME!
1474 * @return DOCUMENT ME!
1476 public AccessibleContext getAccessibleContext()
1478 if (accessibleContext == null)
1479 accessibleContext = new AccessibleJTabbedPane(this);
1480 return accessibleContext;