You are on page 1of 9

Introduction to 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. Even familiar components such as buttons have more capabilities in Swing. or example, a button ma! have both an image and a text string associated with it. Also, the image can be changed as the state of the button changes. "nlike AWT components, Swing components are not implemented b! platform#specific code. Instead, the! are written entirel! in $ava and, therefore, are platform#independent. The term lightweight is used to describe such elements. The Swing component classes The Swing#related classes are contained in %avax.swing and its subpackages, such as %avax.swing.tree. Component Description Abstract&utton &utton'roup ImageIcon $Applet $&utton $(heck&ox $(ombo&ox $+abel $,adio&utton $Scroll-ane $Tabbed-ane $Table $Text ield $Tree Abstract superclass for Swing buttons. Encapsulates a mutuall! exclusive set of buttons. Encapsulates an icon. The Swing version of Applet The Swing push button class. The Swing check box class. Encapsulates a combo box )an combination of a drop#down list and text field*. The Swing version of a label. The Swing version of a radio button. Encapsulates a scrollable window. Encapsulates a tabbed window. Encapsulates a table#based control. The Swing version of a text field. Encapsulates a tree#based control.

JApplet undamental to Swing is the JApplet class, which extends Applet. Applets that use Swing must be subclasses of JApplet. JApplet is rich with functionalit! that is not found in Applet. or example, JApplet supports various .panes,/ such as the content pane, the glass pane, and the root pane. 0owever, one difference between Applet and JApplet is 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 ob%ect. (ontainer get(ontent-ane) * The add( ) method of Container can be used to add a component to a content pane. Its form is shown here1 void add)comp* 0ere, comp is the component to be added to the content pane. Icons and Labels In Swing, icons are encapsulated b! the ImageIcon class, which paints an icon from an image. Two of its constructors are shown here1 ImageIcon)String filename* ImageIcon)",+ url* The first form uses the image in the file named filename. The second form uses the image in the resource identified b! url. The ImageIcon class implements the Icon interface that declares the following methods. ethod Description int getIcon0eight) * ,eturns the height of the icon in pixels. int getIconWidth) * ,eturns the width of the icon in pixels. void paintIcon)(omponent comp, -aints the icon at position x, y on the graphics 'raphics g,int x, int y* context g. Additional information about the paint operation can be provided in comp Swing labels are instances of the JLabel class, which extends JComponent. It can displa! text and2or an icon. Some of its constructors are shown here1 $+abel)Icon i* +abel)String s* $+abel)String s, Icon i, int align* 0ere, s and i are the text and icon used for the label. The align argument is L!"T# $I%&T, C!'T!$, L!ADI'%, or T$AILI'%. The icon and text associated with the label can be obtained b! the following methods1 Icon getIcon) * String getText) * The icon and text associated with a label can be set b! these methods1 void setIcon)Icon icon* void setText)String str* The following applet illustrates how to create and displa! a label containing both an icon and a string. It begins b! creating an ImageIcon ob%ect for the file (rance)gi() This is used as the second argument to the JLabel constructor. The first and last arguments for the JLabel

constructor are the label text and the alignment. inall!, the label is added to the content pane. *rogram on label and imageicon import %ava.awt.34 import %avax.swing.34 23 5applet code67$+abel8emo7 width69:; height6<:;= 52applet= 32 public class $+abel8emo extends $Applet > public void init)* > (ontainer content-ane 6 get(ontent-ane)*4 content-ane.set+a!out)new low+a!out)**4 ImageIcon ii 6 new ImageIcon)7france.gif7*4 $+abel %l 6 new $+abel)7 rance7, ii, $+abel.(E?TE,*4 add)%l*4 @ @

JTe+t"ield Demo:The Swing text field is encapsulated b! the JTe+tComponent class, which extends JComponent. It provides functionalit! that is common to Swing text components. Ane of its subclasses is JTe+t"ield, which allows !ou to edit one line of text. Some of its constructors are Three of JTe+t"ieldBs constructors are shown here1 $Text ield)* $Text ield)int cols* $Text ield)String str, int cols* $Text ield)String str* 0ere, str is the string to be initiall! presented, and cols is the number of columns in the text field. If no string is specified, the text field is initiall! empt!. If the number of columns is not specified, the text field is siCed to fit the specified string. JTe+t"ield generates events in response to user interaction. or example, an Action!,ent is fired when the user presses E?TE,. A Caret!,ent is fired each time the caret )i.e., the cursor* changes position. To obtain the text currentl! in the text field, call getTe+t( ). The following example illustrates JTe+t"ield. It creates a JTe+t"ield and adds it to the content pane. When the user presses E?TE,, an action event is generated. This is handled b! displa!ing the text in the status window.

import %ava.awt.34 import %ava.awt.event.34 import %avax.swing.34 23 5applet code67$Text ield8emo7 width6D;; height6:;= 52applet= 32 public class $Text ield8emo extends $Applet > $Text ield %tf4 public void init)* > set+a!out)new low+a!out)**4 %tf 6 new $Text ield)<:*4 add)%tf*4 %tf.addAction+istener)new Action+istener)* > public void action-erformed)ActionEvent ae* > showStatus)%tf.getText)**4 @ @*4 @ @

The Swing -uttons Swing defines four t!pes of buttons1 J-utton, JToggle-utton, JChec.-o+, and J$adio-utton. All are subclasses of the Abstract-utton class, which extends JComponent. Abstract-utton contains man! methods that allow !ou to control the behavior of buttons, check boxes, and radio buttons. The text associated with a button can be read and written via the following methods1 String getText) * void setText)String str* 0ere, str is the text to be associated with the button. Abstract-utton generate action events when the! are pressed. Abstract-utton is a superclass for push buttons, check boxes, and radio buttons.

The J-utton Class The J-utton class provides the functionalit! of a push button. J-utton allows an icon, a string, or both to be associated with the push button. Some of its constructors are $&utton)Icon icon* $&utton)String str* $&utton)String str, Icon icon*

0ere, str and icon are the string and icon used for the button. When the button is pressed, an Action!,ent is generated. "sing the Action!,ent ob%ect passed to the action*er(ormed( ) method of the registered ActionListener, !ou can obtain the action command string associated with the button. 0owever, !ou can set the action command b! calling setActionCommand( ) on the button. Eou can obtain the action command b! calling getActionCommand( ) on the event ob%ect. String getAction(ommand) * The action command identifies the button. Thus, when using two or more buttons within the same application, the action command gives !ou an eas! wa! to determine which button was pressed. import %avax.swing.34 import %ava.awt.34 import %ava.awt.event.34 23 5applet code6SwingApplet height6D;; width6D;;= 52applet= 32 public class SwingApplet extends $Applet > $&utton %btnAne, %btnTwo4 $+abel %lab4 public void init)* > set+a!out)new low+a!out)**4 %btnAne 6 new $&utton)7Ane7*4 %btnTwo 6 new $&utton)7Two7*4 %lab 6 new $+abel)7-ress a button.7*4 %btnAne.addAction+istener)new Action+istener)* > public void action-erformed)ActionEvent le* > %lab.setText)7&utton Ane pressed.7*4 @ @*4 %btnTwo.addAction+istener)new Action+istener)* > public void action-erformed)ActionEvent le* > %lab.setText)7&utton Two pressed.7*4 @ @*4 get(ontent-ane)*.add)%btnAne*4 get(ontent-ane)*.add)%btnTwo*4 get(ontent-ane)*.add)%lab*4

@ @

Chec. -o+es The JChec.-o+ class, which provides the functionalit! of a check box, is a concrete implementation of Abstract-utton. Its immediate superclass is JToggle-utton, which provides support for two#state buttons. Some of its constructors are shown here1 $(heck&ox)Icon i* $(heck&ox)Icon i, boolean state* $(heck&ox)String s* $(heck&ox)String s, boolean state* $(heck&ox)String s, Icon i* $(heck&ox)String s, Icon i, boolean state* 0ere, i is the icon for the button. The text is specified b! s. If state is true, the check box is initiall! selected. Atherwise, it is not. The state of the check box can be changed via the following method1 void setSelected)boolean state* 0ere, state is true if the check box should be checked. When a check box is selected or deselected, an item event is generated. This is handled b! itemStateChanged( ). Inside itemStateChanged( ), the getItem( ) method gets the JChec.-o+ ob%ect that generated the event. The getTe+t( ) method gets the text for that check box and uses it to set the text inside the text field. The following example illustrates check boxes. It displa!s four check boxes and a label. When the user clicks a check box, an Item!,ent is generated. Inside the itemStateChanged( ) method, getItem( ) is called to obtain a reference to the JChec.-o+ ob%ect that generated the event. ?ext, a call to isSelected( ) determines if the box was selected or cleared. The getTe+t( ) method gets the text for that check box and uses it to set the text inside the label. import %ava.awt.34 import %ava.awt.event.34 import %avax.swing.34 23 5applet code67$(heck&ox8emo7 width69F; height6:;= 52applet= 32 public class $(heck&ox8emo extends $Applet implements Item+istener >

$+abel %lab4 public void init)* > set+a!out)new low+a!out)**4 $(heck&ox cb 6 new $(heck&ox)7(7*4 cb.addItem+istener)this*4 add)cb*4 cb 6 new $(heck&ox)7(GG7*4 cb.addItem+istener)this*4 add)cb*4 cb 6 new $(heck&ox)7$ava7*4 cb.addItem+istener)this*4 add)cb*4 cb 6 new $(heck&ox)7-erl7*4 cb.addItem+istener)this*4 add)cb*4 %lab 6 new $+abel)7Select languages7*4 add)%lab*4 @ public void itemState(hanged)ItemEvent ie* > $(heck&ox cb 6 )$(heck&ox*ie.getItem)*4 if)cb.isSelected)** %lab.setText)cb.getText)* G 7 is selected7*4 else %lab.setText)cb.getText)* G 7 is cleared7*4 @ @

$adio -uttons ,adio buttons are supported b! the J$adio-utton class, which is a concrete implementation of Abstract-utton. Its immediate superclass is JToggle-utton, which provides support for two#state buttons. Some of its constructors are shown here1 $,adio&utton)Icon i* $,adio&utton)Icon i, boolean state* $,adio&utton)String s* $,adio&utton)String s, boolean state* $,adio&utton)String s, Icon i*

$,adio&utton)String s, Icon i, boolean state* 0ere, i is the icon for the button. The text is specified b! s. If state is true, the button is initiall! selected. Atherwise, it is not. ,adio buttons must be configured into a group. Anl! one of the buttons in that group can be selected at an! time. or example, if a user presses a radio button that is in a group, an! previousl! selected button in that group is automaticall! deselected. The -utton%roup class is instantiated to create a button group. Elements are then added to the button group via the following method1 void add)Abstract&utton ab* 0ere, ab is a reference to the button to be added to the group. ,adio button presses generate action events that are handled b! action*er(ormed( ). The getActionCommand( ) method gets the text that is associated with a radio button and uses it to set the text field. The following example illustrates how to use radio buttons. Three radio buttons and one text field are created. When a radio button is pressed, its text is displa!ed in the text field. irst, the content pane for the JApplet ob%ect is obtained and a flow la!out is assigned as its la!out manager. ?ext, three radio buttons are added to the content pane. Then, a button group is defined and the buttons are added to it. inall!, a text field is added to the content pane. import %ava.awt.34 import %ava.awt.event.34 import %avax.swing.34 23 5applet code67$,adio&utton8emo7 width6D;; height6:;= 52applet= 32 public class $,adio&utton8emo extends $Applet implements Action+istener > $Text ield tf4 public void init)* > (ontainer content-ane 6 get(ontent-ane)*4 content-ane.set+a!out)new low+a!out)**4 $,adio&utton b< 6 new $,adio&utton)7A7*4 b<.addAction+istener)this*4 content-ane.add)b<*4 $,adio&utton b9 6 new $,adio&utton)7&7*4 b9.addAction+istener)this*4 content-ane.add)b9*4 $,adio&utton bD 6 new $,adio&utton)7(7*4 bD.addAction+istener)this*4 content-ane.add)bD*4 &utton'roup bg 6 new &utton'roup)*4 bg.add)b<*4 bg.add)b9*4

bg.add)bD*4 tf 6 new $Text ield): *4 content-ane.add)tf*4 @ public void action-erformed)ActionEvent ae* > tf.setText)ae.getAction(ommand)**4 @ @

You might also like