Java AWT Choice holds List of Items. In this video, we will create an AWT example which adds and removes string from AWT Choice Control. We also see how to display selected item by handling the actionPerformed Handler and java.awt.event.ItemEvent.
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 |
package AwtDemoPkg; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Choice; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Label; import java.awt.Panel; import java.awt.TextField; 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 03: Implement Action Listener //Sample 08: Implement Item Listener for Choice public class FrameWindow extends Frame implements WindowListener, ActionListener, ItemListener { //Sample 01: Members private Choice choice; private TextField txtAdd; private TextField txtRemove; public FrameWindow(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(500, 200); setLocation(100,100); addWindowListener(this); //Sample 02: Supporting Controls for this Example //2.1 Create a Panel Panel MidPane = new Panel(); MidPane.setLayout(new FlowLayout()); //2.2 To Add a String txtAdd = new TextField(30); Button btnAdd = new Button("Add"); //2.3 To remove a String txtRemove = new TextField(30); txtRemove.setEnabled(false); Button btnRemove = new Button("Remove"); //Sample 04: Register to Listener, Set Action Cmd btnAdd.addActionListener(this); btnRemove.addActionListener(this); btnAdd.setActionCommand("Add"); btnRemove.setActionCommand("Remove"); //Sample 05: Add the Controls MidPane.add(txtAdd); MidPane.add(btnAdd); MidPane.add(txtRemove); MidPane.add(btnRemove); add(BorderLayout.CENTER,MidPane); //Sample 06: Create Choice Control choice = new Choice(); choice.add("France"); choice.add("Germany"); choice.add("Indonesia"); choice.add("India"); choice.add("United Kingdom"); add(BorderLayout.NORTH, choice); //Sample 10: Register Choice for ItemEvent choice.addItemListener(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 e) { //Sample 07: Implement Button Click Actions String ActionCommand = e.getActionCommand(); switch (ActionCommand) { case "Add": choice.add(txtAdd.getText()); break; case "Remove": String RemoveStr = txtRemove.getText(); if (RemoveStr.length() > 0) choice.remove(txtRemove.getText()); break; } } @Override public void itemStateChanged(ItemEvent e) { //Sample 09: Display the Selected String if (e.getItemSelectable() == choice) { txtRemove.setText(choice.getSelectedItem()); } } } |
- GridBagConstraints GridX, GridY, GridWidth & GridHeight #13
- TextArea – Append Text & Insert text #15
Categories: AWT-Tube
where is the main methods? I can’t find! Please Update!
The youtube video contains the pre-requisites which contains main method or how to use the examples.
To avoid repeted code on every post, we included only the code snippets required for the video.