3 Cube Computer Institute Website: www.3cci.in Reference: The Complete Reference, Java Doc 1. Introduction to Swing Swing: Swing is a set of classes that provides more powerful and flexible components than are possible with the AWT. In addition to the familiar components, such as buttons, check boxes, and labels, Swing supplies several exciting additions, including tabbed panes, scroll panes, trees, and tables. Unlike AWT components, Swing components are not implemented by platform-specific code. Instead, they are written entirely in Java and, therefore, are platform-independent. The term lightweight is used to describe such elements. The Swing-related classes are contained in javax.swing and its subpackages, such as javax.swing.JTree. Difference between Awt and Swing: Awt Swing Native component. Pure Java component. Components are heavy weight. Components are light weight. Native look and feel. Pure java component. Does not have complex component. It has additional components like JTree, JTable, JProgressBar, and JSlider ect. Applet can not have menu. JApplet can contain menu. Components like Button can not have images. Components like JButton can have images. List has scrollbar. JList doesn’t support scrolling but this can be done using ScrollPane. Components can be added directly on the Window or Frame. While adding component to window or Frame, they have to be added on its ContentPane. Does not have SplitPane or TabbedPane. Has SplitPane or TabbedPane. Do not have MDI window. MDI can be achieved using JInternalFrame Object. Menu item can not have images or radio button or check boxes. Menu item can have images or radio button or check boxes. JApplet: Fundamental to Swing is the JApplet class, which extends Applet. JApplet is rich with functionality that is not found in Applet. For example, JApplet supports various “panes,” such as the content pane, the glass pane, and the root pane. Unlike Applet BorderLayout is default layout of JApplet. When adding a component to an instance of JApplet, do not invoke the add ( ) method of the applet. Instead, call add ( ) for the content pane of the JApplet object. The content pane can be obtained via the method shown here: Container getContentPane ( ) Hierarchy: java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet javax.swing.JApplet
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
1 Introduction to Swing
Swing
Swing is a set of classes that provides more powerful and flexible components than are possible with the
AWT In addition to the familiar components such as buttons check boxes and labels Swing supplies
several exciting additions including tabbed panes scroll panes trees and tables
Unlike AWT components Swing components are not implemented by platform-specific code Instead they
are written entirely in Java and therefore are platform-independent The term lightweight is used to describe
such elements
The Swing-related classes are contained in javaxswing and its subpackages such as javaxswingJTree
Difference between Awt and Swing
Awt Swing
Native component Pure Java component
Components are heavy weight Components are light weight
Native look and feel Pure java component
Does not have complex component It has additional components like JTree JTable
JProgressBar and JSlider ect
Applet can not have menu JApplet can contain menu
Components like Button can not have images Components like JButton can have images
List has scrollbar JList doesnrsquot support scrolling but this can be done
using ScrollPane
Components can be added directly on the Window
or Frame
While adding component to window or Frame they
have to be added on its ContentPane
Does not have SplitPane or TabbedPane Has SplitPane or TabbedPane
Do not have MDI window MDI can be achieved using JInternalFrame Object
Menu item can not have images or radio button or
check boxes
Menu item can have images or radio button or check
boxes
JApplet
Fundamental to Swing is the JApplet class which extends Applet JApplet is rich with functionality that is
not found in Applet For example JApplet supports various ldquopanesrdquo such as the content pane the glass
pane and the root pane
Unlike Applet BorderLayout is default layout of JApplet
When adding a component to an instance of JApplet do not invoke the add ( ) method of the applet Instead
call add ( ) for the content pane of the JApplet object The content pane can be obtained via the method
Creates a window with the specified GraphicsConfiguration of a screen device
Methods
setSize(int height int width) setLocation(int x int y) setVisible(boolean state)
Example
import javaxswing public class TopLevelWindows public static void main(String[] args) JFrame f = new JFrame(The Frame) fsetSize(300 300) fsetLocation(100 100) JWindow w = new JWindow( ) wsetSize(300 300) wsetLocation(500 100) fsetVisible(true) wsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
JFrame
Simply put if you know how to use the AWT Frame class then you can use JFrame JFrame is an extended version of Frame only JFrame exhibits a slight incompatibility when compared to the AWT Frame class because it contains only a single child (which is an instance of JRootPane) In order to add any other components to the JFrame instance they must be added to the root pane Hierarchy
Constructor JFrame() Constructs a new frame that is initially invisible JFrame(GraphicsConfiguration gc) Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title JFrame(String title) Creates a new initially invisible Frame with the specified title JFrame(String title GraphicsConfiguration gc) Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device Methods
setLayout(LayoutManager manager) setSize(int height int width) setLocation(int x int y) setVisible(boolean state)
Example
import javaxswing public class JFrameDemo public static void main(String[] args) JFrame f = new JFrame(The Frame) fsetSize(300 300) fsetLocation(100 100) fsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
JPanel
The Swing equivalent of AWTrsquos Panel class is JPanel With few exceptions everything you know about Panel applies equally to JPanel JPanel supports all of the AWT layout managers and also the new layouts provided by Swing
Creates a new JPanel with a double buffer and a flow layout
JPanel(LayoutManager layout)
Create a new buffered JPanel with the specified layout manager
Methods
setLayout(LayoutManager manager) add(Component c) Example import javaxswing public class JPanelDemo public static void main(String[] args) JFrame f = new JFrame(The Frame) JPanel p = new JPanel() fadd(p) fsetSize(300 300) fsetLocation(100 100) fsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
1) Icons
In Swing icons are encapsulated by the ImageIcon class which paints an icon from an image Two of its constructors are shown here Constructors
ImageIcon(String filename) ImageIcon(URL url) The first form uses the image in the file named filename The second form uses the image in the resource identified by url
Methods
int getIconHeight( ) int getIconWidth( )
2) JLabel Swing labels are instances of the JLabel class which extends JComponent It can display text andor an icon Hierarchy
Example import javaawt import javaxswing ltapplet code=JLabelDemo width=300 height=50gt ltappletgt public class JLabelDemo extends JApplet JLabel l1 ImageIcon i1 public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add Label to content pane i1=new ImageIcon(ldquosunsetjpgrdquo) j1= new JLabel(i1) contentPaneadd(j1) 3) JTextField The Swing text field is encapsulated by the JTextComponent class which extends JComponent It provides functionality that is common to Swing text components One of its subclasses is JTextField which allows you to edit one line of text
JTextField () Constructs a new TextField JTextField (int columns) Constructs a new empty TextField with the specified number of columns JTextField (String text) Constructs a new TextField initialized with the specified text JTextField (String text int columns) Constructs a new TextField initialized with the specified text and columns
Example import javaawt import javaxswing ltapplet code=JTextFieldDemo width=300 height=50gt ltappletgt public class JTextFieldDemo extends JApplet JTextField jtf public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add text field to content pane jtf = new JTextField(This is JTextField 15) contentPaneadd(jtf) 4) JButton Swing buttons provide features that are not found in the Button class defined by the AWT For example you can associate an icon with a Swing button Swing buttons are subclasses of the AbstractButton class which extends JComponent
Creates a button with no set text or icon JButton (Icon icon)
Creates a button with an icon JButton (String text)
Creates a button with text JButton (String text Icon icon)
Creates a button with initial text and an icon Methods
String getText( ) void setText(String s)
Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
5) JCheckBox
The JCheckBox class which provides the functionality of a check box is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons (checked or unchecked) Hierarchy
Constructors JCheckBox () Creates an initially unselected check box button with no text no icon JCheckBox (Icon icon) Creates an initially unselected check box with an icon JCheckBox (Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected JCheckBox (String text) Creates an initially unselected check box with text JCheckBox (String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected JCheckBox (String text Icon icon) Creates an initially unselected check box with the specified text and icon JCheckBox (String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected Methods
Radio buttons are supported by the JRadioButton class which is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons Radio buttons must be configured into a group Only one of the buttons in that group can be selected at any time The ButtonGroup class is instantiated to create a button group Its default constructor is invoked for this purpose Elements are then added to the button group via the following method
Constructors JRadioButton () Creates an initially unselected radio button with no set text JRadioButton (Icon icon) Creates an initially unselected radio button with the specified image but no text JRadioButton (Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text JRadioButton (String text) Creates an unselected radio button with the specified text JRadioButton (String text boolean selected) Creates a radio button with the specified text and selection state JRadioButton (String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected JRadioButton (String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state Methods
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
Creates a window with the specified GraphicsConfiguration of a screen device
Methods
setSize(int height int width) setLocation(int x int y) setVisible(boolean state)
Example
import javaxswing public class TopLevelWindows public static void main(String[] args) JFrame f = new JFrame(The Frame) fsetSize(300 300) fsetLocation(100 100) JWindow w = new JWindow( ) wsetSize(300 300) wsetLocation(500 100) fsetVisible(true) wsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
JFrame
Simply put if you know how to use the AWT Frame class then you can use JFrame JFrame is an extended version of Frame only JFrame exhibits a slight incompatibility when compared to the AWT Frame class because it contains only a single child (which is an instance of JRootPane) In order to add any other components to the JFrame instance they must be added to the root pane Hierarchy
Constructor JFrame() Constructs a new frame that is initially invisible JFrame(GraphicsConfiguration gc) Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title JFrame(String title) Creates a new initially invisible Frame with the specified title JFrame(String title GraphicsConfiguration gc) Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device Methods
setLayout(LayoutManager manager) setSize(int height int width) setLocation(int x int y) setVisible(boolean state)
Example
import javaxswing public class JFrameDemo public static void main(String[] args) JFrame f = new JFrame(The Frame) fsetSize(300 300) fsetLocation(100 100) fsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
JPanel
The Swing equivalent of AWTrsquos Panel class is JPanel With few exceptions everything you know about Panel applies equally to JPanel JPanel supports all of the AWT layout managers and also the new layouts provided by Swing
Creates a new JPanel with a double buffer and a flow layout
JPanel(LayoutManager layout)
Create a new buffered JPanel with the specified layout manager
Methods
setLayout(LayoutManager manager) add(Component c) Example import javaxswing public class JPanelDemo public static void main(String[] args) JFrame f = new JFrame(The Frame) JPanel p = new JPanel() fadd(p) fsetSize(300 300) fsetLocation(100 100) fsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
1) Icons
In Swing icons are encapsulated by the ImageIcon class which paints an icon from an image Two of its constructors are shown here Constructors
ImageIcon(String filename) ImageIcon(URL url) The first form uses the image in the file named filename The second form uses the image in the resource identified by url
Methods
int getIconHeight( ) int getIconWidth( )
2) JLabel Swing labels are instances of the JLabel class which extends JComponent It can display text andor an icon Hierarchy
Example import javaawt import javaxswing ltapplet code=JLabelDemo width=300 height=50gt ltappletgt public class JLabelDemo extends JApplet JLabel l1 ImageIcon i1 public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add Label to content pane i1=new ImageIcon(ldquosunsetjpgrdquo) j1= new JLabel(i1) contentPaneadd(j1) 3) JTextField The Swing text field is encapsulated by the JTextComponent class which extends JComponent It provides functionality that is common to Swing text components One of its subclasses is JTextField which allows you to edit one line of text
JTextField () Constructs a new TextField JTextField (int columns) Constructs a new empty TextField with the specified number of columns JTextField (String text) Constructs a new TextField initialized with the specified text JTextField (String text int columns) Constructs a new TextField initialized with the specified text and columns
Example import javaawt import javaxswing ltapplet code=JTextFieldDemo width=300 height=50gt ltappletgt public class JTextFieldDemo extends JApplet JTextField jtf public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add text field to content pane jtf = new JTextField(This is JTextField 15) contentPaneadd(jtf) 4) JButton Swing buttons provide features that are not found in the Button class defined by the AWT For example you can associate an icon with a Swing button Swing buttons are subclasses of the AbstractButton class which extends JComponent
Creates a button with no set text or icon JButton (Icon icon)
Creates a button with an icon JButton (String text)
Creates a button with text JButton (String text Icon icon)
Creates a button with initial text and an icon Methods
String getText( ) void setText(String s)
Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
5) JCheckBox
The JCheckBox class which provides the functionality of a check box is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons (checked or unchecked) Hierarchy
Constructors JCheckBox () Creates an initially unselected check box button with no text no icon JCheckBox (Icon icon) Creates an initially unselected check box with an icon JCheckBox (Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected JCheckBox (String text) Creates an initially unselected check box with text JCheckBox (String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected JCheckBox (String text Icon icon) Creates an initially unselected check box with the specified text and icon JCheckBox (String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected Methods
Radio buttons are supported by the JRadioButton class which is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons Radio buttons must be configured into a group Only one of the buttons in that group can be selected at any time The ButtonGroup class is instantiated to create a button group Its default constructor is invoked for this purpose Elements are then added to the button group via the following method
Constructors JRadioButton () Creates an initially unselected radio button with no set text JRadioButton (Icon icon) Creates an initially unselected radio button with the specified image but no text JRadioButton (Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text JRadioButton (String text) Creates an unselected radio button with the specified text JRadioButton (String text boolean selected) Creates a radio button with the specified text and selection state JRadioButton (String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected JRadioButton (String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state Methods
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
JWindow
JWindow class is similar to the JFrame class It uses a JRootPane for component management and
implements the RootPaneContainerBasically it is top level window with no andorment sJWindow class adds
no additional event handling capabilities beyond thoes of the JFrame and Window Classes
Creates a window with the specified GraphicsConfiguration of a screen device
Methods
setSize(int height int width) setLocation(int x int y) setVisible(boolean state)
Example
import javaxswing public class TopLevelWindows public static void main(String[] args) JFrame f = new JFrame(The Frame) fsetSize(300 300) fsetLocation(100 100) JWindow w = new JWindow( ) wsetSize(300 300) wsetLocation(500 100) fsetVisible(true) wsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
JFrame
Simply put if you know how to use the AWT Frame class then you can use JFrame JFrame is an extended version of Frame only JFrame exhibits a slight incompatibility when compared to the AWT Frame class because it contains only a single child (which is an instance of JRootPane) In order to add any other components to the JFrame instance they must be added to the root pane Hierarchy
Constructor JFrame() Constructs a new frame that is initially invisible JFrame(GraphicsConfiguration gc) Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title JFrame(String title) Creates a new initially invisible Frame with the specified title JFrame(String title GraphicsConfiguration gc) Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device Methods
setLayout(LayoutManager manager) setSize(int height int width) setLocation(int x int y) setVisible(boolean state)
Example
import javaxswing public class JFrameDemo public static void main(String[] args) JFrame f = new JFrame(The Frame) fsetSize(300 300) fsetLocation(100 100) fsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
JPanel
The Swing equivalent of AWTrsquos Panel class is JPanel With few exceptions everything you know about Panel applies equally to JPanel JPanel supports all of the AWT layout managers and also the new layouts provided by Swing
Creates a new JPanel with a double buffer and a flow layout
JPanel(LayoutManager layout)
Create a new buffered JPanel with the specified layout manager
Methods
setLayout(LayoutManager manager) add(Component c) Example import javaxswing public class JPanelDemo public static void main(String[] args) JFrame f = new JFrame(The Frame) JPanel p = new JPanel() fadd(p) fsetSize(300 300) fsetLocation(100 100) fsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
1) Icons
In Swing icons are encapsulated by the ImageIcon class which paints an icon from an image Two of its constructors are shown here Constructors
ImageIcon(String filename) ImageIcon(URL url) The first form uses the image in the file named filename The second form uses the image in the resource identified by url
Methods
int getIconHeight( ) int getIconWidth( )
2) JLabel Swing labels are instances of the JLabel class which extends JComponent It can display text andor an icon Hierarchy
Example import javaawt import javaxswing ltapplet code=JLabelDemo width=300 height=50gt ltappletgt public class JLabelDemo extends JApplet JLabel l1 ImageIcon i1 public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add Label to content pane i1=new ImageIcon(ldquosunsetjpgrdquo) j1= new JLabel(i1) contentPaneadd(j1) 3) JTextField The Swing text field is encapsulated by the JTextComponent class which extends JComponent It provides functionality that is common to Swing text components One of its subclasses is JTextField which allows you to edit one line of text
JTextField () Constructs a new TextField JTextField (int columns) Constructs a new empty TextField with the specified number of columns JTextField (String text) Constructs a new TextField initialized with the specified text JTextField (String text int columns) Constructs a new TextField initialized with the specified text and columns
Example import javaawt import javaxswing ltapplet code=JTextFieldDemo width=300 height=50gt ltappletgt public class JTextFieldDemo extends JApplet JTextField jtf public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add text field to content pane jtf = new JTextField(This is JTextField 15) contentPaneadd(jtf) 4) JButton Swing buttons provide features that are not found in the Button class defined by the AWT For example you can associate an icon with a Swing button Swing buttons are subclasses of the AbstractButton class which extends JComponent
Creates a button with no set text or icon JButton (Icon icon)
Creates a button with an icon JButton (String text)
Creates a button with text JButton (String text Icon icon)
Creates a button with initial text and an icon Methods
String getText( ) void setText(String s)
Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
5) JCheckBox
The JCheckBox class which provides the functionality of a check box is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons (checked or unchecked) Hierarchy
Constructors JCheckBox () Creates an initially unselected check box button with no text no icon JCheckBox (Icon icon) Creates an initially unselected check box with an icon JCheckBox (Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected JCheckBox (String text) Creates an initially unselected check box with text JCheckBox (String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected JCheckBox (String text Icon icon) Creates an initially unselected check box with the specified text and icon JCheckBox (String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected Methods
Radio buttons are supported by the JRadioButton class which is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons Radio buttons must be configured into a group Only one of the buttons in that group can be selected at any time The ButtonGroup class is instantiated to create a button group Its default constructor is invoked for this purpose Elements are then added to the button group via the following method
Constructors JRadioButton () Creates an initially unselected radio button with no set text JRadioButton (Icon icon) Creates an initially unselected radio button with the specified image but no text JRadioButton (Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text JRadioButton (String text) Creates an unselected radio button with the specified text JRadioButton (String text boolean selected) Creates a radio button with the specified text and selection state JRadioButton (String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected JRadioButton (String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state Methods
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
JFrame
Simply put if you know how to use the AWT Frame class then you can use JFrame JFrame is an extended version of Frame only JFrame exhibits a slight incompatibility when compared to the AWT Frame class because it contains only a single child (which is an instance of JRootPane) In order to add any other components to the JFrame instance they must be added to the root pane Hierarchy
Constructor JFrame() Constructs a new frame that is initially invisible JFrame(GraphicsConfiguration gc) Creates a Frame in the specified GraphicsConfiguration of a screen device and a blank title JFrame(String title) Creates a new initially invisible Frame with the specified title JFrame(String title GraphicsConfiguration gc) Creates a JFrame with the specified title and the specified GraphicsConfiguration of a screen device Methods
setLayout(LayoutManager manager) setSize(int height int width) setLocation(int x int y) setVisible(boolean state)
Example
import javaxswing public class JFrameDemo public static void main(String[] args) JFrame f = new JFrame(The Frame) fsetSize(300 300) fsetLocation(100 100) fsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
JPanel
The Swing equivalent of AWTrsquos Panel class is JPanel With few exceptions everything you know about Panel applies equally to JPanel JPanel supports all of the AWT layout managers and also the new layouts provided by Swing
Creates a new JPanel with a double buffer and a flow layout
JPanel(LayoutManager layout)
Create a new buffered JPanel with the specified layout manager
Methods
setLayout(LayoutManager manager) add(Component c) Example import javaxswing public class JPanelDemo public static void main(String[] args) JFrame f = new JFrame(The Frame) JPanel p = new JPanel() fadd(p) fsetSize(300 300) fsetLocation(100 100) fsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
1) Icons
In Swing icons are encapsulated by the ImageIcon class which paints an icon from an image Two of its constructors are shown here Constructors
ImageIcon(String filename) ImageIcon(URL url) The first form uses the image in the file named filename The second form uses the image in the resource identified by url
Methods
int getIconHeight( ) int getIconWidth( )
2) JLabel Swing labels are instances of the JLabel class which extends JComponent It can display text andor an icon Hierarchy
Example import javaawt import javaxswing ltapplet code=JLabelDemo width=300 height=50gt ltappletgt public class JLabelDemo extends JApplet JLabel l1 ImageIcon i1 public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add Label to content pane i1=new ImageIcon(ldquosunsetjpgrdquo) j1= new JLabel(i1) contentPaneadd(j1) 3) JTextField The Swing text field is encapsulated by the JTextComponent class which extends JComponent It provides functionality that is common to Swing text components One of its subclasses is JTextField which allows you to edit one line of text
JTextField () Constructs a new TextField JTextField (int columns) Constructs a new empty TextField with the specified number of columns JTextField (String text) Constructs a new TextField initialized with the specified text JTextField (String text int columns) Constructs a new TextField initialized with the specified text and columns
Example import javaawt import javaxswing ltapplet code=JTextFieldDemo width=300 height=50gt ltappletgt public class JTextFieldDemo extends JApplet JTextField jtf public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add text field to content pane jtf = new JTextField(This is JTextField 15) contentPaneadd(jtf) 4) JButton Swing buttons provide features that are not found in the Button class defined by the AWT For example you can associate an icon with a Swing button Swing buttons are subclasses of the AbstractButton class which extends JComponent
Creates a button with no set text or icon JButton (Icon icon)
Creates a button with an icon JButton (String text)
Creates a button with text JButton (String text Icon icon)
Creates a button with initial text and an icon Methods
String getText( ) void setText(String s)
Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
5) JCheckBox
The JCheckBox class which provides the functionality of a check box is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons (checked or unchecked) Hierarchy
Constructors JCheckBox () Creates an initially unselected check box button with no text no icon JCheckBox (Icon icon) Creates an initially unselected check box with an icon JCheckBox (Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected JCheckBox (String text) Creates an initially unselected check box with text JCheckBox (String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected JCheckBox (String text Icon icon) Creates an initially unselected check box with the specified text and icon JCheckBox (String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected Methods
Radio buttons are supported by the JRadioButton class which is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons Radio buttons must be configured into a group Only one of the buttons in that group can be selected at any time The ButtonGroup class is instantiated to create a button group Its default constructor is invoked for this purpose Elements are then added to the button group via the following method
Constructors JRadioButton () Creates an initially unselected radio button with no set text JRadioButton (Icon icon) Creates an initially unselected radio button with the specified image but no text JRadioButton (Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text JRadioButton (String text) Creates an unselected radio button with the specified text JRadioButton (String text boolean selected) Creates a radio button with the specified text and selection state JRadioButton (String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected JRadioButton (String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state Methods
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
JPanel
The Swing equivalent of AWTrsquos Panel class is JPanel With few exceptions everything you know about Panel applies equally to JPanel JPanel supports all of the AWT layout managers and also the new layouts provided by Swing
Creates a new JPanel with a double buffer and a flow layout
JPanel(LayoutManager layout)
Create a new buffered JPanel with the specified layout manager
Methods
setLayout(LayoutManager manager) add(Component c) Example import javaxswing public class JPanelDemo public static void main(String[] args) JFrame f = new JFrame(The Frame) JPanel p = new JPanel() fadd(p) fsetSize(300 300) fsetLocation(100 100) fsetVisible(true)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
1) Icons
In Swing icons are encapsulated by the ImageIcon class which paints an icon from an image Two of its constructors are shown here Constructors
ImageIcon(String filename) ImageIcon(URL url) The first form uses the image in the file named filename The second form uses the image in the resource identified by url
Methods
int getIconHeight( ) int getIconWidth( )
2) JLabel Swing labels are instances of the JLabel class which extends JComponent It can display text andor an icon Hierarchy
Example import javaawt import javaxswing ltapplet code=JLabelDemo width=300 height=50gt ltappletgt public class JLabelDemo extends JApplet JLabel l1 ImageIcon i1 public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add Label to content pane i1=new ImageIcon(ldquosunsetjpgrdquo) j1= new JLabel(i1) contentPaneadd(j1) 3) JTextField The Swing text field is encapsulated by the JTextComponent class which extends JComponent It provides functionality that is common to Swing text components One of its subclasses is JTextField which allows you to edit one line of text
JTextField () Constructs a new TextField JTextField (int columns) Constructs a new empty TextField with the specified number of columns JTextField (String text) Constructs a new TextField initialized with the specified text JTextField (String text int columns) Constructs a new TextField initialized with the specified text and columns
Example import javaawt import javaxswing ltapplet code=JTextFieldDemo width=300 height=50gt ltappletgt public class JTextFieldDemo extends JApplet JTextField jtf public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add text field to content pane jtf = new JTextField(This is JTextField 15) contentPaneadd(jtf) 4) JButton Swing buttons provide features that are not found in the Button class defined by the AWT For example you can associate an icon with a Swing button Swing buttons are subclasses of the AbstractButton class which extends JComponent
Creates a button with no set text or icon JButton (Icon icon)
Creates a button with an icon JButton (String text)
Creates a button with text JButton (String text Icon icon)
Creates a button with initial text and an icon Methods
String getText( ) void setText(String s)
Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
5) JCheckBox
The JCheckBox class which provides the functionality of a check box is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons (checked or unchecked) Hierarchy
Constructors JCheckBox () Creates an initially unselected check box button with no text no icon JCheckBox (Icon icon) Creates an initially unselected check box with an icon JCheckBox (Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected JCheckBox (String text) Creates an initially unselected check box with text JCheckBox (String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected JCheckBox (String text Icon icon) Creates an initially unselected check box with the specified text and icon JCheckBox (String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected Methods
Radio buttons are supported by the JRadioButton class which is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons Radio buttons must be configured into a group Only one of the buttons in that group can be selected at any time The ButtonGroup class is instantiated to create a button group Its default constructor is invoked for this purpose Elements are then added to the button group via the following method
Constructors JRadioButton () Creates an initially unselected radio button with no set text JRadioButton (Icon icon) Creates an initially unselected radio button with the specified image but no text JRadioButton (Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text JRadioButton (String text) Creates an unselected radio button with the specified text JRadioButton (String text boolean selected) Creates a radio button with the specified text and selection state JRadioButton (String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected JRadioButton (String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state Methods
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
1) Icons
In Swing icons are encapsulated by the ImageIcon class which paints an icon from an image Two of its constructors are shown here Constructors
ImageIcon(String filename) ImageIcon(URL url) The first form uses the image in the file named filename The second form uses the image in the resource identified by url
Methods
int getIconHeight( ) int getIconWidth( )
2) JLabel Swing labels are instances of the JLabel class which extends JComponent It can display text andor an icon Hierarchy
Example import javaawt import javaxswing ltapplet code=JLabelDemo width=300 height=50gt ltappletgt public class JLabelDemo extends JApplet JLabel l1 ImageIcon i1 public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add Label to content pane i1=new ImageIcon(ldquosunsetjpgrdquo) j1= new JLabel(i1) contentPaneadd(j1) 3) JTextField The Swing text field is encapsulated by the JTextComponent class which extends JComponent It provides functionality that is common to Swing text components One of its subclasses is JTextField which allows you to edit one line of text
JTextField () Constructs a new TextField JTextField (int columns) Constructs a new empty TextField with the specified number of columns JTextField (String text) Constructs a new TextField initialized with the specified text JTextField (String text int columns) Constructs a new TextField initialized with the specified text and columns
Example import javaawt import javaxswing ltapplet code=JTextFieldDemo width=300 height=50gt ltappletgt public class JTextFieldDemo extends JApplet JTextField jtf public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add text field to content pane jtf = new JTextField(This is JTextField 15) contentPaneadd(jtf) 4) JButton Swing buttons provide features that are not found in the Button class defined by the AWT For example you can associate an icon with a Swing button Swing buttons are subclasses of the AbstractButton class which extends JComponent
Creates a button with no set text or icon JButton (Icon icon)
Creates a button with an icon JButton (String text)
Creates a button with text JButton (String text Icon icon)
Creates a button with initial text and an icon Methods
String getText( ) void setText(String s)
Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
5) JCheckBox
The JCheckBox class which provides the functionality of a check box is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons (checked or unchecked) Hierarchy
Constructors JCheckBox () Creates an initially unselected check box button with no text no icon JCheckBox (Icon icon) Creates an initially unselected check box with an icon JCheckBox (Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected JCheckBox (String text) Creates an initially unselected check box with text JCheckBox (String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected JCheckBox (String text Icon icon) Creates an initially unselected check box with the specified text and icon JCheckBox (String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected Methods
Radio buttons are supported by the JRadioButton class which is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons Radio buttons must be configured into a group Only one of the buttons in that group can be selected at any time The ButtonGroup class is instantiated to create a button group Its default constructor is invoked for this purpose Elements are then added to the button group via the following method
Constructors JRadioButton () Creates an initially unselected radio button with no set text JRadioButton (Icon icon) Creates an initially unselected radio button with the specified image but no text JRadioButton (Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text JRadioButton (String text) Creates an unselected radio button with the specified text JRadioButton (String text boolean selected) Creates a radio button with the specified text and selection state JRadioButton (String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected JRadioButton (String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state Methods
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JLabelDemo width=300 height=50gt ltappletgt public class JLabelDemo extends JApplet JLabel l1 ImageIcon i1 public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add Label to content pane i1=new ImageIcon(ldquosunsetjpgrdquo) j1= new JLabel(i1) contentPaneadd(j1) 3) JTextField The Swing text field is encapsulated by the JTextComponent class which extends JComponent It provides functionality that is common to Swing text components One of its subclasses is JTextField which allows you to edit one line of text
JTextField () Constructs a new TextField JTextField (int columns) Constructs a new empty TextField with the specified number of columns JTextField (String text) Constructs a new TextField initialized with the specified text JTextField (String text int columns) Constructs a new TextField initialized with the specified text and columns
Example import javaawt import javaxswing ltapplet code=JTextFieldDemo width=300 height=50gt ltappletgt public class JTextFieldDemo extends JApplet JTextField jtf public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add text field to content pane jtf = new JTextField(This is JTextField 15) contentPaneadd(jtf) 4) JButton Swing buttons provide features that are not found in the Button class defined by the AWT For example you can associate an icon with a Swing button Swing buttons are subclasses of the AbstractButton class which extends JComponent
Creates a button with no set text or icon JButton (Icon icon)
Creates a button with an icon JButton (String text)
Creates a button with text JButton (String text Icon icon)
Creates a button with initial text and an icon Methods
String getText( ) void setText(String s)
Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
5) JCheckBox
The JCheckBox class which provides the functionality of a check box is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons (checked or unchecked) Hierarchy
Constructors JCheckBox () Creates an initially unselected check box button with no text no icon JCheckBox (Icon icon) Creates an initially unselected check box with an icon JCheckBox (Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected JCheckBox (String text) Creates an initially unselected check box with text JCheckBox (String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected JCheckBox (String text Icon icon) Creates an initially unselected check box with the specified text and icon JCheckBox (String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected Methods
Radio buttons are supported by the JRadioButton class which is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons Radio buttons must be configured into a group Only one of the buttons in that group can be selected at any time The ButtonGroup class is instantiated to create a button group Its default constructor is invoked for this purpose Elements are then added to the button group via the following method
Constructors JRadioButton () Creates an initially unselected radio button with no set text JRadioButton (Icon icon) Creates an initially unselected radio button with the specified image but no text JRadioButton (Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text JRadioButton (String text) Creates an unselected radio button with the specified text JRadioButton (String text boolean selected) Creates a radio button with the specified text and selection state JRadioButton (String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected JRadioButton (String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state Methods
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTextFieldDemo width=300 height=50gt ltappletgt public class JTextFieldDemo extends JApplet JTextField jtf public void init() Get content pane Container contentPane = getContentPane() contentPanesetLayout(new FlowLayout()) Add text field to content pane jtf = new JTextField(This is JTextField 15) contentPaneadd(jtf) 4) JButton Swing buttons provide features that are not found in the Button class defined by the AWT For example you can associate an icon with a Swing button Swing buttons are subclasses of the AbstractButton class which extends JComponent
Creates a button with no set text or icon JButton (Icon icon)
Creates a button with an icon JButton (String text)
Creates a button with text JButton (String text Icon icon)
Creates a button with initial text and an icon Methods
String getText( ) void setText(String s)
Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
5) JCheckBox
The JCheckBox class which provides the functionality of a check box is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons (checked or unchecked) Hierarchy
Constructors JCheckBox () Creates an initially unselected check box button with no text no icon JCheckBox (Icon icon) Creates an initially unselected check box with an icon JCheckBox (Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected JCheckBox (String text) Creates an initially unselected check box with text JCheckBox (String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected JCheckBox (String text Icon icon) Creates an initially unselected check box with the specified text and icon JCheckBox (String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected Methods
Radio buttons are supported by the JRadioButton class which is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons Radio buttons must be configured into a group Only one of the buttons in that group can be selected at any time The ButtonGroup class is instantiated to create a button group Its default constructor is invoked for this purpose Elements are then added to the button group via the following method
Constructors JRadioButton () Creates an initially unselected radio button with no set text JRadioButton (Icon icon) Creates an initially unselected radio button with the specified image but no text JRadioButton (Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text JRadioButton (String text) Creates an unselected radio button with the specified text JRadioButton (String text boolean selected) Creates a radio button with the specified text and selection state JRadioButton (String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected JRadioButton (String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state Methods
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
5) JCheckBox
The JCheckBox class which provides the functionality of a check box is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons (checked or unchecked) Hierarchy
Constructors JCheckBox () Creates an initially unselected check box button with no text no icon JCheckBox (Icon icon) Creates an initially unselected check box with an icon JCheckBox (Icon icon boolean selected) Creates a check box with an icon and specifies whether or not it is initially selected JCheckBox (String text) Creates an initially unselected check box with text JCheckBox (String text boolean selected) Creates a check box with text and specifies whether or not it is initially selected JCheckBox (String text Icon icon) Creates an initially unselected check box with the specified text and icon JCheckBox (String text Icon icon boolean selected) Creates a check box with text and icon and specifies whether or not it is initially selected Methods
Radio buttons are supported by the JRadioButton class which is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons Radio buttons must be configured into a group Only one of the buttons in that group can be selected at any time The ButtonGroup class is instantiated to create a button group Its default constructor is invoked for this purpose Elements are then added to the button group via the following method
Constructors JRadioButton () Creates an initially unselected radio button with no set text JRadioButton (Icon icon) Creates an initially unselected radio button with the specified image but no text JRadioButton (Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text JRadioButton (String text) Creates an unselected radio button with the specified text JRadioButton (String text boolean selected) Creates a radio button with the specified text and selection state JRadioButton (String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected JRadioButton (String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state Methods
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
6) JRadioButton
Radio buttons are supported by the JRadioButton class which is a concrete implementation of AbstractButton Its immediate superclass is JToggleButton which provides support for two-state buttons Radio buttons must be configured into a group Only one of the buttons in that group can be selected at any time The ButtonGroup class is instantiated to create a button group Its default constructor is invoked for this purpose Elements are then added to the button group via the following method
Constructors JRadioButton () Creates an initially unselected radio button with no set text JRadioButton (Icon icon) Creates an initially unselected radio button with the specified image but no text JRadioButton (Icon icon boolean selected) Creates a radio button with the specified image and selection state but no text JRadioButton (String text) Creates an unselected radio button with the specified text JRadioButton (String text boolean selected) Creates a radio button with the specified text and selection state JRadioButton (String text Icon icon) Creates a radio button that has the specified text and image and that is initially unselected JRadioButton (String text Icon icon boolean selected) Creates a radio button that has the specified text image and selection state Methods
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
7) JComboBox
Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class which extends JComponent A combo box normally displays one entry However it can also display a drop-down list that allows a user to select a different entry You can also type your selection into the text field Hierarchy
JComboBox () Creates a JComboBox with a default data model JComboBox (Object [] items) Creates a JComboBox that contains the elements in the specified array JComboBox (Vector items) Creates a JComboBox that contains the elements in the specified Vector
Methods
Object getItemAt (int index) int getItemCount () int getSelectedIndex () Object getSelectedItem ()
addItem (Object anObject) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
8) JTabbedPane
A tabbed pane is a component that appears as a group of folders in a file cabinet Each folder has a title when a user selects the folder its contents become visible Only one of the folders may be selected at a time Tabbed panes are commonly used for setting configuration options Tabbed panes are encapsulated by the JTabbedPane class which extends JComponent Hierarchy
JTabbedPane () Creates an empty TabbedPane with a default tab placement of JTabbedPaneTOP JTabbedPane (int tabPlacement) Creates an empty TabbedPane with the specified tab placement of either JTabbedPaneTOP JTabbedPaneBOTTOM JTabbedPaneLEFT or JTabbedPaneRIGHT JTabbedPane (int tabPlacement int tabLayoutPolicy) Creates an empty TabbedPane with the specified tab placement and tab layout policy Methods void addTab(String title Component component) void addTab(String title Icon icon Component component) void addTab(String title Icon icon Component component String tip) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
9) JScrollPane A scroll pane is a component that presents a rectangular area in which a component may be viewed Horizontal andor vertical scroll bars may be provided if necessary Scroll panes are implemented in Swing by the JScrollPane class which extends JComponent
Constructors JScrollPane () Creates an empty (no viewport view) JScrollPane where both horizontal and vertical scrollbars appear when needed JScrollPane (int vsbPolicy int hsbPolicy) Creates an empty (no viewport view) JScrollPane with specified scrollbar policies Methods JScrollBar getVerticalScrollBar () void setVerticalScrollBar (JScrollBar bar) JScrollBar getHorizontalScrollBar () void setHorizontalScrollBar (JScrollBar bar)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
10) JTree A tree is a component that presents a hierarchical view of data A user has the ability to expand or collapse individual subtrees in this display Trees are implemented in Swing by the JTree class which extends JComponent
Constructors JTree () Returns a JTree with a sample model JTree (TreeNode root) Returns a JTree with the specified TreeNode as its root which displays the root node Methods int getRowCount() int getRowForLocation(int x int y) TreePath getPathForLocation (int x int y) Example
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
11) JTable A table is a component that displays rows and columns of data You can drag the cursor on column boundaries to resize columns You can also drag a column to a new position Tables are implemented by the JTable class which extends JComponent
Construstors JTable () Constructs a default JTable that is initialized with a default data model a default column model and a default selection model JTable (int numRows int numColumns) Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel JTable (Object [][] rowData Object[] columnNames) Constructs a JTable to display the values in the two dimensional array rowData with column names columnNames JTable (Vector rowData Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors rowData with column names columnNames
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaxswing ltapplet code=JTableDemo width=400 height=200gt ltappletgt public class JTableDemo extends JApplet public void init() Get content pane Container contentPane = getContentPane() Set layout manager contentPanesetLayout(new BorderLayout()) Initialize column headings String[] colHeads = Name Phone Fax Initialize data Object[][] data = Gail 4567 8675 Ken 7566 5555 Viviane 5634 5887 Melanie 7345 9222 Anne 1237 3333 John 5656 3144 Matt 5672 2176 Claire 6741 4244 Erwin 9023 5159 Ellen 1134 5332 Jennifer 5689 1212 Ed 9030 1313 Helen 6751 1415 Create the table JTable table = new JTable (data colHeads) Add table to a scroll pane int v = ScrollPaneConstantsVERTICAL_SCROLLBAR_AS_NEEDED int h = ScrollPaneConstantsHORIZONTAL_SCROLLBAR_AS_NEEDED JScrollPane jsp = new JScrollPane (table v h) Add scroll pane to the content pane contentPaneadd (jsp BorderLayoutCENTER)
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
12) JList A list box is simply a user interface control containing a collection of similar items from which the user can make one or more selections This is the premise behind the AWT List control which presents a Java component equivalent to list box objects found in all other graphics interfaces Lists are implemented by the JList class which extends JComponent
Hierarchy
javalangObject
javaawtComponent javaawtContainer
javaxswingJComponent javaxswingJList
Constructor JList() Constructs a JList with an empty model JList(ListModel dataModel) Constructs a JList that displays the elements in the specified non-null model JList(Object[] listData) Constructs a JList that displays the elements in the specified array JList(Vector listData) Constructs a JList that displays the elements in the specified Vector Methods
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()
3 Cube Computer Institute Website www3cciin
Reference The Complete Reference Java Doc
Example import javaawt import javaawtevent import javaxswing class ListDemo extends JFrame Instance attributes used in this example JPanel topPanel JList listbox Constructor of main frame public ListDemo () Create a panel to hold all other components topPanel = new JPanel() topPanelsetLayout( new BorderLayout() ) getContentPane()add( topPanel ) Create some items to add to the list String listData[] = Item 1 Item 2 Item 3 Item 4 Create a new list box control listbox = new JList( listData ) topPaneladd( listbox BorderLayoutCENTER ) setSize( 300 100 ) setVisible( true ) public static void main( String args[] ) Create an instance of the test application ListDemo ld = new ListDemo ()