Gui1 58
Gui1 58
Java Swing
• Java Swing is a part of Java Foundation Classes (JFC) that is used to create
window-based applications. It is built on the top of AWT (Abstract
Windowing Toolkit) API and entirely written in java.
• The javax.swing package provides classes for java swing API such as JButton,
JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
What is JFC?
The Java Foundation Classes (JFC) are a set of GUI components which simplify the
development of desktop applications.
Hierarchy of Java Swing classes
Commonly used Methods of Component class
Method Description
JButton
• The JButton class is used to create a labeled button
• A method to be called when the user clicks (the thing you want to happen as a
• A way to know when to trigger that method. In other words, a way to know
• A listener interface is the bridge between the listener and event source (the
button).
• Swing GUI components are event sources.
• An event source is an object that can turn user actions (click a mouse, type a
key, close a window) into events.
• An event is represented as an object. An object of some event class.
ActionListener Interface
• It is callback mechanism
• It is notified whenever you click on the button or menu item. It is notified
against ActionEvent. The ActionListener interface is found in
java.awt.event package.
• It has only one method:
public abstract void actionPerformed(ActionEvent e);
The actionPerformed() method is invoked automatically whenever you click on the
registered component.
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JLabel(String s, Icon i, Creates a JLabel instance with the specified text, image,
int horizontalAlignment) and horizontal alignment.
Methods Description
String getText() returns the text string that a label displays.
void setText(String text) It defines the single line of text this component
will display.
import javax.swing.*;
public class ButtonTest{
ButtonTest(){
JFrame f=new JFrame("Button Example");
JButton b=new JButton(new ImageIcon(“c:\\icon.png"));
b.setBounds(100,100,100, 40);
f.add(b);
f.setSize(300,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ButtonTest(); }
}
JDialog
• The JDialog control represents a top level window with a border and a title used
to take some form of input from the user. It inherits the Dialog class.
Modal means that no other window can be used or activated while the
corresponding JDialog is being displayed.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DialogTest {
private static JDialog d;
DialogTest() {
JFrame f= new JFrame();
d = new JDialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
JButton b = new JButton ("OK");
b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogTest.d.setVisible(false);
} });
d.add( new JLabel ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogTest();
}
}
d.add( new JLabel ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogTest();
}
}
JTextField
• It is a text component that allows the editing of a single line text.
• It inherits JTextComponent class.
Constructor Description
setSize(400,400);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
try{
String host=tf.getText();
String ip=java.net.InetAddress.getByName(host).getHostAddress();
l.setText("IP of "+host+" is: "+ip);
}catch(Exception ex){System.out.println(ex);}
}
public static void main(String[] args) {
new LabelTest();
}}
import javax.swing.*;
import java.awt.event.*;
public class TextFieldTest implements ActionListener{
JTextField tf1,tf2,tf3;
JButton b1,b2;
TextFieldTest(){
JFrame f= new JFrame();
tf1=new JTextField();
tf1.setBounds(50,50,150,20);
tf2=new JTextField();
tf2.setBounds(50,100,150,20);
tf3=new JTextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new JButton("+");
b1=new JButton("+");
b1.setBounds(50,200,50,50);
b2=new JButton("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);
f.setSize(300,300);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}else if(e.getSource()==b2){
c=a-b;
}
String result=String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args) {
new TextFieldTest();
}}
JTextArea
• a multi line region that displays text. It allows the editing of multiple line text.
Constructor Description
JTextArea() Creates a text area that displays no text initially.