Java Swing provides graphical user interface (GUI) components for building desktop applications. It includes common widgets like buttons, text fields, labels and dialog boxes. JOptionPane allows building simple dialog boxes for input/output. Swing components are lightweight and platform-independent compared to the older AWT components. Common Swing components discussed include JFrame for windows, JButton for buttons, JLabel for text, JFileChooser for file selection and JCheckBox, JRadioButton for state buttons.
2. • A graphical user interface (GUI) presents a user-
friendly mechanism for interacting with an
application. These are sometimes called controls
or widgets—short for window gadgets. A GUI
component is an object with which the user
interacts via the mouse, the keyboard or another
form of input, such as voice recognition.
• Java’s so-called Swing GUI components from the
javax.swing package. We cover other
6. Simple GUI-Based Input/Output with
JOptionPane
• Most applications you use on a daily basis use windows or
dialog boxes (also called dialogs) to interact with the user.
For example, an e-mail program allows you to type and
read messages in a window the program provides. Dialog
boxes are windows in which programs display important
messages to the user or obtain information from the user.
Java’s JOptionPane class (package javax.swing) provides
prebuilt dialog boxes for both input and output. These are
displayed by invoking static JOptionPane methods. Program
presents a simple addition application that uses two input
dialogs to obtain integers from the user and a message
dialog to display the sum of the integers the user enters.
7. • // Addition.java
• // Addition program that uses JOptionPane for input and output.
• import javax.swing.JOptionPane; // program uses JOptionPane
• public class Addition
• {
• public static void main( String[] args )
• {
• // obtain user input from JOptionPane input dialogs
• String firstNumber =
• JOptionPane.showInputDialog( "Enter first integer" );
• String secondNumber =
• JOptionPane.showInputDialog( "Enter second integer" );
16. Non-AWT Upgraded Components
• In addition to offering replacements for all the
basic AWT components, the Swing component
• set includes twice as many new components.
22. • package hello;
• import javax.swing.*;
• class jpswrd
• {
• public void Testing()
• {
• JPasswordField pwd = new JPasswordField(10);
• int action = JOptionPane.showConfirmDialog(null, pwd,"Enter
Password",JOptionPane.OK_CANCEL_OPTION);
• if(action < 0)JOptionPane.showMessageDialog(null,"Cancel, X or escape key selected");
• else JOptionPane.showMessageDialog(null,"Your password is "+new
String(pwd.getPassword()));
• System.exit(0);
• }
• public static void main(String args[])
• {jpswrd p= new jpswrd();
• p.Testing();}
• }
25. JFrame
• A Frame is a top-level window with a title and
a border.
• Frame that adds support for the Swing
component architecture.
26. JFrame Features
It’s a window with title, border, (optional)
menu bar and user-specified components.
.It can be moved, resized, iconified.
.It is not a subclass of JComponent.
.Delegates responsibility of managing user-
specified components to a content pane, an
instance of JPanel.
27. Centering JFrame’s
• By default, a Jframe is displayed in the upper-
left corner of the screen. To display a frame
at a specified location, you can use the
setLocation(x, y) method in the JFrame class.
This method places the upper-left corner of a
frame at location (x, y).
28. Class constructors
• JFrame()
Constructs a new frame that is initially
invisible.
• JFrame(String title)
Creates a new, initially invisible Frame with
the specified title.
29. JButton
• A button is a component the user clicks to
trigger a specific action. A Java application
can use several types of buttons, including
command buttons, checkboxes, toggle
buttons and radio buttons.
30. • all the button types are subclasses of
AbstractButton (package javax.swing), which
declares the common features of Swing
buttons.
32. Buttons That Maintain State
• The Swing GUI components contain three
types of state buttons—JToggleButton,
• JCheckBox and JRadioButton—that have
on/off or true/false values. Classes
JCheckBox and JRadioButton are subclasses
of JToggleButton
33. JLabel
• A JLabel displays read-only text, an image, or
both text and an image.
34. JFileChooser
Allows the the user to choose a file
• import javax.swing.JFileChooser;
• public class swing_examples {
• public static void main(String args[])
• {
• JFileChooser fc = new JFileChooser();
• int returnVal = fc.showOpenDialog(null);
• if(returnVal == JFileChooser.APPROVE_OPTION)
• System.out.println("File: " + fc.getSelectedFile());
• }
• }
35. JCheckBox Class
• The class JCheckBox is an implementation of a
check box - an item that can be selected or
deselected, and which displays its state to the
user.
40. /*code for checkbox that display bold text*/
• font=new Font("",Font.BOLD,14);
• jTextField1.setFont(font);
• /*code for checkbox that display italic text*/
• font=new Font("",Font.ITALIC,14);
• jTextField1.setFont(font);
43. JRadio Button
• The class JRadioButton is an implementation
of a radio button - an item that can be
selected or deselected, and which displays its
state to the user.
45. Mathematical Operations using Radio
Buttons
• private void
jButton1ActionPerformed(java.awt.event.Acti
onEvent evt) {
• jTextField1.setText("");
• jTextField2.setText("");
• jTextField3.setText("");