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
Previous revision
Next revision Both sides next revision
swing:combo_box [2015/05/18 07:00]
gthanos [Listener Interface]
swing:combo_box [2018/04/13 09:22]
gthanos [Νon-editable combo box]
Line 20: Line 20:
 Παρακάτω δίνονται δύο παραδείγματα από το site της Oracle, ένα για //non-editable// combo box και ένα για //editable// combo box. Παρακάτω δίνονται δύο παραδείγματα από το site της Oracle, ένα για //non-editable// combo box και ένα για //editable// combo box.
  
-==== Νon-editable combo box ====+===== Νon-editable combo box =====
  
 <code java ComboBoxDemo.java> <code java ComboBoxDemo.java>
-package components; 
- 
 import java.awt.*; import java.awt.*;
 import java.awt.event.*; import java.awt.event.*;
Line 37: 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 48: 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 72: 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);
Line 78: Line 77:
  
   protected void updateLabel(String name) {   protected void updateLabel(String name) {
-    ImageIcon icon = createImageIcon("images/"name + ".gif");+    ImageIcon icon = createImageIcon(name + ".gif");
     picture.setIcon(icon);     picture.setIcon(icon);
     picture.setToolTipText("A drawing of a " + name.toLowerCase());     picture.setToolTipText("A drawing of a " + name.toLowerCase());
Line 133: Line 132:
 Το παραπάνω πρόγραμμα προϋποθέτει τις παρακάτω εικόνες {{:swing:animals.zip|}}. Το παραπάνω πρόγραμμα προϋποθέτει τις παρακάτω εικόνες {{:swing:animals.zip|}}.
  
-==== Εditable combo box ====+===== Εditable combo box =====
  
 <code java ComboBoxDemo2.java> <code java ComboBoxDemo2.java>
- 
-package components; 
  
 import java.awt.*; import java.awt.*;
Line 267: Line 264:
  
  
 +| Προηγούμενο: [[:swing:lists | Λίστες - Η κλάση JList ]] | [[:toc | Περιεχόμενα ]] | Επόμενο: [[:swing:panels | Η κλάση JPanel ]] |
  
swing/combo_box.txt · Last modified: 2018/04/13 08:27 (external edit)