Menus are added to menu bar Menus and menu items are added to menus Creating

Menus are added to menu bar menus and menu items are

This preview shows page 56 - 67 out of 76 pages.

Menus are added to menu bar. Menus and menu items are added to menus.
Image of page 56

Subscribe to view the full document.

Creating Menu Bar 57 A menu bar component is a horizontal menu. Using JMenuBar() JMenuBar is added into a frame object. It forms the root of all menu trees Using <frameObject>.setJMenuBar() A frame displays only one MenuBar at a time.
Image of page 57
Example Code : JMenuBar 58 import javax.swing.JFrame; import javax.swing.JMenuBar; public class MenuBarDemo { JFrame frame; JMenuBar menuBar; public MenuBarDemo() { frame = new JFrame("Menu Bar - Demo"); menuBar = new JMenuBar(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(menuBar); frame.setSize(200,100); frame.setVisible(true); } public static void main(String[] args) { MenuBarDemo mbDemo = new MenuBarDemo(); } }
Image of page 58

Subscribe to view the full document.

Creating a Menu - JMenu 59 The menu component provides a basic pull- down menu. Using JMenu(String); It is added to menu bar or another menu component. Menu bar Menu
Image of page 59
Creating Menu Item - JMenuItem 60 Menu item components are the text leaf nodes of a menu tree. Using JMenuItem(String) Menu items are added to a menu to complete it. Menu item
Image of page 60

Subscribe to view the full document.

Menu Item With Check Box 61 Menu item with check box is a checkable menu item. User can have selections (on or off choices) Using JCheckBoxMenuItem(String,boolean) JCheckboxMenuItem is using ItemListener interface. itemStateChanged() method is called when the checkbox state is modified. Check box menu item
Image of page 61
HOW THINGS WORK IN JAVA GUI? Lesson 5 >> Lesson 5 6 2
Image of page 62

Subscribe to view the full document.

Event Delegation Event is an object described the happening when user perform action at GUI level Eg: To close window when user clicks on exit button The component that generated the event object is known as the event source Eg: Exit button Event handler decipher event object message and process user interaction Window is closing when user clicks on exit button The events and handlers are defined in package java.awt.event.* . >> Lesson 5 6 3
Image of page 63
Event Delegation Model Events are sent to components from source of event. The component will propagate the event to the registered class called listeners . Listener receives and process the event. Listener are classes that implements EventListener interface >> Lesson 5 6 4
Image of page 64

Subscribe to view the full document.

Steps to Code Event Delegation 1. import java.awt.event.* 2. implements the appropriate interface from the defined class Eg : implements ActionListener 3. Register GUI component to the appropriate listener Eg : button.addActionListener(this); 4. Implements interface method Eg : public void actionPerformed(ActionEvent e){…} >> Lesson 5 6 5
Image of page 65
User Action, Source Object, Event Type >> Lesson 5 6 6 Component Event Description Listener Interface JButton ActionEvent User clicked button ActionListener JCheckBox, JCheckBoxMenuItem ItemEvent User toggled a checkbox ItemListener JComboBox, JList ItemEvent ActionEvent User clicked a list item User double click a list item ItemListener ActionListener JMenu ActionEvent User selected menu item ActionListener JTextField ActionEvent User typed Enter key ActionListener
Image of page 66

Subscribe to view the full document.

Image of page 67
  • Spring '17
  • Henry
  • Widgets, ActionListener

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes