In this Java AWT Tutorial, we will learn how to create and use Multi-Select List Control. We will also show all the selected items from this Multi-Select List to TextArea component.
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 |
package AwtDemoPkg; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.List; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 01: Get Action Listner Support public class FrameWindow extends Frame implements WindowListener, ActionListener { //Sample 02: Class Members Button btnGetFruits; Button btnClear; TextArea ta; List awtList; public FrameWindow(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(500, 250); setLocation(100,100); addWindowListener(this); //Sample 03: Create AWT List Control awtList = new List(5, true); awtList.add("Apple"); awtList.add("Orange"); awtList.add("Banana"); awtList.add("Grapes"); awtList.add("Pine Apple"); awtList.add("Strawberry"); awtList.add("Jack Fruit"); awtList.add("Papaya"); awtList.add("Water Melon"); awtList.add("Mango"); //Sample 04: Create supporting Controls ta = new TextArea(5, 50); btnGetFruits = new Button("Get Fruits"); btnClear = new Button("Clear Output"); //Sample 05: Set the Layout and Add Controls setLayout(new FlowLayout()); add(awtList); add(btnGetFruits); add(btnClear); add(ta); //Sample 06: Register Button With Listener btnGetFruits.addActionListener(this); btnClear.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 e) { //Sample 07: Get the Selected Fruits From //Multi-Select AWT List if(e.getSource() == btnGetFruits) { String[] SelectedFruits = awtList.getSelectedItems(); if (SelectedFruits.length == 0) { ta.append("No Friut Selected\r\n" ); return; } for(String fruit: SelectedFruits) ta.append(fruit + "\r\n"); } //Sample 08: Clear Text Area if(e.getSource() == btnClear) ta.setText(""); } } |
Categories: AWT-Tube