2012-02-25 9 views
4

ユーザーが選択した選択を返すために使用されるメソッドはありますか?最後の行が追加され値を返すJComboBox

JPanel ageSelection = new JPanel(); 
JLabel age = new JLabel("Age:"); 

ArrayList<Integer> ageList = new ArrayList<Integer>(); 

for (int i = 1; i <= 100; ++i) { 
    ageList.add(i); 
} 

DefaultComboBoxModel<Integer> modelAge = new DefaultComboBoxModel<Integer>(); 
for (Integer i : ageList) { 
    modelAge.addElement(i); 
} 

JComboBox<Integer> ageEntries = new JComboBox<Integer>(); 
ageEntries.setModel(modelAge); 

ageEntries.addActionListener(new putInTextListener()); 

ageSelection.add(age); 
ageSelection.add(ageEntries); 


class putInTextListener implements ActionListener { 
    public void actionPerformed (ActionEvent event) { 
     ageEntries.getSelectedItem(); 
    } 
} 

ageEntries.getSelectedItem();)、私はエラーを取得する:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

任意のアイデア?

編集コード:

class putInAgeListener implements ItemListener { 
    public void itemStateChanged(ItemEvent e) { 

     Object myAge = ageEntries.getSelectedItem(); 

     String myAgeData = myAge.toString(); 

     int i = Integer.parseInt(myAgeData); 

     System.out.print(i); 

    } 
} 

答えて

5

1)この文は空で、おそらくあなたは、現在選択されてItem

Integer/Object/String myWhatever = ageEntries.getSelectedItem(); 

2からInteger/Object/String値を取得したい)、より良いではなく、JComboBoxに使用ItemListenerだろうActionListener、通知ItemListener発砲イベントSELECTED/DESELECTED、常に2回

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class ComboBoxListeners { 

    private JFrame f; 
    private JComboBox flyFromCombo; 
    private JComboBox flyToCombo; 
    private JLabel tripLabel = new JLabel(); 
    private Object[] itemsFrom; 
    private Object[] itemsTo; 

    public ComboBoxListeners() { 
     itemsFrom = new Object[]{"-", "First - From", "Second - From", "Third - From"}; 
     itemsTo = new Object[]{"-", "First - To", "Second - To", "Third - To"}; 
     //flyFromCombo.setPrototypeDisplayValue("################################################"); 
     flyFromCombo = new JComboBox(itemsFrom); 
     flyFromCombo.addItemListener(new ItemListener() { 

      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if ((e.getStateChange() == ItemEvent.SELECTED)) { 
        String str = flyFromCombo.getSelectedItem().toString(); 
        String str1 = flyToCombo.getSelectedItem().toString(); 
        setLabelText(str, str1); 
       } 
      } 
     }); 
     flyToCombo = new JComboBox(itemsTo); 
     flyToCombo.addItemListener(new ItemListener() { 

      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if ((e.getStateChange() == ItemEvent.SELECTED)) { 
        String str = flyFromCombo.getSelectedItem().toString(); 
        String str1 = flyToCombo.getSelectedItem().toString(); 
        setLabelText(str, str1); 
       } 
      } 
     }); 
     tripLabel.setPreferredSize(new Dimension(400, 30)); 
     f = new JFrame("ComboBox ItemListeners"); 
     f.setLayout(new GridLayout(0, 1, 15, 15)); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(flyFromCombo); 
     f.add(flyToCombo); 
     f.add(tripLabel); 
     f.setLocation(150, 150); 
     f.pack(); 
     f.setVisible(true); 
    } 

    private void setLabelText(String str1, String str2) { 
     String textForLabel = ""; 
     String helpStringFirst = str1.trim(); 
     if (helpStringFirst != null && helpStringFirst.length() > 0) { 
      if (!helpStringFirst.equals("-")) { 
       textForLabel = "Flight No57. from : " + helpStringFirst; 
      } else { 
       textForLabel = "Flight from Un-Know : "; 
      } 
     } 
     String helpStringSecond = str2.trim(); 
     if (helpStringSecond != null && helpStringSecond.length() > 0) { 
      if (!helpStringSecond.equals("-")) { 
       textForLabel = textForLabel + " --> to : " + helpStringSecond; 
      } else { 
       textForLabel += " to : Un-Know "; 
      } 
     } 
     final String pushTextForLabel = textForLabel; 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       tripLabel.setText(pushTextForLabel); 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       ComboBoxListeners comboBoxListeners = new ComboBoxListeners(); 
      } 
     }); 
    } 
} 

EDIT

私は持っていない(とあまりしたくない)JDK7、

参考

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import java.util.ArrayList; 
import javax.swing.*; 

public class ComboBoxListeners { 

    private JFrame f; 
    private JComboBox flyFromCombo; 
    private JLabel tripLabel = new JLabel(); 

    public ComboBoxListeners() { 
     ArrayList<Integer> ageList = new ArrayList<Integer>(); 
     for (int i = 1; i <= 100; ++i) { 
      ageList.add(i); 
     } 
     DefaultComboBoxModel modelAge = new DefaultComboBoxModel(); 
     for (Integer i : ageList) { 
      modelAge.addElement(i); 
     } 
     flyFromCombo = new JComboBox(modelAge); 
     flyFromCombo.addItemListener(new ItemListener() { 

      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if ((e.getStateChange() == ItemEvent.SELECTED)) { 
        String str = flyFromCombo.getSelectedItem().toString(); 
        tripLabel.setText("Selected Age From JComboBox is : " + str); 
       } 
      } 
     }); 
     tripLabel.setPreferredSize(new Dimension(400, 30)); 
     f = new JFrame("ComboBox ItemListeners"); 
     f.setLayout(new GridLayout(0, 1, 15, 15)); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(flyFromCombo); 
     f.add(tripLabel); 
     f.setLocation(150, 150); 
     f.pack(); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       ComboBoxListeners comboBoxListeners = new ComboBoxListeners(); 
      } 
     }); 
    } 
} 
+0

私はまだエラーが発生します...私は編集しましたが、それでも動作しません。 JComboBoxに何が置かれているかに問題はありますか? – Maydayfluffy

+0

あなたは例外を受け取ります。空の星のようなオプションがあります。私の編集 – mKorbel

+0

のエラーは、ageEntries.getSelectedItem()行からのものです。この行は、ユーザーが選択したものをとり、テキストファイルのような場所に配置するためのものでした。私の編集したコードはこれを行う別の試みですが、それも失敗しました。 – Maydayfluffy

3

は、ここにジェネリックを示し@ mKorbelのexampleのばらつきがありますJava 7のJComboBoxおよびComboBoxModelに追加されたパラメータです。また、Java 7で利用可能な新しいの推論機能が使用されます。これはdさらにType Inference for Generic Instance Creationに記載されています。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

/** @see https://stackoverflow.com/a/9440487/230513 */ 
public class ComboBoxListener { 

    private JFrame f = new JFrame("ComboBox ItemListener"); 
    private JPanel panel = new JPanel(); 
    private JComboBox<Integer> combo; 
    private JLabel label = new JLabel("Please select a number from above."); 

    public ComboBoxListener() { 
     DefaultComboBoxModel<Integer> model = new DefaultComboBoxModel<>(); 
     for (int i = 1; i <= 100; ++i) { 
      model.addElement(i); 
     } 
     combo = new JComboBox<>(model); 
     combo.addItemListener(new ItemListener() { 

      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if ((e.getStateChange() == ItemEvent.SELECTED)) { 
        Integer result = (Integer) combo.getSelectedItem(); 
        label.setText(result.toString()); 
       } 
      } 
     }); 
     f = new JFrame("ComboBox ItemListener"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     panel.setLayout(new GridLayout(0, 1, 5, 5)); 
     panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     panel.add(combo); 
     panel.add(label); 
     f.add(panel); 
     f.setLocationByPlatform(true); 
     f.pack(); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       ComboBoxListener cbl = new ComboBoxListener(); 
      } 
     }); 
    } 
} 
+0

Java7 +1ありがとう – mKorbel