Java
Swing - GUIs LESSON NOTE – JTABBEDPANE
INTRODUCING TABS Tabs
are a very common component used by GUIs to organize and give access to
information. They are used by
operating systems, applications and web pages. Here’s a simple example: JTABBEDPANE CLASS In
Java, we can create tabs inside GUIs by using the JTabbedPane.java
class. This class extends the JComponent class and therefore can be added to a JFrame or JPanel. Below
is a simple example of a JTabbedPane: HOW
IT WORKS Creating a basic tab system is fairly
simple in Java. You simply create a JTabbedPane object, add tabs to it and add it to the JFrame or JPanel. ADDING
TABS Tabs require four pieces of
information: ·
Tab
text ·
Tab
icon ·
Component
to be displayed under tab (usually a JPanel) ·
Tool
tip when mouse is hovering over tab Note that if you want no image icon,
you can simply place a null value in for that parameter. EXAMPLE We add a tab by using the following
line: tabPaneObjName.addTab("Tab text", null, panelName, "Tool tip info"); where
null can be
replaced by an icon. SIZING Note that the size of the entire
tabbed pane object will depend on the size of the largest component that will
be part of the tabs. For our example, since we will be using
empty JPanels, we will use the setPreferredSize(…) method
to give the JPanel a non-empty size. EXAMPLE
CODE Here’s a full example with a JTabbedPane created.
It includes no image icons. For
simplicity, the JPanels are each given a different
background colour. Remember that one
of the JPanels needs to be given a preferred size
in order to ensure a Tabbed Pane object of decent size – the example does
this for JPanel #3 called pan3.
TAB
COUNT We can get the number of tabs in the
tabbed pane by using the following method call: int total = tabPaneObjName.getTabCount() REMOVING
TABS We can remove tabs based on their
index number by using: tabPaneObjName.remove(indexToRemove); EXAMPLE Here is an example that will include a
button to add tabs and a button to remove tabs. The code that specifies the preferred size
of the panels exists on the panel of tab #3.
Using the button, remove tab #3 and notice what happens.
|
|||||
|