User Tools

Site Tools


swing:combo_box

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
swing:combo_box [2018/04/13 09:15]
gthanos
swing:combo_box [2018/04/13 09:22]
gthanos [Νon-editable combo box]
Line 23: Line 23:
  
 <code java ComboBoxDemo.java> <code java ComboBoxDemo.java>
- 
 import java.awt.*; import java.awt.*;
 import java.awt.event.*; import java.awt.event.*;
Line 36: Line 35:
    images/Pig.gif    images/Pig.gif
  */  */
 +@SuppressWarnings({"serial", "unchecked"})
 public class ComboBoxDemo extends JPanel public class ComboBoxDemo extends JPanel
               implements ActionListener {               implements ActionListener {
Line 47: Line 47:
     //Create the combo box, select the item at index 4.     //Create the combo box, select the item at index 4.
     //Indices start at 0, so 4 specifies the pig.     //Indices start at 0, so 4 specifies the pig.
-    JComboBox petList = new JComboBox(petStrings);+    JComboBox<String> petList = new JComboBox<>(petStrings);
     petList.setSelectedIndex(4);     petList.setSelectedIndex(4);
     petList.addActionListener(this);     petList.addActionListener(this);
Line 71: Line 71:
   /** Listens to the combo box. */   /** Listens to the combo box. */
   public void actionPerformed(ActionEvent e) {   public void actionPerformed(ActionEvent e) {
-    JComboBox cb = (JComboBox)e.getSource();+    JComboBox<String> cb = (JComboBox<String>)e.getSource();
     String petName = (String)cb.getSelectedItem();     String petName = (String)cb.getSelectedItem();
     updateLabel(petName);     updateLabel(petName);
swing/combo_box.txt · Last modified: 2018/04/13 08:27 (external edit)