Merge from mainline.
[official-gcc.git] / libjava / classpath / javax / swing / JTabbedPane.java
blob34ab8eeaa66bbec7ef45287670b08473d2176225
1 /* JTabbedPane.java --
2 Copyright (C) 2002, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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. */
39 package javax.swing;
41 import gnu.classpath.NotImplementedException;
43 import java.awt.Color;
44 import java.awt.Component;
45 import java.awt.Point;
46 import java.awt.Rectangle;
47 import java.awt.event.MouseEvent;
48 import java.io.Serializable;
49 import java.util.Locale;
50 import java.util.Vector;
52 import javax.accessibility.Accessible;
53 import javax.accessibility.AccessibleContext;
54 import javax.accessibility.AccessibleRole;
55 import javax.accessibility.AccessibleSelection;
56 import javax.accessibility.AccessibleStateSet;
57 import javax.swing.event.ChangeEvent;
58 import javax.swing.event.ChangeListener;
59 import javax.swing.plaf.TabbedPaneUI;
60 import javax.swing.plaf.UIResource;
62 /**
63 * This is a container for components where only one component is displayed at
64 * a given time and the displayed component can be switched by clicking on
65 * tabs.
67 * <p>
68 * Tabs can be oriented in several ways. They can be above, below, left and
69 * right of the component. Tabs can either wrap around (by creating multiple
70 * rows of tabs) or they can be scrolled (where only a subset of the tabs
71 * can be seen at once). More tabs can be added by calling the
72 * add/addTab/insertTab methods.
73 * </p>
75 public class JTabbedPane extends JComponent implements Serializable,
76 Accessible,
77 SwingConstants
79 /**
80 * Accessibility support for <code>JTabbedPane</code>.
82 // FIXME: This inner class is a complete stub and must be implemented
83 // properly.
84 protected class AccessibleJTabbedPane extends JComponent.AccessibleJComponent
85 implements AccessibleSelection, ChangeListener
87 /**
88 * The serialization UID.
90 private static final long serialVersionUID = 7610530885966830483L;
92 /**
93 * Creates a new AccessibleJTabbedPane object.
95 public AccessibleJTabbedPane()
97 super();
101 * Receives notification when the selection state of the
102 * <code>JTabbedPane</code> changes.
104 * @param e the change event describing the change
106 public void stateChanged(ChangeEvent e)
107 throws NotImplementedException
109 // Implement this properly.
113 * Returns the accessible role of the <code>JTabbedPane</code>, which is
114 * {@link AccessibleRole#PAGE_TAB_LIST}.
116 * @return the accessible role of the <code>JTabbedPane</code>
118 public AccessibleRole getAccessibleRole()
119 throws NotImplementedException
121 return null;
125 * Returns the number of accessible child components of the
126 * <code>JTabbedPane</code>.
128 * @return the number of accessible child components of the
129 * <code>JTabbedPane</code>
131 public int getAccessibleChildrenCount()
132 throws NotImplementedException
134 return 0;
138 * Returns the accessible child component at the specified index.
140 * @param i the index of the child component to fetch
142 * @return the accessible child component at the specified index
144 public Accessible getAccessibleChild(int i)
146 // Testing shows that the reference implementation returns instances
147 // of page here.
148 Accessible child = null;
149 if (i >= 0 && i < tabs.size())
150 child = (Page) tabs.get(i);
151 return child;
155 * Returns the current selection state of the <code>JTabbedPane</code>
156 * as AccessibleSelection object.
158 * @return the current selection state of the <code>JTabbedPane</code>
160 public AccessibleSelection getAccessibleSelection()
161 throws NotImplementedException
163 return null;
167 * Returns the accessible child component at the specified coordinates.
168 * If there is no child component at this location, then return the
169 * currently selected tab.
171 * @param p the coordinates at which to look up the child component
173 * @return the accessible child component at the specified coordinates or
174 * the currently selected tab if there is no child component at
175 * this location
177 public Accessible getAccessibleAt(Point p)
178 throws NotImplementedException
180 return null;
184 * The number of selected child components of the
185 * <code>JTabbedPane</code>. This will be <code>0</code> if the
186 * <code>JTabbedPane</code> has no children, or <code>1</code> otherwise,
187 * since there is always exactly one tab selected.
189 * @return number of selected child components of the
190 * <code>JTabbedPane</code>
192 public int getAccessibleSelectionCount()
193 throws NotImplementedException
195 return 0;
199 * DOCUMENT ME!
201 * @param i DOCUMENT ME!
203 * @return DOCUMENT ME!
205 public Accessible getAccessibleSelection(int i)
206 throws NotImplementedException
208 return null;
212 * DOCUMENT ME!
214 * @param i DOCUMENT ME!
216 * @return DOCUMENT ME!
218 public boolean isAccessibleChildSelected(int i)
219 throws NotImplementedException
221 return false;
225 * DOCUMENT ME!
227 * @param i DOCUMENT ME!
229 public void addAccessibleSelection(int i)
230 throws NotImplementedException
232 // TODO: Implement this properly.
236 * DOCUMENT ME!
238 * @param i DOCUMENT ME!
240 public void removeAccessibleSelection(int i)
241 throws NotImplementedException
243 // TODO: Implement this properly.
247 * DOCUMENT ME!
249 public void clearAccessibleSelection()
250 throws NotImplementedException
252 // TODO: Implement this properly.
256 * DOCUMENT ME!
258 public void selectAllAccessibleSelection()
259 throws NotImplementedException
261 // TODO: Implement this properly.
266 * A helper class that listens for changes to the model.
268 protected class ModelListener implements ChangeListener, Serializable
270 /** DOCUMENT ME! */
271 private static final long serialVersionUID = 497359819958114132L;
274 * Creates a new ModelListener object.
276 protected ModelListener()
278 // Nothing to do here.
282 * This method is called whenever the model is changed.
284 * @param e The ChangeEvent that is passed from the model.
286 public void stateChanged(ChangeEvent e)
288 // Propagate to our listeners.
289 fireStateChanged();
294 * A private class that holds all the information for each tab.
296 private class Page
297 extends AccessibleContext
298 implements Accessible
300 /** The tooltip string. */
301 private String tip;
303 /** The component associated with the tab. */
304 private Component component;
306 /** The active icon associated with the tab. */
307 private transient Icon icon;
309 /** The disabled icon associated with the tab. */
310 private transient Icon disabledIcon;
312 /** The tab's enabled status. */
313 private transient boolean enabled = true;
315 /** The string painted on the tab. */
316 private transient String title;
318 /** The background color of the tab. */
319 private transient Color bg;
321 /** The foreground color of the tab. */
322 private transient Color fg;
324 /** The mnemonic associated with the tab. */
325 private transient int mnemonicKey;
327 /** The index of the underlined character in the string. */
328 private transient int underlinedChar = -1;
331 * Creates a new data storage for the tab.
333 * @param title The string displayed on the tab.
334 * @param icon The active icon displayed on the tab.
335 * @param component The component associated with the tab.
336 * @param tip The tooltip associated with the tab.
338 protected Page(String title, Icon icon, Component component, String tip)
340 this.title = title;
341 this.icon = icon;
342 this.component = component;
343 this.tip = tip;
347 * This method returns the component associated with the tab.
349 * @return The component associated with the tab.
351 public Component getComponent()
353 return component;
357 * This method sets the component associated with the tab.
359 * @param c The component associated with the tab.
361 public void setComponent(Component c)
363 int i = indexOfComponent(component);
364 insertTab(title, icon, c, tip, i);
365 component = c;
366 removeTabAt(i);
370 * This method returns the tooltip string.
372 * @return The tooltip string.
374 public String getTip()
376 return tip;
380 * This method sets the tooltip string.
382 * @param tip The tooltip string.
384 public void setTip(String tip)
386 this.tip = tip;
390 * This method returns the background color.
392 * @return The background color.
394 public Color getBackground()
396 Color background;
397 if (bg == null)
398 background = JTabbedPane.this.getBackground();
399 else
400 background = bg;
401 return background;
405 * This method sets the background color.
407 * @param background The background color.
409 public void setBackground(Color background)
411 bg = background;
415 * This method returns the foreground color.
417 * @return The foreground color.
419 public Color getForeground()
421 Color foreground;
422 if (fg == null)
423 foreground = JTabbedPane.this.getForeground();
424 else
425 foreground = fg;
426 return foreground;
430 * This method sets the foreground color.
432 * @param foreground The foreground color.
434 public void setForeground(Color foreground)
436 fg = foreground;
440 * This method returns the title associated with the tab.
442 * @return The title of the tab.
444 public String getTitle()
446 return title;
449 /** DOCUMENT ME! */
450 private static final long serialVersionUID = 1614381073220130939L;
453 * This method sets the title of the tab.
455 * @param text The title of the tab.
457 public void setTitle(String text)
459 title = text;
460 if (title != null && title.length() <= underlinedChar)
461 setDisplayedMnemonicIndex(title.length() - 1);
465 * This method returns the active icon.
467 * @return The active icon.
469 public Icon getIcon()
471 return icon;
475 * This method sets the active icon.
477 * @param icon The active icon.
479 public void setIcon(Icon icon)
481 this.icon = icon;
485 * This method returns the disabled icon.
487 * @return The disabled icon.
489 public Icon getDisabledIcon()
491 if (disabledIcon == null && icon instanceof ImageIcon)
492 setDisabledIcon(icon);
493 return disabledIcon;
497 * This method sets the disabled icon.
499 * @param disabledIcon The disabled icon.
501 public void setDisabledIcon(Icon disabledIcon)
503 this.disabledIcon = disabledIcon;
507 * This method returns whether the tab is enabled.
509 * @return Whether the tab is enabled.
511 public boolean isEnabled()
513 return enabled;
517 * This method sets whether the tab is enabled.
519 * @param enabled Whether this tab is enabled.
521 public void setEnabled(boolean enabled)
523 this.enabled = enabled;
527 * This method returns the mnemonic.
529 * @return The mnemonic.
531 public int getMnemonic()
533 return mnemonicKey;
537 * This method sets the mnemonic. If the title is set, it will update the
538 * mnemonicIndex.
540 * @param key The mnemonic.
542 public void setMnemonic(int key)
544 setMnemonic((char) key);
548 * This method sets the mnemonic. If the title is set, it will update the
549 * mnemonicIndex.
551 * @param aChar The mnemonic.
553 public void setMnemonic(char aChar)
555 mnemonicKey = aChar;
556 if (title != null)
557 setDisplayedMnemonicIndex(title.indexOf(mnemonicKey));
561 * This method returns the mnemonicIndex.
563 * @return The mnemonicIndex.
565 public int getDisplayedMnemonicIndex()
567 return underlinedChar;
571 * This method sets the mnemonicIndex.
573 * @param index The mnemonicIndex.
575 * @throws IllegalArgumentException If index less than -1 || index greater
576 * or equal to title.length.
578 public void setDisplayedMnemonicIndex(int index)
579 throws IllegalArgumentException
581 if (index < -1 || title != null && index >= title.length())
582 throw new IllegalArgumentException();
584 if (title == null || mnemonicKey == 0 || (index > -1 && title.charAt(index) != mnemonicKey))
585 index = -1;
587 underlinedChar = index;
591 * Returns the accessible context, which is this object itself.
593 * @return the accessible context, which is this object itself
595 public AccessibleContext getAccessibleContext()
597 return this;
601 * Returns the accessible role of this tab, which is always
602 * {@link AccessibleRole#PAGE_TAB}.
604 * @return the accessible role of this tab
606 public AccessibleRole getAccessibleRole()
608 return AccessibleRole.PAGE_TAB;
611 public AccessibleStateSet getAccessibleStateSet()
612 throws NotImplementedException
614 // FIXME: Implement this properly.
615 return null;
618 public int getAccessibleIndexInParent()
619 throws NotImplementedException
621 // FIXME: Implement this properly.
622 return 0;
626 * Returns the number of accessible children, which is always one (the
627 * component of this tab).
629 * @return the number of accessible children
631 public int getAccessibleChildrenCount()
633 return 1;
637 * Returns the accessible child of this tab, which is the component
638 * displayed by the tab.
640 * @return the accessible child of this tab
642 public Accessible getAccessibleChild(int i)
644 // A quick test shows that this method always returns the component
645 // displayed by the tab, regardless of the index.
646 return (Accessible) component;
650 * Returns the locale of this accessible object.
652 * @return the locale of this accessible object
654 public Locale getLocale()
656 // TODO: Is this ok?
657 return Locale.getDefault();
661 private static final long serialVersionUID = 1614381073220130939L;
663 /** The changeEvent used to fire changes to listeners. */
664 protected ChangeEvent changeEvent;
666 /** The listener that listens to the model. */
667 protected ChangeListener changeListener;
669 /** The model that describes this JTabbedPane. */
670 protected SingleSelectionModel model;
672 /** Indicates that the TabbedPane is in scrolling mode. */
673 public static final int SCROLL_TAB_LAYOUT = 1;
675 /** Indicates that the TabbedPane is in wrap mode. */
676 public static final int WRAP_TAB_LAYOUT = 0;
678 /** The current tabPlacement of the TabbedPane. */
679 protected int tabPlacement = SwingConstants.TOP;
681 /** The current tabLayoutPolicy of the TabbedPane. */
682 private transient int layoutPolicy;
684 /** The list of tabs associated with the TabbedPane. */
685 transient Vector tabs = new Vector();
688 * Creates a new JTabbedPane object with tabs on top and using wrap tab
689 * layout.
691 public JTabbedPane()
693 this(SwingConstants.TOP, WRAP_TAB_LAYOUT);
697 * Creates a new JTabbedPane object using wrap tab layout and the given
698 * <code>tabPlacement</code>, where <code>tabPlacement</code> can be one
699 * of the following values: {@link #TOP}, {@link #BOTTOM}, {@link #LEFT} or
700 * {@link #RIGHT}.
702 * @param tabPlacement where the tabs will be placed
704 public JTabbedPane(int tabPlacement)
706 this(tabPlacement, WRAP_TAB_LAYOUT);
710 * Creates a new JTabbedPane object with the given <code>tabPlacement</code>
711 * and <code>tabLayoutPolicy</code>. The <code>tabPlacement</code> can be one
712 * of the following values: {@link #TOP}, {@link #BOTTOM}, {@link #LEFT} or
713 * {@link #RIGHT}. The <code>tabLayoutPolicy</code> can be either
714 * {@link #SCROLL_TAB_LAYOUT} or {@link #WRAP_TAB_LAYOUT}.
716 * @param tabPlacement where the tabs will be placed
717 * @param tabLayoutPolicy the way tabs will be placed
719 * @throws IllegalArgumentException If tabLayoutPolicy or tabPlacement are
720 * not valid.
722 public JTabbedPane(int tabPlacement, int tabLayoutPolicy)
724 if (tabPlacement != TOP && tabPlacement != BOTTOM && tabPlacement != RIGHT
725 && tabPlacement != LEFT)
726 throw new IllegalArgumentException("tabPlacement is not valid.");
727 if (tabLayoutPolicy != SCROLL_TAB_LAYOUT
728 && tabLayoutPolicy != WRAP_TAB_LAYOUT)
729 throw new IllegalArgumentException("tabLayoutPolicy is not valid.");
730 this.tabPlacement = tabPlacement;
731 layoutPolicy = tabLayoutPolicy;
733 changeEvent = new ChangeEvent(this);
734 changeListener = createChangeListener();
736 model = new DefaultSingleSelectionModel();
737 model.addChangeListener(changeListener);
739 updateUI();
743 * This method returns the UI used to display the JTabbedPane.
745 * @return The UI used to display the JTabbedPane.
747 public TabbedPaneUI getUI()
749 return (TabbedPaneUI) ui;
753 * This method sets the UI used to display the JTabbedPane.
755 * @param ui The UI used to display the JTabbedPane.
757 public void setUI(TabbedPaneUI ui)
759 super.setUI(ui);
763 * This method restores the UI to the defaults given by the UIManager.
765 public void updateUI()
767 setUI((TabbedPaneUI) UIManager.getUI(this));
771 * This method returns a string identifier that is used to determine which
772 * UI will be used with the JTabbedPane.
774 * @return A string identifier for the UI.
776 public String getUIClassID()
778 return "TabbedPaneUI";
782 * This method creates a ChangeListener that is used to listen to the model
783 * for events.
785 * @return A ChangeListener to listen to the model.
787 protected ChangeListener createChangeListener()
789 return new ModelListener();
793 * This method adds a ChangeListener to the JTabbedPane.
795 * @param l The ChangeListener to add.
797 public void addChangeListener(ChangeListener l)
799 listenerList.add(ChangeListener.class, l);
803 * This method removes a ChangeListener to the JTabbedPane.
805 * @param l The ChangeListener to remove.
807 public void removeChangeListener(ChangeListener l)
809 listenerList.remove(ChangeListener.class, l);
813 * This method fires a ChangeEvent to all the JTabbedPane's ChangeListeners.
815 protected void fireStateChanged()
817 Object[] changeListeners = listenerList.getListenerList();
818 if (changeEvent == null)
819 changeEvent = new ChangeEvent(this);
820 for (int i = changeListeners.length - 2; i >= 0; i -= 2)
822 if (changeListeners[i] == ChangeListener.class)
823 ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent);
828 * This method returns all ChangeListeners registered with the JTabbedPane.
830 * @return The ChangeListeners registered with the JTabbedPane.
832 public ChangeListener[] getChangeListeners()
834 return (ChangeListener[]) super.getListeners(ChangeListener.class);
838 * This method returns the model used with the JTabbedPane.
840 * @return The JTabbedPane's model.
842 public SingleSelectionModel getModel()
844 return model;
848 * This method changes the model property of the JTabbedPane.
850 * @param model The new model to use with the JTabbedPane.
852 public void setModel(SingleSelectionModel model)
854 if (model != this.model)
856 SingleSelectionModel oldModel = this.model;
857 this.model.removeChangeListener(changeListener);
858 this.model = model;
859 this.model.addChangeListener(changeListener);
860 firePropertyChange("model", oldModel, this.model);
865 * This method returns the tabPlacement.
867 * @return The tabPlacement used with the JTabbedPane.
869 public int getTabPlacement()
871 return tabPlacement;
875 * This method changes the tabPlacement property of the JTabbedPane.
877 * @param tabPlacement The tabPlacement to use.
879 * @throws IllegalArgumentException If tabPlacement is not one of TOP,
880 * BOTTOM, LEFT, or RIGHT.
882 public void setTabPlacement(int tabPlacement)
884 if (tabPlacement != TOP && tabPlacement != BOTTOM && tabPlacement != RIGHT
885 && tabPlacement != LEFT)
886 throw new IllegalArgumentException("tabPlacement is not valid.");
887 if (tabPlacement != this.tabPlacement)
889 int oldPlacement = this.tabPlacement;
890 this.tabPlacement = tabPlacement;
891 firePropertyChange("tabPlacement", oldPlacement, this.tabPlacement);
896 * This method returns the tabLayoutPolicy.
898 * @return The tabLayoutPolicy.
900 public int getTabLayoutPolicy()
902 return layoutPolicy;
906 * This method changes the tabLayoutPolicy property of the JTabbedPane.
908 * @param tabLayoutPolicy The tabLayoutPolicy to use.
910 * @throws IllegalArgumentException If tabLayoutPolicy is not one of
911 * SCROLL_TAB_LAYOUT or WRAP_TAB_LAYOUT.
913 public void setTabLayoutPolicy(int tabLayoutPolicy)
915 if (tabLayoutPolicy != SCROLL_TAB_LAYOUT
916 && tabLayoutPolicy != WRAP_TAB_LAYOUT)
917 throw new IllegalArgumentException("tabLayoutPolicy is not valid.");
918 if (tabLayoutPolicy != layoutPolicy)
920 int oldPolicy = layoutPolicy;
921 layoutPolicy = tabLayoutPolicy;
922 firePropertyChange("tabLayoutPolicy", oldPolicy, layoutPolicy);
927 * This method returns the index of the tab that is currently selected.
929 * @return The index of the selected tab.
931 public int getSelectedIndex()
933 return model.getSelectedIndex();
937 * This method checks the index.
939 * @param index The index to check.
940 * @param start DOCUMENT ME!
941 * @param end DOCUMENT ME!
943 * @throws IndexOutOfBoundsException DOCUMENT ME!
945 private void checkIndex(int index, int start, int end)
947 if (index < start || index >= end)
948 throw new IndexOutOfBoundsException("Index < " + start + " || Index >= "
949 + end);
953 * This method sets the selected index. This method will hide the old
954 * component and show the new component.
956 * @param index The index to set it at.
958 public void setSelectedIndex(int index)
960 checkIndex(index, -1, tabs.size());
961 if (index != getSelectedIndex())
963 if (getSelectedIndex() != -1 && getSelectedComponent() != null)
964 getSelectedComponent().hide();
965 if (index != -1 && getComponentAt(index) != null)
966 getComponentAt(index).show();
967 model.setSelectedIndex(index);
972 * This method returns the component at the selected index.
974 * @return The component at the selected index.
976 public Component getSelectedComponent()
978 int selectedIndex = getSelectedIndex();
979 Component selected = null;
980 if (selectedIndex >= 0)
981 selected = getComponentAt(selectedIndex);
982 return selected;
986 * This method sets the component at the selected index.
988 * @param c The component associated with the selected index.
990 public void setSelectedComponent(Component c)
992 if (c.getParent() == this)
993 setSelectedIndex(indexOfComponent(c));
994 else
995 setComponentAt(getSelectedIndex(), c);
999 * This method inserts tabs into JTabbedPane. This includes adding the
1000 * component to the JTabbedPane and hiding it.
1002 * @param title the title of the tab; may be <code>null</code>
1003 * @param icon the tab's icon; may be <code>null</code>
1004 * @param component the component associated with the tab
1005 * @param tip the tooltip for the tab
1006 * @param index the index to insert the tab at
1008 public void insertTab(String title, Icon icon, Component component,
1009 String tip, int index)
1011 if (title == null)
1012 title = "";
1013 Page p = new Page(title, icon, component, tip);
1014 tabs.insertElementAt(p, index);
1016 // Hide the component so we don't see it. Do it before we parent it
1017 // so we don't trigger a repaint.
1018 if (component != null)
1020 component.hide();
1021 super.add(component);
1024 if (getSelectedIndex() == -1)
1025 setSelectedIndex(0);
1027 revalidate();
1028 repaint();
1032 * This method adds a tab to the JTabbedPane.
1034 * @param title the title of the tab; may be <code>null</code>
1035 * @param icon the icon for the tab; may be <code>null</code>
1036 * @param component the associated component
1037 * @param tip the associated tooltip
1039 public void addTab(String title, Icon icon, Component component, String tip)
1041 insertTab(title, icon, component, tip, tabs.size());
1045 * This method adds a tab to the JTabbedPane.
1047 * @param title the title of the tab; may be <code>null</code>
1048 * @param icon the icon for the tab; may be <code>null</code>
1049 * @param component the associated component
1051 public void addTab(String title, Icon icon, Component component)
1053 insertTab(title, icon, component, null, tabs.size());
1057 * This method adds a tab to the JTabbedPane.
1059 * @param title the title of the tab; may be <code>null</code>
1060 * @param component the associated component
1062 public void addTab(String title, Component component)
1064 insertTab(title, null, component, null, tabs.size());
1068 * This method adds a tab to the JTabbedPane. The title of the tab is the
1069 * Component's name. If the Component is an instance of UIResource, it
1070 * doesn't add the tab and instead add the component directly to the
1071 * JTabbedPane.
1073 * @param component The associated component.
1075 * @return The Component that was added.
1077 public Component add(Component component)
1079 if (component instanceof UIResource)
1080 super.add(component);
1081 else
1082 insertTab(component.getName(), null, component, null, tabs.size());
1084 return component;
1088 * This method adds a tab to the JTabbedPane. If the Component is an
1089 * instance of UIResource, it doesn't add the tab and instead add the
1090 * component directly to the JTabbedPane.
1092 * @param title the title of the tab; may be <code>null</code>
1093 * @param component the associated component
1095 * @return The Component that was added.
1097 public Component add(String title, Component component)
1099 if (component instanceof UIResource)
1100 super.add(component);
1101 else
1102 insertTab(title, null, component, null, tabs.size());
1103 return component;
1107 * This method adds a tab to the JTabbedPane. If the Component is an
1108 * instance of UIResource, it doesn't add the tab and instead add the
1109 * component directly to the JTabbedPane.
1111 * @param component The associated component.
1112 * @param index The index to insert the tab at.
1114 * @return The Component that was added.
1116 public Component add(Component component, int index)
1118 if (component instanceof UIResource)
1119 super.add(component);
1120 else
1121 insertTab(component.getName(), null, component, null, index);
1122 return component;
1126 * This method adds a tab to the JTabbedPane. If the Component is an
1127 * instance of UIResource, it doesn't add the tab and instead add the
1128 * component directly to the JTabbedPane. If the constraints object is an
1129 * icon, it will be used as the tab's icon. If the constraints object is a
1130 * string, we will use it as the title.
1132 * @param component The associated component.
1133 * @param constraints The constraints object.
1135 public void add(Component component, Object constraints)
1137 add(component, constraints, tabs.size());
1141 * This method adds a tab to the JTabbedPane. If the Component is an
1142 * instance of UIResource, it doesn't add the tab and instead add the
1143 * component directly to the JTabbedPane. If the constraints object is an
1144 * icon, it will be used as the tab's icon. If the constraints object is a
1145 * string, we will use it as the title.
1147 * @param component The associated component.
1148 * @param constraints The constraints object.
1149 * @param index The index to insert the tab at.
1151 public void add(Component component, Object constraints, int index)
1153 if (component instanceof UIResource)
1154 super.add(component);
1155 else
1157 if (constraints instanceof String)
1158 insertTab((String) constraints, null, component, null, index);
1159 else
1160 insertTab(component.getName(),
1161 (constraints instanceof Icon) ? (Icon) constraints : null,
1162 component, null, index);
1167 * Removes the tab at index. After the component associated with
1168 * index is removed, its visibility is reset to true to ensure it
1169 * will be visible if added to other containers.
1171 * @param index The index of the tab to remove.
1173 public void removeTabAt(int index)
1175 checkIndex(index, 0, tabs.size());
1177 // We need to adjust the selection if we remove a tab that comes
1178 // before the selected tab or if the selected tab is removed.
1179 // This decrements the selected index by 1 if any of this is the case.
1180 // Note that this covers all cases:
1181 // - When the selected tab comes after the removed tab, this simply
1182 // adjusts the selection so that after the removal the selected tab
1183 // is still the same.
1184 // - When we remove the currently selected tab, then the tab before the
1185 // selected tab gets selected.
1186 // - When the last tab is removed, then we have an index==0, which gets
1187 // decremented to -1, which means no selection, which is 100% perfect.
1188 int selectedIndex = getSelectedIndex();
1189 if (selectedIndex >= index)
1190 setSelectedIndex(selectedIndex - 1);
1192 Component comp = getComponentAt(index);
1194 // Remove the tab object.
1195 tabs.remove(index);
1197 // Remove the component. I think we cannot assume that the tab order
1198 // is equal to the component order, so we iterate over the children
1199 // here to find the and remove the correct component.
1200 if (comp != null)
1202 Component[] children = getComponents();
1203 for (int i = children.length - 1; i >= 0; --i)
1205 if (children[i] == comp)
1207 super.remove(i);
1208 comp.setVisible(true);
1209 break;
1213 revalidate();
1214 repaint();
1218 * Removes the specified Component from the JTabbedPane.
1220 * @param component The Component to remove.
1222 public void remove(Component component)
1224 super.remove(component);
1228 * Removes the tab and component which corresponds to the specified index.
1230 * @param index The index of the tab to remove.
1232 public void remove(int index)
1234 super.remove(index);
1235 removeTabAt(index);
1239 * This method removes all tabs and associated components from the
1240 * JTabbedPane.
1242 public void removeAll()
1244 setSelectedIndex(-1);
1245 for (int i = getTabCount() - 1; i >= 0; i--)
1246 removeTabAt(i);
1250 * This method returns how many tabs are in the JTabbedPane.
1252 * @return The number of tabs in the JTabbedPane.
1254 public int getTabCount()
1256 return tabs.size();
1260 * This method returns the number of runs used to paint the JTabbedPane.
1262 * @return The number of runs.
1264 public int getTabRunCount()
1266 return ((TabbedPaneUI) ui).getTabRunCount(this);
1270 * This method returns the tab title given the index.
1272 * @param index The index of the tab.
1274 * @return The title for the tab.
1276 public String getTitleAt(int index)
1278 checkIndex(index, 0, tabs.size());
1279 return ((Page) tabs.elementAt(index)).getTitle();
1283 * This method returns the active icon given the index.
1285 * @param index The index of the tab.
1287 * @return The active icon for the tab.
1289 public Icon getIconAt(int index)
1291 checkIndex(index, 0, tabs.size());
1292 return ((Page) tabs.elementAt(index)).getIcon();
1296 * This method returns the disabled icon given the index.
1298 * @param index The index of the tab.
1300 * @return The disabled icon for the tab.
1302 public Icon getDisabledIconAt(int index)
1304 checkIndex(index, 0, tabs.size());
1305 return ((Page) tabs.elementAt(index)).getDisabledIcon();
1309 * This method returns the tooltip string for the tab.
1311 * @param index The index of the tab.
1313 * @return The tooltip string for the tab.
1315 public String getToolTipTextAt(int index)
1317 checkIndex(index, 0, tabs.size());
1318 return ((Page) tabs.elementAt(index)).getTip();
1322 * This method returns the foreground color for the tab.
1324 * @param index The index of the tab.
1326 * @return The foreground color for the tab.
1328 public Color getForegroundAt(int index)
1330 checkIndex(index, 0, tabs.size());
1331 return ((Page) tabs.elementAt(index)).getForeground();
1335 * This method returns the background color for the tab.
1337 * @param index The index of the tab.
1339 * @return The background color for the tab.
1341 public Color getBackgroundAt(int index)
1343 checkIndex(index, 0, tabs.size());
1344 return ((Page) tabs.elementAt(index)).getBackground();
1348 * This method returns the component associated with the tab.
1350 * @param index The index of the tab.
1352 * @return The component associated with the tab.
1354 public Component getComponentAt(int index)
1356 checkIndex(index, 0, tabs.size());
1357 return ((Page) tabs.elementAt(index)).getComponent();
1361 * This method returns whether this tab is enabled. Disabled tabs cannot be
1362 * selected.
1364 * @param index The index of the tab.
1366 * @return Whether the tab is enabled.
1368 public boolean isEnabledAt(int index)
1370 checkIndex(index, 0, tabs.size());
1371 return ((Page) tabs.elementAt(index)).isEnabled();
1375 * This method returns the mnemonic for the tab.
1377 * @param tabIndex The index of the tab.
1379 * @return The mnemonic for the tab.
1381 public int getMnemonicAt(int tabIndex)
1383 checkIndex(tabIndex, 0, tabs.size());
1384 return ((Page) tabs.elementAt(tabIndex)).getMnemonic();
1388 * This method returns the mnemonic index for the tab.
1390 * @param tabIndex The index of the tab.
1392 * @return The mnemonic index for the tab.
1394 public int getDisplayedMnemonicIndexAt(int tabIndex)
1396 checkIndex(tabIndex, 0, tabs.size());
1397 return ((Page) tabs.elementAt(tabIndex)).getDisplayedMnemonicIndex();
1401 * This method returns the bounds of the tab given the index.
1403 * @param index The index of the tab.
1405 * @return A rectangle describing the bounds of the tab.
1407 public Rectangle getBoundsAt(int index)
1409 checkIndex(index, 0, tabs.size());
1410 return ((TabbedPaneUI) ui).getTabBounds(this, index);
1414 * This method sets the title of the tab.
1416 * @param index The index of the tab.
1417 * @param title The new title.
1419 public void setTitleAt(int index, String title)
1421 checkIndex(index, 0, tabs.size());
1422 ((Page) tabs.elementAt(index)).setTitle(title);
1426 * This method sets the icon of the tab.
1428 * @param index The index of the tab.
1429 * @param icon The new icon.
1431 public void setIconAt(int index, Icon icon)
1433 checkIndex(index, 0, tabs.size());
1434 ((Page) tabs.elementAt(index)).setIcon(icon);
1438 * This method sets the disabled icon of the tab.
1440 * @param index The index of the tab.
1441 * @param disabledIcon The new disabled icon.
1443 public void setDisabledIconAt(int index, Icon disabledIcon)
1445 checkIndex(index, 0, tabs.size());
1446 ((Page) tabs.elementAt(index)).setDisabledIcon(disabledIcon);
1450 * This method sets the tooltip text of the tab.
1452 * @param index The index of the tab.
1453 * @param toolTipText The tooltip text.
1455 public void setToolTipTextAt(int index, String toolTipText)
1457 checkIndex(index, 0, tabs.size());
1458 ((Page) tabs.elementAt(index)).setTip(toolTipText);
1462 * This method sets the background color of the tab.
1464 * @param index The index of the tab.
1465 * @param background The background color of the tab.
1467 public void setBackgroundAt(int index, Color background)
1469 checkIndex(index, 0, tabs.size());
1470 ((Page) tabs.elementAt(index)).setBackground(background);
1474 * This method sets the foreground color of the tab.
1476 * @param index The index of the tab.
1477 * @param foreground The foreground color of the tab.
1479 public void setForegroundAt(int index, Color foreground)
1481 checkIndex(index, 0, tabs.size());
1482 ((Page) tabs.elementAt(index)).setForeground(foreground);
1486 * This method sets whether the tab is enabled.
1488 * @param index The index of the tab.
1489 * @param enabled Whether the tab is enabled.
1491 public void setEnabledAt(int index, boolean enabled)
1493 checkIndex(index, 0, tabs.size());
1494 ((Page) tabs.elementAt(index)).setEnabled(enabled);
1498 * This method sets the component associated with the tab.
1500 * @param index The index of the tab.
1501 * @param component The component associated with the tab.
1503 public void setComponentAt(int index, Component component)
1505 checkIndex(index, 0, tabs.size());
1506 ((Page) tabs.elementAt(index)).setComponent(component);
1510 * This method sets the displayed mnemonic index of the tab.
1512 * @param tabIndex The index of the tab.
1513 * @param mnemonicIndex The mnemonic index.
1515 public void setDisplayedMnemonicIndexAt(int tabIndex, int mnemonicIndex)
1517 checkIndex(tabIndex, 0, tabs.size());
1518 ((Page) tabs.elementAt(tabIndex)).setDisplayedMnemonicIndex(mnemonicIndex);
1522 * This method sets the mnemonic for the tab.
1524 * @param tabIndex The index of the tab.
1525 * @param mnemonic The mnemonic.
1527 public void setMnemonicAt(int tabIndex, int mnemonic)
1529 checkIndex(tabIndex, 0, tabs.size());
1530 ((Page) tabs.elementAt(tabIndex)).setMnemonic(mnemonic);
1534 * This method finds the index of a tab given the title.
1536 * @param title The title that belongs to a tab.
1538 * @return The index of the tab that has the title or -1 if not found.
1540 public int indexOfTab(String title)
1542 int index = -1;
1543 for (int i = 0; i < tabs.size(); i++)
1545 if (((Page) tabs.elementAt(i)).getTitle().equals(title))
1547 index = i;
1548 break;
1551 return index;
1555 * This method finds the index of a tab given the icon.
1557 * @param icon The icon that belongs to a tab.
1559 * @return The index of the tab that has the icon or -1 if not found.
1561 public int indexOfTab(Icon icon)
1563 int index = -1;
1564 for (int i = 0; i < tabs.size(); i++)
1566 if (((Page) tabs.elementAt(i)).getIcon() == icon)
1568 index = i;
1569 break;
1572 return index;
1576 * This method finds the index of a tab given the component.
1578 * @param component A component associated with a tab.
1580 * @return The index of the tab that has this component or -1 if not found.
1582 public int indexOfComponent(Component component)
1584 int index = -1;
1585 for (int i = 0; i < tabs.size(); i++)
1587 if (((Page) tabs.elementAt(i)).getComponent() == component)
1589 index = i;
1590 break;
1593 return index;
1597 * This method returns a tab index given an (x,y) location. The origin of
1598 * the (x,y) pair will be the JTabbedPane's top left position. The tab
1599 * returned will be the one that contains the point. This method is
1600 * delegated to the UI.
1602 * @param x The x coordinate of the point.
1603 * @param y The y coordinate of the point.
1605 * @return The index of the tab that contains the point.
1607 public int indexAtLocation(int x, int y)
1609 return ((TabbedPaneUI) ui).tabForCoordinate(this, x, y);
1613 * This method returns the tooltip text given a mouse event.
1615 * @param event The mouse event.
1617 * @return The tool tip text that is associated with this mouse event.
1619 public String getToolTipText(MouseEvent event)
1621 int index = indexAtLocation(event.getX(), event.getY());
1622 return ((Page) tabs.elementAt(index)).getTip();
1626 * This method returns a string representation of this JTabbedPane. It is
1627 * mainly used for debugging purposes.
1629 * @return A string representation of this JTabbedPane.
1631 protected String paramString()
1633 return "JTabbedPane";
1637 * DOCUMENT ME!
1639 * @return DOCUMENT ME!
1641 public AccessibleContext getAccessibleContext()
1643 if (accessibleContext == null)
1644 accessibleContext = new AccessibleJTabbedPane();
1645 return accessibleContext;