User Tools

Site Tools


swing:combo_box

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revision Both sides next revision
swing:combo_box [2015/05/10 20:16]
gthanos created
swing:combo_box [2018/04/13 09:22]
gthanos [Νon-editable combo box]
Line 10: Line 10:
 ===== Listener Interface ===== ===== Listener Interface =====
  
-Ένα Combo Box συνδέεται με ένα ή περισσότερα αντικείμενα του τύπου  [[http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html|ActionListener]]. Για να εντοπίσετε την επιλογή του χρήστη αντί να κάνετε τα εξής+Ένα Combo Box συνδέεται με ένα ή περισσότερα αντικείμενα του τύπου  [[http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html|ActionListener]]. Για να εντοπίσετε την επιλογή του χρήστη αρκεί να κάνετε τα εξής (__με την προϋπόθεση ότι έχετε ορίσει τον κατάλληλο Listener για το ComboBox σας__):
 <code java> <code java>
 public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
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)