chapter 08 advanced gui new
TRANSCRIPT
ISB16003-OOP Jan2010
Chapter 8
Prepared by :Puan Robiah Hamzah
Advanced Graphical User Interface- JmenuItem
ISB16003-OOP Jan2010
Objectives
Create menus using components JMenuBar, JMenu, JMenuItem, JCheckBoxMenuItem and JRadioButton
ISB16003-OOP Jan2010
Swing components
In previous lesson, you were introduced to only two components (JButton, JTextField) and several containers (JFrame and JPanel)
Here are some of the Swing components :
AbstractButton
JButton
JToggleButtonJCheckBox
JRadioButton
JLabel
JTextComponent
JComponent
JComboBox
JSlider
JScrollBar
JList
JTextArea
JTextField
Frequently used Swing GUI components
JMenuItem
JCheckBoxMenuItem
JMenu
JRadioButtonMenuItem
JPasswordField
::
…
ISB16003-OOP Jan2010
Creating menus
Java provides 5 classes that implement menus : 1) JMenuBar2) JMenu 3) JMenuItem 4) JCheckBoxMenuItem
5) JRadioButtonMenuItem Menu bar hold the menus Menu hold the menu items (select or toggle on/off) A menu item can be an instance of 3, 4 or 5 above
ISB16003-OOP Jan2010
.
1. Create a menu bar and associate it with a frame :
JFrame frame = new JFrame(“MyMenus);
frame.setSize(300,200);
frame.setVisible(true);
JMenuBar jmb = new JMenuBar();
frame.setJMenuBar(jmb); Attach a menu bar to a frame
2. Create menus and associate them with the menu bar
JMenu fileMenu = new JMenu(“File”);
JMenu helpMenu = new JMenu(“Help”);
jmb.add(fileMenu);
jmb.add(helpMenu);
Add two menus into the menu bar
Creating menus (cont)
ISB16003-OOP Jan2010
Creating menus (cont)
3. Create menus items and add them to the menu (File menu)
fileMenu.add(new JMenuItem(“New”));
fileMenu.add(new JMenuItem(“Open”));
fileMenu.addSeparator();
fileMenu.add(new JMenuItem(“Print”));
fileMenu.addSeparator();
fileMenu.add(new JMenuItem(“Exit”));
4. Create submenu items for “Software” submenu in Help menu
separator
JMenu swHelpSubMenu = new JMenu(”Software”);
helpMenu.add(swHelpSubMenu);
swHelpSubMenu.add(new JMenuItem(“Unix”));
swHelpSubMenu.add(new JMenuItem(“NT”));
ISB16003-OOP Jan2010
Creating menus (cont)
5. Create check box menu items and add into Help menu
helpMenu.add(new JCheckBoxMenuItem(“Check it”));
6. Create radio button menu items
JMenu clrHelpSubMenu = new JMenu(”Color”);
helpMenu.add(clrHelpSubMenu);
JRadioButtonMenuItem jrbmiBlue,jrbmiYellow, jrbmiRed;
clrHelpSubMenu.add(jrbmiBlue = new JRadioButtonMenuItem(“Blue”));
clrHelpSubMenu.add(jrbmiYellow = new JRadioButtonMenuItem(“Yellow”));
clrHelpSubMenu.add(jrbmiRed = new JRadioButtonMenuItem(“Red”));
ButtonGroup btg = new ButtonGroup();
btg.add(jrbmiBlue);
btg.add(jrbmiBlue);
btg.add(jrbmiBlue);
Group of mutually exclusive choices
3 explicit references here will be grouped later
ISB16003-OOP Jan2010
Menu item event handling
The menu item generates ActionEvent. Your program must implement the ActionListener and actionPerformed handler to respond to the menu selection.
The following is an example :public void actionPerformed(ActionEvent e)
{ String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JMenuItem)
{ if (“New” equals(actionCommand))
respondToNew(); //responds to “New” menu item
else
:
}
ISB16003-OOP Jan2010
An example of GUI application Create menus using components JMenuBar, JMenu and JMenuItem:
Write a program that create a user interface to perform arithmetic.The interface contains labels and text fields for Number 1, Number 2 and Result. The Result textfield displays the result of the arithmetic operation between Number 1 and Number 2. Figure 2 below is the sample output of the program. You have to include the menu bar, menu and menu item as part of the interface.
ISB16003-OOP Jan2010
Guideline for GUI exercise
import java.awt.*;import java.awt.event.*;import javax.swing.*;
public class MyMenuDemo extends JFrame implements ActionListener {// Text fields for Number 1, Number 2, and Result private JTextField jtfNum1, jtfNum2, jtfResult;
// Buttons "Add", "Subtract", "Multiply" and "Divide" private JButton jbtAdd, jbtSub, jbtMul, jbtDiv;
// Menu items "Add", "Subtract", "Multiply","Divide" and "Close" private JMenuItem jmiAdd, jmiSub, jmiMul, jmiDiv, jmiClose;
ISB16003-OOP Jan2010
Guideline for GUI exercise (cont)public MyMenuDemo() {
1) Create menu bar
2) Add menu “Operation” and “Exit” to menu bar
3) Add menu items to menu “Operation” and “Exit”
4) Create p1 that holds the labels and textfields
5) Create p2 that holds the buttons
6) Add menu bar and the 2 panels to frame content pane
Container container = getContentPane();container.add(jmb,BorderLayout.NORTH);container.add(p1, BorderLayout.CENTER);container.add(p2, BorderLayout.SOUTH);
7) Register listener to each source object
8) Handle ActionEvent from buttons and menu items
i. If (user clicks JButton)
handle button event
else
if (user select JMenuItem)
handle menu item event
Buttons and menu items
String actionCommand = e.getActionCommandif (e.getSource() instanceof JButton) if(“Add”.equals(actionCommand)) calculate(‘+’); else if(“Substract”.equals(actionCommand)) calculate(‘-’); :
Today, add those statements in blue..
(A)
(B)
Done by example
ISB16003-OOP Jan2010
Guideline for GUI exercise (cont)8) ii. Write the calculate() method
private void calculate (char operator){ int num1 = (Integer.parseInt(jtfNum1.getText().trim())); int num2 = (Integer.parseInt(jtfNum1.getText().trim())); int result = 0;
switch(operator) { case ’+’: result= num1+num2; break; : } jtfResult.setText(String.valueOf(result);
Obtain num1 and num2
Set result in jtfResult
Note : Don’t forget to repeat code (B) for handling menu item event
9) Write the main() with the usual content
(C)
Get rid of blank spaces