Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 141 Further GUI Programming Learning Outcomes oExtend examples presented to write more useful applications. oWrite non-trivial, event-driven GUI applications.

Similar presentations


Presentation on theme: "Unit 141 Further GUI Programming Learning Outcomes oExtend examples presented to write more useful applications. oWrite non-trivial, event-driven GUI applications."— Presentation transcript:

1 Unit 141 Further GUI Programming Learning Outcomes oExtend examples presented to write more useful applications. oWrite non-trivial, event-driven GUI applications. Introduction Example 1: Enhancing the Telephone Handset Example Example 2: Menu Test Self-check Exercise 1 Example 3: File Dialog Test Example 4: Popup Menu Test Self-check Exercise 2 Exercises

2 Unit 142 Introduction This is our concluding session on GUIs and event-driven programming. In our discussion on GUI-based programs so far, we have covered: –GUI design issues, GUI components and containers. –Basic event handling. –Basic layout management. We now cover typical GUI elements found in Windows environment including: –Menus and pop-up menus –File dialogs – Mnemonics and keyboard accelerator With this coverage the student's knowledge base of the basics would be broadened.

3 Unit 143 Introduction to Example 1 This example builds on Example 4 of the preceding section. Example 4 of the preceding section developed a simple GUI for a telephone handset. We will now add event-handling code to display typed numbers. Here is a sample output of the program:

4 Unit 144 Example 1: The Telephone Handset 1 import java.awt.*; import javax.swing.*; import java.awt.event.*; 2 class TelephoneTest extends TestGridBagLayout 3 implements ActionListener{ 4 public TelephoneTest(){ 5 Component components[] = getContentPane().getComponents(); 6 JPanel cancelPanel = (JPanel)components[13]; 7 JButton cancel = (JButton)cancelPanel.getComponent(0); 8 for(int i=0;i<components.length; i++){ 9 if(components[i] instanceof JButton) 10 ((JButton)components[i]).addActionListener(this); 11 } 12 cancel.addActionListener(this); 13 } 14 public void actionPerformed(ActionEvent ae) { 15 if (ae.getActionCommand().equals("Cancel")) 16 display.setText(""); 17 else 18 display.setText(display.getText()+ae.getActionCommand()); 19 } 20 public static void main(String [] args){ 21 new TelephoneTest().setVisible(true); 22 }}

5 Unit 145 Introduction to Example 2 In this example we demonstrate how menus, separator, mnemonic and accelerators can be added into an application. The output of the example is as follows:

6 Unit 146 Class Hierarchy for Menus Classes used to define menus are: oJMenuBar oJMenuItem oJMenu oJCheckButtonMenuItem oJRadioButtonMenuItem

7 Unit 147 Details of Program Output

8 Unit 148 Example 2: Menus and Mnemonics 1 import java.awt.event.*; 2 import javax.swing.*; 3 class MenuTest extends JFrame { 4 private JMenuBar menuBar = new JMenuBar(); 5 protected JMenu fileMenu = new JMenu("File"); 6 protected JMenuItem neW, open, quit, save, print; 7 private JMenuItem saveCurrent, saveAs, saveAll; 8 MenuTest(String title){ 9 super(title); 10 setJMenuBar(menuBar); 11 menuBar.add(fileMenu); 12 fileMenu.setMnemonic('F'); 13 fileMenu.add(neW = new JMenuItem ("New")); 14 fileMenu.add(open = new JMenuItem ("Open")); 15 open.setMnemonic('o'); 16 fileMenu.add(save = new JMenu ("Save")); 17 save.add(saveCurrent = new JMenuItem ("Save Current")); 18 save.add(saveAs = new JMenuItem ("Save As")); 19 save.add(saveAll = new JMenuItem ("Save All")); 20 fileMenu.add(save); 21 fileMenu.add(print = new JCheckBoxMenuItem ("Print")); 22 fileMenu.addSeparator();

9 Unit 149 Menus and Mnemonics (cont’d) 23 fileMenu.add(quit = new JMenuItem("Quit")); 24 quit.setMnemonic(KeyEvent.VK_Q); 25 quit.setAccelerator( 26 KeyStroke.getKeyStroke(KeyEvent.VK_Q,KeyEvent.CTRL_MASK)); 27 quit.addActionListener(new ActionListener(){ 28 public void actionPerformed(ActionEvent e){ 29 int result=JOptionPane.showConfirmDialog(MenuTest.this, 30 "Quit/Close Menu Demos Window?"); 31 if (result == JOptionPane.YES_OPTION) 32 System.exit(0); 33 } 34 }); 35 setSize(300,300); 36 } 37 public static void main(String args[]){ 38 MenuTest t = new MenuTest("Menus Test"); 39 t.setVisible(true); 40 } 41 }

10 Unit 1410 Introduction to Example 3 This example extends Example 2 to launch a file dialog on selecting the Open menu. The selected file name is simply printed on another display window. The output is as follows:

11 Unit 1411 Example 3: File Dialog Test 1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 class FileDialogTest extends MenuTest { 5 JEditorPane textPane= new JEditorPane(); 6 FileDialogTest(String title){ 7 super(title); 8 open.addActionListener( new ActionListener() { 9 public void actionPerformed(ActionEvent e ) { 10 FileDialog fd = new FileDialog(FileDialogTest.this); 11 fd.setVisible(true); 12 textPane.setText("Selected file: "+fd.getFile()); 13 } }); 14 getContentPane().add(textPane); 15 } 16 public static void main(String args[]){ 17 FileDialogTest t = new FileDialogTest("File Dialog Test"); 18 t.setVisible(true); 19 20 } 21 }

12 Unit 1412 Introduction to Example 4 This example extends Example 2 of the preceding section to add pop-up menus.. The output looks like: The menu consists of three colors that can be used to reset a component’s background color.

13 Unit 1413 Example 4: Popup Menu Test 1 import java.awt.*; import java.awt.event.*; import javax.swing.*; 2 public class PopupMenuTest extends TestGridLayout{ 3 Component selectedComponent; 4 JPopupMenu colorMenu = new JPopupMenu(); 5 JMenuItem blue, white, yellow; 6 public PopupMenuTest(){ 7 setTitle("Popup Menu Test"); 8 colorMenu.add(blue=new JMenuItem("Blue")); 9 colorMenu.add(white=new JMenuItem("White")); 10 colorMenu.add(yellow=new JMenuItem("Yellow")); 11 Component components[] = getContentPane().getComponents(); 12 class MyListener extends MouseAdapter { 13 public void mousePressed(MouseEvent e){checkPopup(e);} 14 public void mouseClicked(MouseEvent e){checkPopup(e);} 15 public void mouseReleased(MouseEvent e){checkPopup(e);} 16 public void checkPopup(MouseEvent e) { 17 if (e.isPopupTrigger()){ 18 selectedComponent = e.getComponent(); 19 colorMenu.show(e.getComponent(),e.getX(),e.getY()); 20 } 21 } 22 }

14 Unit 1414 Popup Menu Test (cont’d) 23 MouseListener mylistener = new MyListener(); 24 for(int i=0;i<components.length-1; i++){ 25 JButton b = (JButton)components[i]; 26 b.addMouseListener(mylistener); 27 } 28 blue.addActionListener(new ActionListener () { 29 public void actionPerformed(ActionEvent ae){ 30 selectedComponent.setBackground(Color.blue); 31 }}); 32 white.addActionListener(new ActionListener() { 33 public void actionPerformed(ActionEvent ae){ 34 selectedComponent.setBackground(Color.white); 35 }}); 36 yellow.addActionListener(new ActionListener() { 37 public void actionPerformed(ActionEvent ae){ 38 selectedComponent.setBackground(Color.yellow); 39 }}); 40 } 41 public static void main(String[] args) { 42 new PopupMenuTest().setVisible(true); 43 } 44 }

15 Unit 1415 Review Exercises 1What are the event sources and event listeners in Example 1? 2Can the event handler in Example 1 be implemented in a single anonymous class? If not, why not? 3Modify Example 1 to use two inner classes instead of one. Can the two inner classes both be anonymous? Is the program with one inner class better than the one with two? Explain. 4Enhance Example 1 so that when the Redial button is pushed the last text displayed on the text component is displayed again. 5Write a Java program to illustrate how a component event can be handled. 6Modify Example 3 so that when a file is double-clicked from the file dialog indow the file is opened rather than displaying its name in the editor pane. 7When you run Example 4, you will find that the lower part of the window is not responding to the popup menu. Modify this program to ensure that that part of the window also responds to the popup menu events like the buttons in the window.


Download ppt "Unit 141 Further GUI Programming Learning Outcomes oExtend examples presented to write more useful applications. oWrite non-trivial, event-driven GUI applications."

Similar presentations


Ads by Google