In this AWT Tutorial, we add CheckboxMenuItems & Separator to existing Menu. Here, we will convert MenuItem as CheckboxMenuItem and then handle the ItemEvent from it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
import java.awt.BorderLayout; import java.awt.CheckboxMenuItem; import java.awt.Color; import java.awt.Frame; import java.awt.Label; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 13: Implement Item Listener public class FrameWin extends Frame implements WindowListener, ActionListener, ItemListener { //Sample 01: Class Members Label lblDisplay; //Sample 11a: CheckBox Menu Items CheckboxMenuItem chkRect; CheckboxMenuItem chkPentagon; CheckboxMenuItem chkCircle; public FrameWin(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(400, 400); setLocation(100,100); addWindowListener(this); //Sample 02: Add a Label Control lblDisplay = new Label(); lblDisplay.setBackground(Color.LIGHT_GRAY); add(BorderLayout.SOUTH, lblDisplay); //Sample 03: Add menu Bar MenuBar AwtMenuBar = new MenuBar(); setMenuBar(AwtMenuBar); //Sample 04: Create Menus Menu mnuHelp = new Menu("Help"); Menu mnuDraw = new Menu("Draw"); AwtMenuBar.add(mnuDraw); AwtMenuBar.add(mnuHelp); //Sample 05: Create Menu Items MenuItem siLine = new MenuItem("Line"); MenuItem miLine = new MenuItem("Multi Line"); //Sample 11b: Modify MenuItem as CheckBoxMenu //Item & Have them as Class Member chkRect = new CheckboxMenuItem("Rectangle"); chkPentagon = new CheckboxMenuItem("Pentagon"); chkCircle = new CheckboxMenuItem("Circle"); //<= 11 MenuItem help = new MenuItem("F1 Help"); MenuItem about = new MenuItem("About Menu Demo"); //Sample 06: Add Menu Items to Menu mnuDraw.add(siLine); mnuDraw.add(miLine); //Sample 10: Add Separator => mnuDraw.addSeparator(); //<= 10 mnuDraw.add(chkRect); mnuDraw.add(chkPentagon); mnuDraw.add(chkCircle); mnuHelp.add(help); mnuHelp.add(about); //Sample 07: Set ActionCommand & register with Listeners siLine.setActionCommand("Draw Line"); miLine.setActionCommand("Draw Multi Line"); //Sample 12: Comment Action Commands //chkRect.setActionCommand("Draw Rectangle"); //chkPentagon.setActionCommand("Draw Pentagon"); //chkCircle.setActionCommand("Draw Circle"); //<= 12 help.setActionCommand("F1 Help"); about.setActionCommand("About Menu Demo"); //Sample 08: Add Action Listener siLine.addActionListener(this); miLine.addActionListener(this); //Sample 14:Change the Listener to Item Listener chkRect.addItemListener(this); chkPentagon.addItemListener(this); chkCircle.addItemListener(this); //<= 14 help.addActionListener(this); about.addActionListener(this); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } @Override public void actionPerformed(ActionEvent ae) { //Sample 09: Set the Label lblDisplay.setText(ae.getActionCommand()); } @Override public void itemStateChanged(ItemEvent e) { //Sample 15: Report What item is Selected CheckboxMenuItem menuClicked = (CheckboxMenuItem) e.getItemSelectable(); String Sel = ""; if (menuClicked.getState() == true) Sel = "Selected"; else Sel = "UnSelected"; String str = menuClicked.getLabel() + " Item " + Sel; lblDisplay.setText(str); } } |
Categories: AWT-Tube