Basically to create a JTabbedPane component in Java, one should follow Use JTabbedPane(JTabbedPane. . Java Annotations Tutorial. 5. Java JTabbedPane class – Java Swing Tutorial Explaining the JTabbedPane Component. Java JTabbedPane – The java swing JTabbedPane control is available in Swing. This component appears as a group of folders in a file cabinet.
|Published (Last):||17 January 2006|
|PDF File Size:||5.13 Mb|
|ePub File Size:||6.85 Mb|
|Price:||Free* [*Free Regsitration Required]|
Examples and practices described in this page don’t take advantage of improvements introduced in later releases. With the JTabbedPane class, you can have several components, such as panelsshare the same space. The user chooses which component to view by selecting the tab corresponding to the desired component.
If you want similar functionality without the tab interface, you can use a card layout instead of a tabbed pane. To create a tabbed pane, instantiate JTabbedPanecreate the components you wish it tutroial display, and then add the components to the tabbed pane using the addTab method.
Java JTabbedpane Class Example
The following picture introduces an application called TabbedPaneDemo that has a tabbed pane with four tabs. As the TabbedPaneDemo example shows, a tab can have a tool tip and a mnemonic, and it can display both text and an image.
The default tab placement is set to the TOP location, as shown above. The following code from TabbedPaneDemo. Note that no event-handling code is necessary. The JTabbedPane object takes care of mouse and keyboard events for you. As the previous code shows, the addTab method handles the bulk of the work in setting up a tab in a tabbed pane.
The addTab method has several forms, but they all use both a string title and the component to be displayed by the tab. Optionally, you can specify an icon and tool tip string. The text or icon or both can be null. Another way to create a tab is to use the insertTab method, which lets you specify the index of the tab you’re adding. Note that the addTab method does not allow index specification in this step.
To switch to a specific tab programmatically, use the setSelectedIndex or the setSelectedComponent methods. When building components to add to a tabbed pane, keep in mind that no matter which child of a tabbed pane is visible, each child gets the same amount of space in which to display itself. The preferred size of the tabbed pane is just big enough to display its tallest child at its preferred height, and its widest child at its preferred width.
Similarly, the minimum size of the tabbed pane depends on the biggest minimum width and height of all its children. In the TabbedPaneDemo example, the fourth panel has a preferred width and height that are larger than those of the other panels.
Thus, the preferred size of the tabbed pane is just big enough to display the fourth panel at its preferred size. If you do not understand how preferred size is used, please refer to How Layout Management Works. The TabComponentsDemo example introduces a tabbed pane whose tabs contain real components.
The use of custom components brings new features such as buttons, combo boxes, labels and other components to tabs, and allows more complex user interaction. The following code from ButtonTabComponent. Note that event-handling code is necessary. Since each tab contains a real JButton object, you must attach an ActionListener to the close button. As the user clicks the button, the actionPerformed method determines the index of the tab it belongs to and removes the corresponding tab.
The code below, taken from ButtonTabComponent. The following tables list the commonly used JTabbedPane constructors and methods. The API for using tabbed panes falls into the following categories:. This table lists examples that use JTabbedPane and points to where those examples are described.
Using Swing Components Section: How to Use Various Components. To Create Tabbed Panes To create a tabbed pane, instantiate JTabbedPanecreate the components you wish it to display, and then add the components to the tabbed pane using the addTab method.
Alternatively, to compile and run the example yourself, consult the example index. Put the cursor over a tab. The tool tip associated with the tab appears. As a convenience, you can specify tool tip text when you add a component to the tabbed pane. Select a tab by clicking it. The tabbed pane displays the component corresponding to the tab. Select a tab by entering its mnemonic. For example, in the Java look and feel you can select the tab labeled “Tab 3” by typing Alt Navigate between scrollable tabs.
This example provides scrollable tabs. Resize the dialog box by moving its left or right boundary so that tabs do not fit within the dialog. Scroll arrows appear next to the tabs. Click the arrow to view one of the hidden tabs. Note that clicking the arrow only reveals hidden tabs. It does not select a new tab.
Select a tab by clicking it make sure not to hit the little cross. Put the cursor over one of the widgets with a little cross. The cross turns magenta and gets enclosed in a square.
A tool tip associated with the close button appears. Click the cross with the left mouse button to close the tab. Restore the tabs that have been removed by choosing the Reset JTabbedPane item from the Options menu.
Note that tabs with custom components are displayed on top of original tabbed pane tabs. To view the tabs underneath, open the Options menu and clear the Use TabComponents checkbox.
Display the tabs with components by selecting the Use TabComponents checkbox again.
Now the tabbed pane is empty. How to Use Split Panes Next page: How to Use Tables. Creates a tabbed pane. The first optional argument specifies where the tabs should appear.
By default, the tabs appear at the top of the tabbed pane. You can specify these positions defined in the SwingConstants interface, which JTabbedPane implements: The second optional argument specifies the tab layout policy. You can specify one of these ktabbedpane defined in JTabbedPane: Adds a new tab to the tabbed pane.
The first argument specifies the text on the tab. The optional icon argument specifies the tab’s icon. The component argument specifies the component that the tabbed pane should show when the tab is selected. The fourth argument, if present, specifies the tool tip text for the tab. Sets or gets the policy that the tabbed pane uses in laying out tabs when all tabs do not jtavbedpane within a single run.
Sets or gets the location where the tabs appear relative to the content. Inserts a tab at the specified index, where the first tab is at index 0.
The arguments are the same as for addTab. Selects jtabbedpzne tab that has the specified component or index. Selecting a tab has the effect of displaying its associated component.
Sets or gets which component is associated with the tab at the specified index. The first tab is at index 0. Sets or gets the background or foreground color used by the tab at the specified index.
Java JTabbedPane class example | Wideskills
By default, a tab uses the tabbed pane’s background and foreground colors. For example, if the tabbed pane’s foreground is black, then each tab’s title is black except for any tabs for which you specify another color using setForegroundAt.
Sets or gets which character should be decorated to represent the mnemonic. This is useful when the mnemonic character jtxbbedpane multiple times in the tab’s title and you don’t want the first occurrence underlined.
Sets the component that is responsible for rendering the title or icon or both for the tab specified by the first argument. When a null value is specified, JTabbedPane renders the title or icon. The same component cannot be used for several tabs. Gets the tab component for the tab at the index specified by the argument.
JTabbedPane « Swing « Java Tutorial
If there is no tab component for the specified tab, a null value is returned. Checks if the specified component belongs to one of the tabs. Return the index of the corresponding tab or -1 if there is no such a tuhorial. Demonstrates a few tabbed pane features, such as tool tips, icons, scrollable layout, and mnemonics.