Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views27 pages

Module 3 - Notes

The document discusses the Swing API, which is a set of GUI components built on top of AWT to create Java-based applications, overcoming AWT's limitations such as platform dependency and heavyweight components. It highlights key features of Swing, including its lightweight nature and pluggable look and feel (PLAF), as well as various components and containers used in Swing applications. Additionally, it provides examples of event handling and the creation of Swing applets, showcasing the use of various Swing components like JButton, JLabel, and JTextField.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views27 pages

Module 3 - Notes

The document discusses the Swing API, which is a set of GUI components built on top of AWT to create Java-based applications, overcoming AWT's limitations such as platform dependency and heavyweight components. It highlights key features of Swing, including its lightweight nature and pluggable look and feel (PLAF), as well as various components and containers used in Swing applications. Additionally, it provides examples of event handling and the creation of Swing applets, showcasing the use of various Swing components like JButton, JLabel, and JTextField.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

.

Module-3

Swings
Basics
Limitations of AWT API

AWT translates its various visual components into their corresponding, platform-specific equivalents, or peers. Therefore, the look and
feel will be decided by the platform and not by Java. Therefore, AWT is referred to as heavyweight. This led to following problems

 Component might act differently on different platforms


 Look and feel of each component was fixed and could not be easily changed by the program.
 It caused some restrictions on usage of the components.

Definition

Swing API is set of extensible GUI Components to ease developer's life to create JAVA based Front End/ GUI Applications. It is built on
top of AWT API and it overcomes most of its limitations. Swing has almost every control corresponding to AWT controls.

AWT versus Swing

AWT Swing
AWT components are platform-dependent Swing are platform independent
AWT is called the abstract window tool Swing is part of the java foundation classes
AWT components are heavyweight components Swing components are lightweight components because
swing sits on the top of AWT
AWT occupies more memory space Swing occupies less memory space
AWT require javax.awt package Swing requires javax.swing package
AWT is not MVC based Swing are MVC based architecture
AWT works slower Swing works faster

4.2
Swing features

Here are the two key features of swing.

Lightweight

Swing component are independent of native Operating System's API as Swing API controls are
rendered mostly using pure JAVA code instead of underlying operating system calls.

Pluggable look and feel (PLAF)

Swing based GUI Application’s look and feel logic can be separated from the component’s business
logic.

Downloaded by Prathima Mahapurush


.

Advantages of PLAF

 It is possible to define look and feel that is consistent across all platforms.
 Conversely, it is also possible to create a look and feel that acts like a specific platform.
 It is also possible to create a custom look and feel.
 Look and feel can be changed dynamically at runtime.

Other features of Swing

 Rich controls
Swing provides a rich set of advanced controls like Tree, TabbedPane, slider, colorpicker, table controls

 Highly Customizable
Swing controls can be customized in very easy way as visual appearance is independent of internal representation

4.3
Components and Containers

Components

 A component is an independent visual control such as push button or a slider.


 In general, all the swing components are derived from JComponent class (apart from four top
level containers).
 JComponent class inherits the AWT classes Container and Component

Containers

 A container holds a group of components. Thus, container is a special type of component that is designed
to hold other components.
 There are two types of containers
 Top level containers (JFrame, JApplet, JWindow, and JDialog):
These containers do not inherit the JComponent. They do directly inherit the AWT classes
Component and Container. Therefore, they are
heavyweight. They cannot be contained within any other
component.
 Those who inherit JComponent are the second type of
container: They are lightweight

Here are some of the Swing components:

JApplet JButton JCheckBox JCheckBoxMenuItem


JColorChooser JComboBox JComponent JDesktopPane
JDialog JEditorPane JFileChooser JFormattedTextFiel
d
JFrame JInternalFrame JLabel JLayeredPane
JList JMenu JMenuBar JMenuItem
JOptionPane JPanel JPasswordField JPopupMenu
JProgressBar JRadioButton JRadioButtonMenuItem JRootPane
JScrollBar JScrollPane JSeparator JSlider

Downloaded by Prathima Mahapurush


.

JSpinner JSplitPane JTabbedPane JTable


JTextArea JTextField JTextPane JTogglebutto
n
JToolBar JToolTip JTree JViewport
JWindow

4.4
The Swing packages

Java SE6 defines following swing packages

javax.swing
javax.swing.border
javax.swing.colorchooser
javax.swing.event
javax.swing.filechooser
javax.swing.plaf
javax.swing.plaf.basic
javax.swing.plaf.metal
javax.swing.plaf.multi
javax.swing.plaf.synth
javax.swing.table
javax.swing.text
javax.swing.text.html
javax.swing.text.html.pars
er javax.swing.text.rtf
javax.swing.tree
javax.swing.undo

4.5
A Simple Swing Application

import javax.swing.*;

class Example
{
Example()
{
// Create a new JFrame container.
JFrame frame = new JFrame("My first Swing Application");

// Give the frame an initial size.


frame.setSize(400, 200);

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


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create a text-based label.


JLabel label = new JLabel("WELCOME TO THE WORLD OF SWINGS. !!");

// Add the label to the content pane.


frame.add(label);

// Display the frame.


frame.setVisible(true);
}

Downloaded by Prathima Mahapurush


.

Downloaded by Prathima Mahapurush


.

public static void main(String args[])


{

new Example();
}
}

Output

4.6
Event Handling

Basics

Change in the state of an object is known as event i.e. event describes the change in state of source. Events are generated as result
of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse,
entering a character through keyboard, selecting an item from list, scrolling the page are the activities that causes an event to
happen.

Foreground events: These events require the direct interaction of the user. Example: Click on a button
Background events: These don’t require the interaction of the user. Example: OS interrupt.

As we learnt in unit 3, delegation event model has the following key participants namely:

 Event: Event is an object that describes the state change.


 Event Source: An Event Source is an object which originates or "fires" events.
 Event Listener: A listener is an object which will be notified when an event occurs.

Event handling example:

import java.awt.*;
import
java.awt.event.*;
import javax.swing.*;

public class Example


{

private JFrame mainFrame;


private JLabel headerLabel;
private JLabel statusLabel;
private JPanel
controlPanel;

Downloaded by Prathima Mahapurush


.

public Example()

Downloaded by Prathima Mahapurush


.

{
render();
}

public static void main(String[] args)


{
Example swingControlDemo = new Example();
swingControlDemo.showEventDemo();
}

private void render()


{
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400, 400);
mainFrame.setLayout(new GridLayout(3, 1));

headerLabel = new JLabel("", JLabel.CENTER);


statusLabel = new JLabel("", JLabel.CENTER);

statusLabel.setSize(350, 100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new
FlowLayout());

mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}

private void showEventDemo()


{
headerLabel.setText("Control in action: Button");

JButton okButton = new JButton("OK");


JButton submitButton = new
JButton("Submit"); JButton cancelButton =
new JButton("Cancel");

okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");

okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());

controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);

mainFrame.setVisible(true);
}

private class ButtonClickListener implements ActionListener


{
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if (command.equals("OK"))

Downloaded by Prathima Mahapurush


.

{
statusLabel.setText("Ok Button clicked.");
}
else if (command.equals("Submit"))
{
statusLabel.setText("Submit Button clicked.");
}
else
{
statusLabel.setText("Cancel Button clicked.");
}
}
}
}

Output

Steps involved in event handling

1. The User clicks the button and the event is generated.


2. Now the object of concerned event class is created automatically and information about the source and the event
get populated with in same object.
3. Event object is forwarded to the method of registered listener class.
4. The method now gets executed and returns.

In order to design a listener class we have to develop some listener interfaces. These Listener interfaces forecast some public abstract
callback methods which must be implemented by the listener class.

If you do not implement the any if the predefined interfaces then your class can not act as a listener class for a source object.

Downloaded by Prathima Mahapurush


.

4.7
Create a Swing Applet
Concept

Swing-based applets are similar to AWT-based applets, but it extends JApplet rather than Applet. JApplet is derived from Applet.
Thus, JApplet includes all of the functionality found in Applet and adds support for Swing.

Swing applets use the same four lifecycle methods as described in unit 2: init(), start(), stop(), and destroy()

Painting is accomplished differently in Swing than it is in the AWT, and a Swing applet will not normally override the paint() method.

Example:

import javax.swing.*;
import java.awt.*;
import
java.awt.event.*;

/*
This HTML can be used to launch the applet:
<object code="MySwingApplet" width=220 height=90>
</object>
*/
public class Example extends JApplet
{
JButton
button1;
JButton
button2; JLabel
label;

// Initialize the applet.


public void init()
{
render(); // initialize the GUI
}

// This applet does not need to override start(), stop(),


// or destroy().
// Set up and initialize the GUI.
private void render()
{
// Set the applet to use flow layout.
setLayout(new FlowLayout());
// Make two buttons.
button1 = new
JButton("Button1"); button2 =
new JButton("Button2");
// Add action listener for Alpha.
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le)
{
label.setText("Button1 was pressed.");
}
});
// Add action listener for Beta.
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le)
{
label.setText("Button2 was pressed.");
}
});
// Add the buttons to the content pane.

Downloaded by Prathima Mahapurush


.

add(button1);
add(button2);

Downloaded by Prathima Mahapurush


.

// Create a text-based label.


label = new JLabel("Press a button.");
// Add the label to the content pane.
add(label);
}
}

Output

Let’s discuss some of the lightweight components derived from JComponent class.

4.8
JLabel and ImageIcon

Description

A JLabel object provides text instructions or information on a GUI — display a single line of read-only text, an image or both text and
image.

JLabel defines three constructors

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

Here,
str and icon are the text and icon used for the label.
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, along with several others used by
the Swing classes.

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

Example:

import javax.swing.*;

@SuppressWarnings("serial"

)
public class Example extends JApplet
{
public void init()
{
render();

Downloaded by Prathima Mahapurush


.

private void render()


{
// Create an icon.
ImageIcon imgIcon = new ImageIcon("logo.png");

// Create a label.
JLabel label = new JLabel("This is a sample message", imgIcon, JLabel.LEFT);

// Add the label to the content pane.


add(label);
}
}

Output

4.9
JTextField

Description

JTextField is an input area where the user can type in characters. If you want to let the user enter multiple lines of text, you
cannot use JTextField’s unless you create several of them. The solution is to use JTextArea, which enables the user to enter
multiple lines of text.

When the user types data into them and presses the Enter key, an action event occurs. If the program registers an event liste ner, the
listener processes the event and can use the data in the text field at the time of the event in the program

JTextField defines three constructors

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

Here,
str is the string to be initially presented, and

Mr. Ashok Kumar K | 9742024066 | [email protected]

Downloaded by Prathima Mahapurush


.

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.

Example

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial"

)
public class Example extends JApplet
{
JTextField textfield;

public void init()


{
render();
}

private void render()


{
// Change to flow layout.
setLayout(new FlowLayout());

// Add text field to content pane.


textfield = new JTextField(15);
add(textfield);

textfield.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
// Show text when user presses ENTER.
showStatus(textfield.getText());
}
});
}
}

Output

Downloaded by Prathima Mahapurush


.

4.10
The Swing buttons

Swing defines four types of buttons:


JButton,
JToggleButton,
JCheckBox, and
JRadioButton.
All are subclasses of the AbstractButton class, which extends JComponent

The text associated with the button can be read and written via the following methods

String getText()
void setText(String str)

A button generates an action event when it is pressed. Other events are also possible.

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. When the button is pressed, an ActionEvent is generated and it is handled by the
actionPerformed() method of registered ActionListener
It defines three constructors

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

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

Example:

import java.awt.*;
import java.awt.event.*;

Downloaded by Prathima Mahapurush


.

import javax.swing.*;

@SuppressWarnings("serial")
public class Example extends JApplet implements ActionListener
{
JLabel label;

public void init()


{
render();
}

private void render()


{
// Change to flow layout.
setLayout(new FlowLayout());

// Add buttons to content pane.


ImageIcon aklcIcon = new ImageIcon("aklc.png");
JButton button1 = new JButton(aklcIcon);
button1.setActionCommand("AKLC");
button1.addActionListener(this);
add(button1);

ImageIcon jmasterIcon = new ImageIcon("jmaster.png");


JButton button2 = new JButton(jmasterIcon);
button2.setActionCommand("JMASTER");
button2.addActionListener(this);
add(button2);

// Create and add the label to content


pane. label = new JLabel("Choose an
institute"); add(label);
}

// Handle button events.


public void actionPerformed(ActionEvent ae)
{
label.setText("You selected " + ae.getActionCommand());
}
}

Output

Downloaded by Prathima Mahapurush


.

JToggleButton
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. It generates an ItemEvent.

Example:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class Example extends JApplet
{
JToggleButton toggleButton;

public void init()


{
render();
}

private void render()


{
// Change to flow layout.
setLayout(new FlowLayout());

// Make a toggle button.


toggleButton = new JToggleButton("ON");

// Add an item listener for the toggle button.


toggleButton.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ie)
{
if (toggleButton.isSelected())
toggleButton.setText("OFF");
else

Downloaded by Prathima Mahapurush


.

} toggleButton.setText("ON");
});

// Add the toggle button and label to the content


pane. add(toggleButton);
}
}

Output

JCheckBox
The JCheckBox class provides the functionality of a check box. When the user selects or deselects a check box, an
ItemEvent is generated.

Example

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class Example extends JApplet implements ItemListener
{
JLabel label;

public void init()


{
render();
}

private void render()


{
// Change to flow layout.

Downloaded by Prathima Mahapurush


.

setLayout(new FlowLayout());

// Add check boxes to the content pane.


JCheckBox cb = new JCheckBox("C");
cb.addItemListener(this);
add(cb);

cb = new JCheckBox("C++");
cb.addItemListener(this);
add(cb);

cb = new JCheckBox("Java");
cb.addItemListener(this);
add(cb);
// Create the label and add it to the content
pane. label = new JLabel("Select languages");
add(label);
}

// Handle item events for the check boxes.


public void itemStateChanged(ItemEvent ie)
{
JCheckBox cb = (JCheckBox) ie.getItem();
if (cb.isSelected())
label.setText(cb.getText() + " is selected");
els
e label.setText(cb.getText() + " is cleared");
}
}

Output

JRadioButton
Radio buttons are a group of mutually exclusive buttons, in which only one button can be selected at any one time.

Example

import java.awt.*;

Downloaded by Prathima Mahapurush


.

import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class Example extends JApplet implements ActionListener
{
JLabel label;

public void init()


{
render();
}

private void render()


{
// Change to flow layout.
setLayout(new FlowLayout());

// Create radio buttons and add them to content


pane. JRadioButton b1 = new JRadioButton("Male");
b1.addActionListener(this);
add(b1);

JRadioButton b2 = new JRadioButton("Female");


b2.addActionListener(this);
add(b2);

// Define a button group.


ButtonGroup bg = new
ButtonGroup(); bg.add(b1);
bg.add(b2);

// Create a label and add it to the content


pane. label = new JLabel("Select your Gender");
add(label);
}

// Handle button selection.


public void actionPerformed(ActionEvent ae)
{
label.setText("You selected " + ae.getActionCommand());
}
}

Output

Downloaded by Prathima Mahapurush


.

4.11
JTabbedPane

Description

A JTabbedPane contains a tab that can have a tool tip and a mnemonic, and it can display both text and an image.

Procedure to use a tabbed pane


 Create an instance of JTabbedPane.
 Add each tab by calling addTab()
 Add the tabbed pane to the content pane.

Example:

import javax.swing.*;

@SuppressWarnings("serial"

)
public class Example extends JApplet
{
public void init()
{
render();
}

private void render()


{
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Sem 1", new Sem1());
tabbedPane.addTab("Sem 2", new Sem2());
add(tabbedPane);
}
}

// Make the panels that will be added to the tabbed pane.


@SuppressWarnings("serial")
class Sem1 extends JPanel
{
public Sem1()
Downloaded by Prathima Mahapurush
.

{
JButton b1 = new JButton("Mathematics 1");
add(b1);
JButton b2 = new JButton("CCP");
add(b2);
JButton b3 = new JButton("Basic
Electronics"); add(b3);
JButton b4 = new JButton("Chemistry");
add(b4);
}
}

@SuppressWarnings("serial")
class Sem2 extends JPanel
{
public Sem2()
{
JButton b1 = new JButton("Mathematics 2");
add(b1);
JButton b2 = new JButton("Civil");
add(b2);
JButton b3 = new JButton("Basic
Electricals"); add(b3);
JButton b4 = new JButton("Physics");
add(b4);
}
}

Output

4.12
JScrollPane

Description

JScrollPane is a lightweight container that automatically handles the scrolling of another component. The component being scrolled
can either be an individual component, such as a table, or a group of components contained within another lightweight container, such
as a JPanel. In either case, if the object being scrolled is larger than the viewable area, horizontal and/or vertical scroll bars are
automatically provided, and the component can be scrolled through the pane. Because JScrollPane automates scrolling, it
usually eliminates the need to manage individual scroll bars

Downloaded by Prathima Mahapurush


.

Steps to use a scroll pane


 Create the component to be scrolled.
 Create an instance of JScrollPane, passing to it the object to be scrolled.
 Add the scroll pane to the content pane

Example

import java.awt.*;

import javax.swing.*;

@SuppressWarnings("serial"

)
public class Example extends JApplet
{
public void init()
{
render();
}

private void render()


{
// Add 400 buttons to a panel.
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(20, 20));
int b = 0;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
jp.add(new JButton("Button " + b));
++b;
}
}

// Create the scroll pane.


JScrollPane jsp = new
JScrollPane(jp);

// Add the scroll pane to the content pane.


// Because the default border layout is used,
// the scroll pane will be added to the
center. add(jsp, BorderLayout.CENTER);
}
}

Output

Downloaded by Prathima Mahapurush


.

4.13
JList

Description

JList supports the selection of one or more items from a list.

Example

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;

@SuppressWarnings("serial"

)
public class Example extends JApplet
{
JList<Object> list;
JLabel label;
JScrollPane
scrollpane;

// Create an array of cities.


String Cities[] = { "Bangalore", "Mysore", "Mandya", "Hassan", "Kolar",
"Shimoga", "Chikamagalur", "Dakshina Kannda", "Madikeri",
"Gulbarga", "Dharwad", "Belgaum" };

public void init()


{
render();
}

private void render()


{
// Change to flow layout.
setLayout(new FlowLayout());// Create a
JList. list = new JList<Object>(Cities);
// Set the list selection mode to single selection.
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Add the list to a scroll pane.

Downloaded by Prathima Mahapurush


.

scrollpane = new JScrollPane(list);

Downloaded by Prathima Mahapurush


.

// Set the preferred size of the scroll pane.


scrollpane.setPreferredSize(new Dimension(120, 90));
// Make a label that displays the selection.
label = new JLabel("Choose a City");
// Add selection listener for the list.
list.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent le)
{
// Get the index of the changed item.
int idx = list.getSelectedIndex();
// Display selection, if item was selected.
if (idx != -1)
label.setText("Current selection: " + Cities[idx]);
els
e // Otherwise, reprompt.
label.setText("Choose a
} City");
});
// Add the list and label to the content
pane. add(scrollpane);
add(label);
}
}

Output

4.14
JComboBox

Description

JComboBox is a combination of a text field and a drop-down list. A combo box normally displays one entry, but it will also display a drop-
down list that allows a user to select a different entry.

Example

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

Downloaded by Prathima Mahapurush


.

@SuppressWarnings("serial")
public class Example extends JApplet
{
JLabel label;
JComboBox<Object> comboBox;
String flags[] = { "aklc", "jmaster" };

public void init()


{
render();
}

private void render()


{
// Change to flow layout.
setLayout(new FlowLayout());

// Instantiate a combo box and add it to the content pane.


comboBox = new JComboBox<Object>(flags);
add(comboBox);

// Handle selections.
comboBox.addActionListener(new
ActionListener() {
public void actionPerformed(ActionEvent ae)
{
String s = (String)
comboBox.getSelectedItem();
} label.setIcon(new ImageIcon(s + ".png"));
});

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


label = new JLabel(new ImageIcon("aklc.png"));
add(label);
}
}

Output

4.15
JTable

Downloaded by Prathima Mahapurush


.

Description

JTable 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. Depending on its configuration, it is also possible to select a row, column, or cell within the
table, and to change the data within a cell.

Steps to setup a simple JTable.


 Create an instance of JTable.
 Create a JScrollPane object, specifying the table as the object to scroll.
 Add the table to the scroll pane
 Add the scroll pane to the content pane.

Example

import javax.swing.*;
/*
<applet code="JTableDemo" width=400 height=200>
</applet>
*/
@SuppressWarnings("serial"
)
public class Example extends JApplet
{
public void init()
{
render();
}

private void render()


{
// Initialize column headings.
String[] colHeads = { "USN", "Name", "Gender" };
// Initialize data.
Object[][] data = { { 7, "Vidya", "Female" }, { 9, "Ashok", "Male" },
{ 12, "Manoj", "Male" }, { 44, "Rekha", "Female" } };

JTable table = new JTable(data, colHeads);


// Add the table to a scroll pane.
JScrollPane jsp = new
JScrollPane(table);
// Add the scroll pane to the content pane.
} add(jsp);
}

Output

Downloaded by Prathima Mahapurush

You might also like