0% found this document useful (0 votes)
28 views

Advanced Programming - Java: Dr. Hikmat A. M. Abdeljaber

This document discusses Java GUI components. It provides an example of creating a simple GUI application that displays labels with text and images using the JLabel component. It also discusses other common Swing GUI components like JTextField, JButton, JCheckBox, JComboBox and JList.

Uploaded by

Imtithal Saeed
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)
28 views

Advanced Programming - Java: Dr. Hikmat A. M. Abdeljaber

This document discusses Java GUI components. It provides an example of creating a simple GUI application that displays labels with text and images using the JLabel component. It also discusses other common Swing GUI components like JTextField, JButton, JCheckBox, JComboBox and JList.

Uploaded by

Imtithal Saeed
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
You are on page 1/ 18

Advanced Programming - Java

Dr. Hikmat A. M. AbdelJaber


 GUI enables you to interact friendly with application.
 Java provides several GUI components (controls)
inherited from class JComponent including JLabel,
JTextField, JButton, JCheckBox, JRadioButton,
JComboBox, JList, JMenu, JFileChooser, JDialog,
JPanel, JFrame, etc.
 GUI components = controls = widgets (windowgadgets)
import javax.swing.JOptionPane;

public class Addition


{
public static void main(String[] args)
{
String firstNumber = JOptionPane.showInputDialog("Enter first number");
String secondNumber = JOptionPane.showInputDialog("Enter second number");

int number1 = Integer.parseInt(firstNumber);


int number2 = Integer.parseInt(secondNumber);
int sum = number1 + number2;

JOptionPane.showMessageDialog(null, "The sum is “ + sum


,"Sum of two integers", JOptionPane.PLAIN_MESSAGE);
}
}
 Output
 Note: if you type non-integer value or click Cancel
button then run-time error will occur.
 showInputDialog and showMessageDialog methods are
used for I/O.
 parseInt method of the class Integer is used to convert
String to int value.
 null in showMessageDialog determines where to
position the dialog box (null indicates the dialog should
appear in the center of screen).
 Types of message dialog:

(default)
 Some swing GUI components: JLabel, JTextField,
Jbutton, JCheckBox, JComboBox, JList, Jpanel, …
 (Swing vs. AWT)
swing components are more portable than the AWT
components (applicant’s Feel-and-Look is same on
different platforms).
 (Lightweight vs. Heavyweight GUI Components)
Unlike AWT components, most swing components are
lightweight because they are not tied (not relied) on the
local platform’s Windowing system.
 (Superclass of swing components (JComponent))
Object
Component
Container Java.awt (heavyweight)
JComponent Javax.swing (lightweight)
Superclass of all lightweight components

 (Some features of JComponents)


 Pluggable Feel-and-Look.
 Mnemonics (shortcut keys).
 Tool tips (component description).
 Support for user-interface localization (display in
different languages).
JLabel component
 We will develop a project to create label with
1. text 2. image 3. text and image
 The project contains two programs (two classes):
1. One class uses JLabel component that extends JFrame
2. Another class for testing the first program (application)
import java.awt.FlowLayout; First Program
import javax.swing.Icon; LabelFrame.java
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class LabelFrame extends JFrame


{
private JLabel label1;
private JLabel label2;
private JLabel label3;

public LabelFrame()
{
super("Testing JLabel");
setLayout(new FlowLayout());
label1 = new JLabel ("Label with text");
label1.setToolTipText("This is label 1");
add(label1);

Icon pic = new ImageIcon(getClass().getResource("\\images\\cat.gif"));


label2 = new JLabel(pic);
label2.setToolTipText("This is label 2");
add(label2);

label3 = new JLabel();


label3.setText("Label with text and icon at bottom");
label3.setIcon(pic);
label3.setHorizontalTextPosition(SwingConstants.CENTER);
label3.setVerticalTextPosition(SwingConstants.BOTTOM);
label3.setToolTipText("This is label 3");
add(label3);
} // end constructor
} // end class LabelFrame
import javax.swing.JFrame; Second Program
LabelTest.java
public class LabelTest
{
public static void main(String[] args)
{
LabelFrame labelframe = new LabelFrame();
labelframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
labelframe.setSize(650, 400); // you can use setBound
labelframe.setVisible(true);
} // end main
} // end class LabelTest
Output
 JLabel is a subclass of JComponent.
 Layout Managers: help you position components.
 Types: FlowLayout: components are placed on a
container from left to right.
 setLayout is inherited indirectly from Container class.
 setToolTipText is inherited from JComponent class.
 add is used to add components to JFrame. It is inherited
indirectly from Container class.
 Icon is interface which manipulates icons.
 ImageIcon is used to locate and load images such as:
gif, jpg, jpeg, png.
 ImageIcon is a class implements interface Icon.
This means that ImageIcon is an Icon.
superclass subclass

Icon pic = new ImageIcon(…);


 getClass retrieves a reference represent the current class.
It is inherited from class Object.
 getResource returns the location of the image as URL.
The class ImageIcon uses this URL to locate the image
then loads it into memory.
 setText and setIcon are JLabel methods.
 setHorizontalTextPosition and setVerticalTextPosition
are JLabel methods use constants from SwingConstants.
For Horizontal Alignment: LEFT, CENTER, RIGHT.
For Vertical Alignment: TOP, CENTER, BOTTOM.
import java.awt.*;
import javax.swing.*;

public class GUI_Components extends JFrame


{
public GUI_Components()
{
super("Displaying GUI components");
setLayout(new FlowLayout());

JTextField textField = new JTextField("This is Text Field");


add(textField);

JButton button = new JButton("This is Button");


add(button);

JCheckBox checkBox = new JCheckBox("This is Check Box");


add(checkBox);

JRadioButton radioButton = new JRadioButton("This is Radio Button");


add(radioButton);
JComboBox comboBox = new JComboBox();
comboBox.addItem("Computer Science");
comboBox.addItem("Computer Engineering");
comboBox.addItem("Information System");
add(comboBox);

String[] colorNames = {"Black", "Blue", "Red", "Green", "Yellow", "White"};


JList list = new JList(colorNames);
list.setVisibleRowCount(3);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(new JScrollPane(list));
} // end constructor
public static void main(String[] args)
{
GUI_Components gui = new GUI_Components();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setBounds(300, 200, 900, 200);
gui.setVisible(true);
} // end main
} // end class

You might also like