0% found this document useful (0 votes)
192 views19 pages

Understanding Java Swing Architecture

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views19 pages

Understanding Java Swing Architecture

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module – 3 Swings

Module 3 SWINGS

The Origins of Swing

Java’s original GUI subsystem: the Abstract Window Toolkit. The AWT defines a basic set of controls, windows,
and dialog boxes that support a usable, but limited graphical interface. AWT translates its various visual
components into their corresponding, platform-specific equivalents, or peers. This means that the look and feel
of a component is defined by the platform, not by Java. Because the AWT components use native code
resources, they are referred to as heavyweight.

Drawbacks of AWT :

1. Because of variations between operating systems, a component might look, or even act, differently on
different platforms. This potential variability threatened the overarching philosophy of Java: write once,
run anywhere.
2. The look and feel of each component was fixed (because it is defined by the platform) and could not be
(easily) changed.
3. The use of heavyweight components caused some frustrating restrictions. For example, a heavyweight
component was always opaque.

Swing --

● Introduced in 1997
● Swing was included as part of the Java Foundation Classes (JFC).
● Swing was initially available for use with Java 1.1 as a separate library.
● With Java 1.2, Swing (and the rest of the JFC) was fully integrated into Java.

Swing Is Built on the AWT

Swing does not replace AWT. Instead, Swing is built on the foundation of the AWT. Swing also uses the same
event handling mechanism as the AWT. Therefore, a basic understanding of the AWT and of event handling is
required to use Swing.

Two Key Swing Features

1. Swing Components Are Lightweight


a. they are written entirely in Java and do not map directly to platform-specific peers.
b. lightweight components are more efficient and more flexible
c. lightweight components do not translate into native peers, the look and feel of each component is
determined by Swing, not by the underlying operating system
d. As a result, each component will work in a consistent manner across all platforms.
2. Swing Supports a Pluggable Look and Feel
a. Swing supports a pluggable look and feel (PLAF). Because each Swing component is rendered by Java
code rather than by native peers, the look and feel of a component is under the control of Swing.
b. It is possible to separate the look and feel of a component from the logic of the component
c. It is possible to “plug in” a new look and feel for any given component without creating any side
effects in the code that uses that component.

Prof Vasanthalakshmi M, Dept of ISE, SCE 1


Module – 3 Swings

d. Pluggable look-and-feels offer several important advantages.


i) It is possible to define a look and feel that is consistent across all platforms.
ii) Conversely, it is possible to create a look and feel that acts like a specific platform.
iii) It is also possible to design a custom look and feel.
iv) Finally, the look and feel can be changed dynamically at run time

The MVC Connection

A visual component is a composite of three distinct aspects:

• The state information associated with the component -- MODEL

• The way that the component looks when rendered on the screen -- VIEW

• The way that the component reacts to the user -- CONTROLLER

In MVC terminology, the model corresponds to the state information associated with the component. For
example, in the case of a check box, the model contains a field that indicates if the box is checked or unchecked.
The view determines how the component is displayed on the screen, including any aspects of the view that are
affected by the current state of the model. The controller determines how the component reacts to the user. For
example, when the user clicks a check box, the controller reacts by changing the model to reflect the user’s
choice (checked or unchecked). This then results in the view being updated.

The controller acts as an interface between view and model. It intercepts all the requests i.e. receives input and
commands to Model / View to change accordingly.

By separating a component into a model, a view, and a controller, the specific implementation of each can be
changed without affecting the other two. For instance, different view implementations can render the same
component in different ways without affecting the model or the controller.

Swing uses a modified version of MVC that combines the view and the controller into a single logical entity called
the UI delegate. For this reason, Swing’s approach is called either the Model-Delegate architecture or the
Separable Model architecture.

Swing’s pluggable look and feel is made possible by its Model-Delegate architecture. Because the view (look) and
controller (feel) are separate from the model, the look and feel can be changed without affecting how the
component is used within a program. Conversely, it is possible to customize the model without affecting the way
that the component appears on the screen or responds to user input.

Models are defined by interfaces and UI delegates are classes.

Prof Vasanthalakshmi M, Dept of ISE, SCE 2


Module – 3 Swings

Difference between AWT and Swings:

AWT Swing
AWT components are platform- dependent Swing components are platform-independent.
AWT components are heavyweight. Swing components are lightweight.
AWT provides less components than Swing. Swing provides more powerful components such as
tables, lists, scrollpanes, colorchooser and etc.
AWT doesn't follows MVC(Model View Controller) where Swing follows MVC.
model represents data, view represents presentation
and controller acts as an interface between model and
view.

Components and Containers

A Swing GUI consists of two key items: components and containers.

Component is an independent visual control. A container holds a group of components. Thus, a container is a
special type of component that is designed to hold other components. In order for a component to be displayed,
it must be held within a container. Thus, all Swing GUIs will have at least one container.

Since containers are components, a container can also hold other containers. This enables Swing to define what
is called a containment hierarchy, at the top of which must be a top-level container.

Components

● Swing components are derived from the JComponent class. (Exceptions the four top-level containers
JFrame, JApplet, JWindow, and JDialog).
● JComponent provides the functionality that is common to all components.
● JComponent supports the pluggable look and feel. JComponent class inherits the AWT classes --
Container and Component.
● Thus, a Swing component is built on and compatible with an AWT component.
● All of Swing’s components are represented by classes defined within the package [Link].
● All component classes begin with the letter J.

The following table shows the class names for Swing components (including those used as containers).

Prof Vasanthalakshmi M, Dept of ISE, SCE 3


Module – 3 Swings

Containers

Swing defines two types of containers.

1) The first are top-level containers: JFrame, JApplet, JWindow, and JDialog. The top-level containers are
heavyweight because they inherit the AWT classes Component and Container. A top-level container must
be at the top of a containment hierarchy. A top-level container is not contained within any other
container. The most commonly used container for Java standalone applications is JFrame.

2) The second type of containers supported by Swing are lightweight containers. Lightweight containers
inherit JComponent. A general-purpose lightweight container is JPanel. Lightweight containers are often
used to organize and manage groups of related components because a lightweight container can be
contained within another container.

Prof Vasanthalakshmi M, Dept of ISE, SCE 4


Module – 3 Swings

The Top-Level Container Panes

Each top-level container defines a set of panes.


Whenever we create a top level container four
sub-level containers are automatically created:
Glass pane (JGlass), Root pane (JRootPane),
Layered pane(JLayeredPane) and Content pane.

At the top of the hierarchy is an instance of JRootPane.


JRootPane is a lightweight container whose purpose is to
manage the other panes. It also helps manage the
optional menu bar.

The glass pane is the top-level pane.


It sits above and completely covers all other panes.
By default, it is a transparent instance of JPanel. The glass pane enables you to manage mouse events that affect
the entire container (rather than an individual control) or to paint over any other component.

The layered pane is an instance of JLayeredPane. The layered pane allows components to be given a depth value.
This value determines which component overlays another. (Thus, the layered pane lets you specify a Z-order for a
component, although this is not something that you will usually need to do.) The layered pane holds the content
pane and the (optional) menu bar.

The pane with which your application will interact the most is the content pane, because this is the pane to which
you will add visual components. In other words, when you add a component, such as a button, to a top-level
container, you will add it to the content pane. By default, the content pane is an opaque instance of JPanel.

The Swing Packages

Swing is a very large subsystem and makes use of many packages. The main package is [Link]. This package
must be imported into any program that uses Swing. It contains the classes that implement the basic Swing
components, such as push buttons, labels, and check boxes.

JFrame:

Frame represents a window with a title bar and borders. Frame becomes the basis for creating the GUIs for an
application because all the components go into the frame. To create a frame, we have to create an object to
JFrame class in swing as

JFrame jf=new JFrame(); // create a frame without title

JFrame jf=new JFrame(“title”); // create a frame with title

To close the frame, use setDefaultCloseOperation() method of JFrame class . Closing the window causes the
entire application to terminate.

Prof Vasanthalakshmi M, Dept of ISE, SCE 5


Module – 3 Swings

setDefaultCloseOperation(constant) where constant values are

JFrame.EXIT_ON_CLOSE This closes the application upon clicking the close button
JFrame.DISPOSE_ON_CLOSE close button This closes the application upon clicking the close button
JFrame.DO_NOTHING_ON_CLOSE This will not perform any operation upon clicking close button
JFrame.HIDE_ON_CLOSE This hides the frame upon clicking close button

To create simple swing example, you need a frame. In swing, we use JFrame class to create a frame. There are
two ways to create a frame in swing.

• By extending JFrame class (inheritance)

Ex: class Example extends JFrame {

…….. }

• By creating the object of JFrame class (association)

Ex: class Example {

JFrame obj=new JFrame();

…….. }

A Simple Swing Example

We can write the code of swing inside the main() or constructor.

In Main() Method: [Link]

import [Link].*;

public class SwingExample


{
public static void main(String[] args)
{ JFrame f=new JFrame("Simple Swing Example");
[Link](300,300);
[Link](null);
[Link](true);
}}

(OR) In Constructor()

import [Link].*;

class SwingExample extends JFrame


{ SwingExample() {

setSize(300,300); //frame size 300 width and 300 height

Prof Vasanthalakshmi M, Dept of ISE, SCE 6


Module – 3 Swings

setLayout(null); //no layout manager


setVisible(true); //now frame will be visible, by default not visible
setTitle("SwingExample "); //Set frame Title
}
public static void main(String args[])
{ SwingExample f=new SwingExample();
}}

Components of Swing:
JComponent:

The class JComponent is the base class for all Swing components except top-level containers.

Constructor: JComponent();

The following are the JComponent class's methods to manipulate the appearance of the component.

JLabel and ImageIcon

● JLabel is Swing’s easiest-to-use component.


● It creates a label and to display text and/or an icon.
● It is a passive component that does not respond to user input and do not generates any event.

JLabel defines several constructors. Here are three of them:

● JLabel(Icon icon)
● JLabel(String str)
● JLabel(String str, Icon icon, int align)

Here, str and icon are the text and image icon used to display on the label.

Prof Vasanthalakshmi M, Dept of ISE, SCE 7


Module – 3 Swings

The align argument specifies the horizontal alignment of the text and/or icon within the dimensions of the label.
It must be one of the following values: LEFT, RIGHT, CENTER, LEADING, or TRAILING. These constants are defined
in the SwingConstants interface.

Notice that icons are specified by objects of type Icon, which is an interface defined by Swing. The easiest way to
obtain an icon is to use the ImageIcon class. ImageIcon implements Icon and encapsulates an image. Thus, an
object of type ImageIcon can be passed as an argument to the Icon parameter of JLabel’s constructor.

There are several ways to provide the image, including reading it from a file or downloading it from a URL.

ImageIcon() 🡪 Creates an uninitialized image icon.

ImageIcon(Image image) 🡪 Creates an ImageIcon from an image object.

ImageIcon(String filename) 🡪 Creates an ImageIcon from the specified file.

ImageIcon(URL location) 🡪 Creates an ImageIcon from the specified URL.

The icon and text associated with the label can be obtained by the following methods:

Icon getIcon( ) String getText( )

The icon and text associated with a label can be set by these methods:

void setIcon(Icon icon) void setText(String str)

Here, icon and str are the icon and text, respectively.

Therefore, using setText( ) it is possible to change the text inside a label during program execution.

JTextField

● JTextField is the simplest and most widely used Swing text component.
● JTextField allows you to edit one line of text.
● It is derived from JTextComponent, which provides the basic functionality common to Swing text
components.
● JTextField uses the Document interface for its model.

Three of JTextField’s constructors are shown here:

● JTextField(int cols)
● JTextField(String str, int cols)
● JTextField(String str)

Here, str is the string to be initially presented, and cols is the number of columns in the text field.
If no string is specified, the text field is initially empty. If the number of columns is not specified, the text field is
sized to fit the specified string.

JTextField generates several types ofevents in response to user interaction. Commonly handled events are:
Prof Vasanthalakshmi M, Dept of ISE, SCE 8
Module – 3 Swings

1. ActionEvent is fired when the user presses enter.


2. A CaretEvent is fired each time the caret (i.e., the cursor) changes position. (CaretEvent is packaged in
[Link].)

To obtain the string or text currently in the text field call getText( ).

The Swing Buttons

● Swing defines four types of buttons: JButton, JToggleButton, JCheckBox, and JRadioButton.
● All are subclasses of the AbstractButton class, which extends JComponent.
● AbstractButton contains many methods that allow you to control the behavior of buttons. For example,
you can define different icons that are displayed for the button when it is disabled, pressed, or selected.
● The text associated with a button can be read and written via the following methods:

String getText( ) void setText(String str)

Here, str is the text to be associated with the button.

The model used by all buttons is defined by the ButtonModel interface. A button generates an action event
when it is pressed.

JButton

● The JButton class provides the functionality of a push button.


● JButton allows an icon, a string, or both to be associated with the push button.

Three of its constructors are shown here:

● JButton(Icon icon)
● JButton(String str)
● JButton(String str, Icon icon)

Here, str and icon are the string and image icon used for the button.

When the button is pressed, an ActionEvent is generated.

Using the ActionEvent object passed to the actionPerformed( ) method of the registered ActionListener, you can
obtain the action command string associated with the button. By default, this is the string displayed inside the
button.

We can set and obtain the action command by calling setActionCommand( ) on the button and method
getActionCommand( ) on the event object. It is declared like this:

setActionCommand(String ) String getActionCommand( )

The action command identifies the button. Thus, when using two or more buttons within the same application,
the action command gives you an easy way to determine which button was pressed.

JToggleButton

Prof Vasanthalakshmi M, Dept of ISE, SCE 9


Module – 3 Swings

● A useful variation on the push button is called a toggle button.


● A toggle button looks just like a push button, but it acts differently because it has two states: pushed and
released.
● That is, when you press a toggle button, it stays pressed rather than popping back up as a regular push
button does.
● When you press the toggle button a second time, it releases (pops up).
● Therefore, each time a toggle button is pushed, it toggles between its two states.
● Toggle buttons are objects of the JToggleButton class.
● JToggleButton is a superclass for two other Swing components JCheckBox and JRadioButton that also
represent two-state controls.
● JToggleButton defines the basic functionality of all two-state components.

JToggleButton defines several constructors:

1. JToggleButton(): Creates an initially unselected toggle button without setting the text or image.
2. JToggleButton(Icon icon): Creates an initially unselected toggle button with the specified image but no text.
3. JToggleButton(Icon icon, boolean selected): Creates a toggle button with the specified image and selection
state, but no text.
4. JToggleButton(String text): Creates an unselected toggle button with the specified text.
5. JToggleButton(String text, boolean selected): Creates a toggle button with the specified text and selection
state.
6. JToggleButton(String text, Icon icon): Creates a toggle button that has the specified text and image, and
that is initially unselected.
7. JToggleButton(String text, Icon icon, boolean selected): Creates a toggle button with the specified text,
image, and selection state.

By default, the button is in the off position.

Like JButton, JToggleButton generates an action event each time it is pressed and also generates an item event.
Item event is used by those components that support the concept of selection. When a JToggleButton is pressed
in, it is selected. When it is popped out, it is deselected.

To handle item events, you must implement the ItemListener interface. Each time an item event is generated, it is
passed to the itemStateChanged( ) method defined by ItemListener. Inside itemStateChanged( ), the getItem( )
method can be called on the ItemEvent object to obtain a reference to the JToggleButton instance that
generated the event.

It is shown here:

● Object getItem( ) 🡪 A reference to the button is returned.

To determine a toggle button’s state , isSelected( ) method is called on the button that generated the event.

● boolean isSelected( ) It returns true if the button is selected and false otherwise

Few methods used on this component are :

● public void setText(String text): This method sets the text which appears on the button.

Prof Vasanthalakshmi M, Dept of ISE, SCE 10


Module – 3 Swings

● public void setSelected(boolean b): This method sets the state of the button. If b is true, the button will
appear selected and if b is false, the button will appear de-selected.
● public void setEnabled(boolean b): This method is used to disable the button. If b is false, the button will
be grayed out and will no more be clickable.
● public void addActionListener(ActionListener l): This method attached an action listener to this button
so that any action performed on this button can be catched at the backend.
● public void addItemListener(ItemListener l): This method attaches an item listener to this button so that
the selection and deselection of this button can be handled at the backend.

Check Boxes

The JCheckBox class provides the functionality of a check box. Its immediate superclass is JToggleButton, which
provides support for two-state buttons, as just described. Some of its constructors are shown here:

● JCheckBox(Icon i)
● JCheckBox(Icon i, boolean state)
● JCheckBox(String s)
● JCheckBox(String s, boolean state)
● JCheckBox(String s, Icon i)
● JCheckBox(String s, Icon i, boolean state)

Here, i is the icon for the button. The text is specified by s.

If state is true, the check box is initially selected. Otherwise, it is not.

The state of the check box can be changed via the following method: void setSelected(boolean state)

Here, state is true if the check box should be checked.

When the user selects or deselects a check box, an ItemEvent is generated. This is handled by itemStateChanged( )
method defined by ItemListener class.
The getItem( ) method gets the JCheckBox object that generated the event.
The easiest way to determine the selected state of a check box is to call isSelected( ) on the JCheckBox instance.

Radio Buttons:
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. Some of its constructors are
shown here:

● JRadioButton(Icon i)
● JRadioButton(Icon i, boolean state)
● JRadioButton(String s)
● JRadioButton(String s, boolean state)
● JRadioButton(String s, Icon i)
● JRadioButton(String s, Icon i, boolean state)

Here, i is the icon for the button. The text is specified by s. If state is true, the button is initially selected. Otherwise, it
is not.

Prof Vasanthalakshmi M, Dept of ISE, SCE 11


Module – 3 Swings

Radio buttons must be configured into a group. Only one of the buttons in that group can be selected at any time.
For example, if a user presses a radio button that is in a group, any previously selected button in that group is
automatically deselected. 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:

void add(AbstractButton ab) Here, ab is a reference to the button to be added to the group.

A JRadioButton generates action events, item events, and change events each time the button selection changes.
Most often, it is the action event that is handled, which means that you will normally implement the ActionListener
interface.

Inside the method actionPerformed( ) defined by ActionListener, we can use a number of different ways to
determine which button was selected.

● First, you can check its action command by calling getActionCommand( ). By default, the action command is
the same as the button label, but you can set the action command to something else by calling
setActionCommand( ) on the radio button.
● Second, you can call getSource( ) on the ActionEvent object and check that reference against the buttons.
● Third, you can check each radio button to find out which one is currently selected by calling isSelected( ) on
each button.

Finally, each button could use its own action event handler. Remember, each time an action event occurs, it means
that the button being selected has changed and that one and only one button will be selected.

Programs on each component along with its event handlers


Prof Vasanthalakshmi M, Dept of ISE, SCE 12
Module – 3 Swings

1. The program uses a JFrame container to hold an instance of a JLabel. The label displays a short text message.

import [Link].*;
class SwingDemo { SwingDemo() {

// Create a new JFrame container.


JFrame jfrm = new JFrame("A Simple Swing Application");

// Give the frame an initial size.


[Link](275, 100);

// Terminate the program when the user closes the application.


[Link](JFrame.EXIT_ON_CLOSE);

// Create a text-based label.


JLabel jlab = new JLabel(" Swing means powerful GUIs.");

// Add the label to the content pane.


[Link](jlab);

// Display the frame.


[Link](true);
}
public static void main(String[] args)
{
// Create the frame on the event dispatching thread.
[Link](new Runnable()
{ public void run()
{ new SwingDemo();
} });
}}

2. /*creates a JTextField and adds it to the content pane. When the user presses enter key, an action event is
generated. This is handled by displaying the text in a label.*/

// Demonstrate JTextField component

package tf;

import [Link].*;
import [Link].*;
import [Link].*;

public class Tf {
public Tf() {

Prof Vasanthalakshmi M, Dept of ISE, SCE 13


Module – 3 Swings

// Set up the JFrame.


JFrame jfrm = new JFrame("JTextFieldDemo");
[Link](new FlowLayout());
[Link](JFrame.EXIT_ON_CLOSE);
[Link](200, 220);

// Add a text field to content pane.


JTextField jtf = new JTextField(15);
[Link](jtf);

// Add a label.
JLabel jlab = new JLabel();
[Link](jlab);

// Handle action events.


[Link](new ActionListener() {
public void actionPerformed(ActionEvent ae) {

// Show text when user presses ENTER.


[Link]([Link]());
} });

// Display the frame.


[Link](true);
}

public static void main(String[] args) {


// Create the frame on the event dispatching thread.
[Link](
new Runnable() {
public void run() {
new Tf();
} } );
}
}
3. /*icon-based button. It displays 3 push buttons and a label. Each button displays an icon that represents a
picture. When a button is pressed, the name of that picture is displayed in the label.

Demonstrate JButton component */


package button;

import [Link].*;
import [Link].*;
import [Link].*;

public class Button implements ActionListener {


JLabel jlab;

Prof Vasanthalakshmi M, Dept of ISE, SCE 14


Module – 3 Swings

public Button() {

// Set up the JFrame.


JFrame jfrm = new JFrame("JButtonDemo");
[Link](new FlowLayout());
[Link](JFrame.EXIT_ON_CLOSE);
[Link](500, 450);

// Add buttons to content pane.


ImageIcon rose = new ImageIcon("C:/Users/Hp/Desktop/[Link]");
JButton jb = new JButton(rose);
[Link]("rose");
[Link](this);
[Link](jb);

ImageIcon bird = new ImageIcon("C:/Users/Hp/Desktop/[Link]");


jb = new JButton(bird);
[Link]("bird");
[Link](this);
[Link](jb);

ImageIcon fruit = new ImageIcon("C:/Users/Hp/Documents/[Link]");


jb = new JButton(fruit);
[Link]("fruit");
[Link](this);
[Link](jb);

// add the label to content pane.


jlab = new JLabel("Choose a picture");
[Link](jlab);

[Link]();
[Link](true);
}

public void actionPerformed(ActionEvent ae)


{
[Link]("You selected " + [Link]());
}
public static void main(String[] args) {
// Create the frame on the event dispatching thread.
[Link]( new Runnable() {
public void run() {
new Button(); } } ); }}

Prof Vasanthalakshmi M, Dept of ISE, SCE 15


Module – 3 Swings

4. /*Demonstrate ToggleButton. Create a togglebutton with text “on/off” on it. When it is pressed , the
corresponding event handler must display “ button is ON” on label and when the button is released , text
displayed is “Button is OFF” */

package tog;

import [Link].*;
import [Link].*;
import [Link].*;

public class Tog {


public Tog(){

JFrame jfrm = new JFrame("JToggleButtonDemo");


[Link](new FlowLayout());
[Link](JFrame.EXIT_ON_CLOSE);
[Link](200, 100);

JLabel jlab = new JLabel("Button is off.");

JToggleButton jtbn = new JToggleButton("On/Off");

[Link](new ItemListener() {
public void itemStateChanged(ItemEvent ie)
{
if([Link]())
[Link]("Button is on.");
else
[Link]("Button is off.");
} });

[Link](jtbn);
[Link](jlab);

[Link](true);
}

public static void main(String[] args) {


// Create the frame on the event dispatching thread.
[Link]( new Runnable() {
public void run() {
new Tog();
} } );
}
}

Prof Vasanthalakshmi M, Dept of ISE, SCE 16


Module – 3 Swings

/* 5 . It displays four check boxes and a label. When the user clicks a check box, an ItemEvent is generated. The text
for that check box is used to set the text inside the label. */

package chbox;

import [Link].*;
import [Link].*;
import [Link].*;

public class Chbox implements ItemListener {


JLabel jlab;
public Chbox() {

// Set up the JFrame.


JFrame jfrm = new JFrame("JCheckBoxDemo");
[Link](new FlowLayout());
[Link](JFrame.EXIT_ON_CLOSE);
[Link](450, 300);

// Add check boxes to the content pane.


JCheckBox cb = new JCheckBox("C");
[Link](this);
[Link](cb);

cb = new JCheckBox("C++");
[Link](this);
[Link](cb);

cb = new JCheckBox("Java");
[Link](this);
[Link](cb);

cb = new JCheckBox("Perl");
[Link](this);
[Link](cb);

// Create the label and add it to the content pane.


jlab = new JLabel("Select languages");
//[Link](100,100,150,20);
[Link](jlab);

// Display the frame.


[Link](true);
[Link](null);
}

Prof Vasanthalakshmi M, Dept of ISE, SCE 17


Module – 3 Swings

// Handle item events for the check boxes.


public void itemStateChanged(ItemEvent ie) {
JCheckBox cb = (JCheckBox)[Link]();
if([Link]())
[Link]([Link]() + " is selected");
else
[Link]([Link]() + " is cleared");
}

public static void main(String[] args)


{
// Create the frame on the event dispatching thread.
[Link]( new Runnable() {
public void run() {
new Chbox();
} } );
}
}

/*6. Demonstrate RadioButton. Three radio buttons are created. The buttons are then added to a button group.
Within the handler, get the text that is associated with the radio button and uses it to set the text within a label. */

import [Link].*;
import [Link].*;
import [Link].*;

public class JRadioButtonDemo implements ActionListener


{
JLabel jlab;
public JRadioButtonDemo() {

// Set up the JFrame.


JFrame jfrm = new JFrame("JRadioButtonDemo");
[Link](new FlowLayout());
[Link](JFrame.EXIT_ON_CLOSE);
[Link](250, 100);

// Create radio buttons and add them to content pane.

JRadioButton b1 = new JRadioButton("A");


[Link](this);
[Link](b1);

Prof Vasanthalakshmi M, Dept of ISE, SCE 18


Module – 3 Swings

JRadioButton b2 = new JRadioButton("B");


[Link](this);
[Link](b2);

JRadioButton b3 = new JRadioButton("C");


[Link](this);
[Link](b3);

// Define a button group.


ButtonGroup bg = new ButtonGroup();
[Link](b1);
[Link](b2);
[Link](b3);

// Create a label and add it to the content pane.


jlab = new JLabel("Select One");
[Link](jlab);

// Display the frame.


[Link](true); }

// Handle button selection.


public void actionPerformed(ActionEvent ae)
{
[Link]("You selected " + [Link]());
}

public static void main(String[] args) {


// Create the frame on the event dispatching thread.
[Link]( new Runnable()
{
public void run() {
new JRadioButtonDemo(); } } ); } }

8. Along with these programs , learn one Lab pgm on Alpha, Beta buttons

Prof Vasanthalakshmi M, Dept of ISE, SCE 19

You might also like