Professional Documents
Culture Documents
Component and Layout Manager: Course: Business Application Development Effective Period: July 2017
Component and Layout Manager: Course: Business Application Development Effective Period: July 2017
Component and Layout Manager: Course: Business Application Development Effective Period: July 2017
Development
Effective Period : July 2017
Session 7
Learning Objectives
Outline
• Introduction
• Label
• Button
• Radio Button
• Scroll Bar
• Slider
• Check Box
• Text Area
• Text Field
• Password Field
• List View
• Combo Box
Introduction
Outline
• Introduction
• Label
• Button
• Radio Button
• Scroll Bar
• Slider
• Check Box
• Text Area
• Text Field
• Password Field
• List View
• Combo Box
Label (1)
• Keyword ”Jlabel”
• A label is a display area for short writings, an image or
both
• Inheritance of JComponent
Label (2)
• Code
• import javax.swing.*;
import java.awt.*;
public ButtonDemo(){
labelName = new JLabel("Full Name : ");
textName = new JTextField("Janz", 10);
buttonOk = new JButton("Submit");
textName.setEditable(true);
textName.setForeground(Color.RED);
textName.setHorizontalAlignment(SwingConstants.RIGHT);
panel.setLayout(new FlowLayout());
panel.add(labelName);
panel.add(textName);
panel.add(buttonOk);
add(panel);
buttonOk.addActionListener(this);
}
public static void main(String[] args) {
JFrame frame = new ButtonDemo();
frame.setTitle("Button Testing");
frame.setSize(300, 100);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Full name is "+ textName.getText());
}
}
Check Box and Radio Button (1)
• Keyword “JCheckBox”; “JRadioButton”
• Check Box
– It allows user to choose more than 1 choices
• Radio Button
– User only allows to choose 1 choice
• Implement interface ItemListener and ActionListener
• Override:
– actionPerformed(ActionEvent e) for ActionListener
– itemStateChanged(ItemEvent e) for ItemListener
Check Box and Radio Button (2)
• Code
• import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public RadioCheckDemo(){
setLayout(new FlowLayout());
panel1.setLayout(new GridLayout(3,1));
panel1.add(rdRed = new JRadioButton("Red"));
panel1.add(rdYellow = new JRadioButton("Yellow"));
panel1.add(rdGreen = new JRadioButton("Green"));
group.add(rdRed);
group.add(rdYellow);
group.add(rdGreen);
panel2.setLayout(new GridLayout(3,1));
panel2.add(checkRed = new JCheckBox("Red"));
panel2.add(checkYellow = new JCheckBox("Yellow"));
panel2.add(checkGreen = new JCheckBox("Green"));
add(panel1);
add(panel2);
}
public static void main(String[] args) {
JFrame frame = new RadioCheckDemo();
frame.setTitle("Text Testing");
frame.setSize(300, 150);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Combo Boxes and List
• Keyword “JComboBox”; “JList”
• User only allows to have 1 answer
• Choices can be displayed in a form of list or dropdown
list
• For dropdown list:
– Implement interface ItemListener
– Overrid
• For list:
– Implement interface ListSelectionListener (package
javax.swing.event)
– Overtide valueChanged (ActionEvent e)
Check Box and Radio Button (2)
• Code
• import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
Bina Nusantara
ISYS6197 19