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

Chapter 08 Advanced Gui New

This document discusses creating menus in a graphical user interface (GUI) using Java Swing components. It provides guidelines for creating menus with a JMenuBar, JMenu, and JMenuItem components. Specifically, it demonstrates how to add menus and menu items to a menu bar, attach the menu bar to a JFrame, and handle menu item selection events using an ActionListener. The document also provides an example GUI application that performs arithmetic operations and includes menus to select the operation.

Uploaded by

Adlina Dzul
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Chapter 08 Advanced Gui New

This document discusses creating menus in a graphical user interface (GUI) using Java Swing components. It provides guidelines for creating menus with a JMenuBar, JMenu, and JMenuItem components. Specifically, it demonstrates how to add menus and menu items to a menu bar, attach the menu bar to a JFrame, and handle menu item selection events using an ActionListener. The document also provides an example GUI application that performs arithmetic operations and includes menus to select the operation.

Uploaded by

Adlina Dzul
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter 8

Advanced Graphical User InterfaceJmenuItem

Prepared by : Puan Robiah Hamzah


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 :
JButton JCheckBoxMenuItem JMenu JRadioButtonMenuItem JLabel JTextComponent JTextField JComboBox JTextArea JList JScrollBar JSlider JToggleButton JCheckBox JRadioButton JPasswordField JComponent AbstractButton JMenuItem

Frequently used Swing GUI components


ISB16003-OOP Jan2010

: :

Creating menus

Java provides 5 classes that implement menus : 1) JMenuBar 2) 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

.
JFrame

Creating menus (cont)


frame = new JFrame(MyMenus);

1. Create a menu bar and associate it with a frame :


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


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));

separator

4. Create submenu items for Software submenu in Help menu


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);

3 explicit references here will be grouped later

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(); Group of mutually exclusive choices
ISB16003-OOP Jan2010

btg.add(jrbmiBlue); btg.add(jrbmiBlue); btg.add(jrbmiBlue);

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) 2) 3) 4) 5) 6) Create menu bar Add menu Operation and Exit to menu bar Add menu items to menu Operation and Exit Create p1 that holds the labels and textfields Create p2 that holds the buttons Add menu bar and the 2 panels to frame content pane

Done by example

Container container = getContentPane(); (A) container.add(jmb,BorderLayout.NORTH); container.add(p1, BorderLayout.CENTER); container.add(p2, BorderLayout.SOUTH);


7) 8) Register listener to each source object Handle ActionEvent from buttons and menu items i.

Today, add those statements in blue.. Buttons and menu items

(B)

String actionCommand = e.getActionCommand If (user clicks JButton) if (e.getSource() instanceof JButton) handle button event if(Add.equals(actionCommand)) else calculate(+); if (user select JMenuItem) else handle menu item event if(Substract.equals(actionCommand)) calculate(-); : ISB16003-OOP Jan2010

Guideline for GUI exercise (cont)


8) ii. Write the calculate() method

(C)
Obtain num1 and num2

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);

Set result in jtfResult

Get rid of blank spaces

Note : Dont forget to repeat code (B) for handling menu item event

9)

Write the main() with the usual content

ISB16003-OOP Jan2010

You might also like