2016-06-23 34 views
0

私は現在、選択したアイテムをコンボボックスに別の値に設定しようとしています。この例では靴のサイズを使用していますが、靴のサイズを選択して靴のサイズを選択し、開始ボタンを押してサイズコードを読みたいとします。ComboBoxの選択項目を別の値に設定するにはどうすればよいですか?

これは私が達成しようとしているものです:

http://imgur.com/a/ItnSq

これは私が持っているものです。

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

public class ComboBoxDemo extends JFrame implements ActionListener{ 
    //declarations 
    JButton btnPay = new JButton("Start"); 

    /*ShoeSizeArray 
    String[] comboLabels = {"5", "5.5","6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "10", "10.5", "11", "11.5", 
    "12.5", "13.5", "14", "14.5", "15.5", "16"};*/ 

    //Sizecode Array 
    String[] comboLabels = {"560", "570", "580", "590", "600", "610", "620", "630", "640", "650", "660", "670", "680", "690", "700", "710", 
    "720", "730", "740", "750", "760", "770"}; 
    JComboBox <String> combo = new JComboBox<String>(comboLabels); 
    JTextArea display = new JTextArea(5,20); 

    //constructor 
    public ComboBoxDemo(){ 
    super("Combo box"); 
    //panel for button and combobox 
    JPanel buttonPanel = new JPanel(); 
    //add panel to frame 
    add(buttonPanel, BorderLayout.SOUTH); 
    //add button and combobox to panel 
    buttonPanel.add(btnPay); 
    buttonPanel.add(combo); 
    //register button with ActionListener 
    btnPay.addActionListener(this); 
    //add text area to center of frame 
    add(display, BorderLayout.CENTER); 
    } 
    public void actionPerformed(ActionEvent e){ 
    String sizecode = (String)combo.getSelectedItem(); 
    display.append("\nYou selected the sizecode " + sizecode); 
    } 

    public static void main(String[] args){ 
    JFrame fr = new ComboBoxDemo(); 
    fr.setLocationRelativeTo(null); 
    fr.setSize(200,200); 
    fr.setVisible(true); 
    } 
} 
+0

あなたはどのような問題に直面していますか? – Beniton

+0

現時点では、選択したインデックス値をComboBoxから別の値に設定する方法がわかりません。これは私が現在抱えていることです。 http://i.imgur.com/nwn3FS0.png – Mxxm

+0

私はまだあなたが達成しようとしているものを得ていません。開始ボタンをクリックすると、選択したコンボボックスの値が表示されます。今あなたは何を変えたいですか? – Beniton

答えて

1

あなたはSHOESIZEクラスを作成し、選択に基づいてラベルを設定します。

以下のコードを参考にしてください。

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 

public class ComboBoxDemo extends JFrame implements ActionListener { 
    // declarations 
    JButton btnPay = new JButton("Start"); 

    // ShoeSizeArray 
    String[] sizes = { "5", "5.5", "6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "10", "10.5", "11", "11.5", "12.5", 
      "13.5", "14", "14.5", "15.5", "16" }; 

    // Sizecode Array 
    String[] comboLabels = { "560", "570", "580", "590", "600", "610", "620", "630", "640", "650", "660", "670", "680", 
      "690", "700", "710", "720", "730", "740", "750", "760", "770" }; 
    JComboBox<Shoesize> combo = new JComboBox<Shoesize>(); 
    JTextArea display = new JTextArea(5, 20); 

    // constructor 
    public ComboBoxDemo() { 
     super("Combo box"); 
     // panel for button and combobox 
     JPanel buttonPanel = new JPanel(); 
     // add panel to frame 
     add(buttonPanel, BorderLayout.SOUTH); 
     for (int i = 0; i < sizes.length; i++) { 
      Shoesize size = new Shoesize(Double.parseDouble(sizes[i]), comboLabels[i]); 
      combo.addItem(size); 
     } 
     // add button and combobox to panel 
     buttonPanel.add(btnPay); 
     buttonPanel.add(combo); 
     // register button with ActionListener 
     btnPay.addActionListener(this); 
     // add text area to center of frame 
     add(display, BorderLayout.CENTER); 
    } 

    public void actionPerformed(ActionEvent e) { 
     Shoesize sizecode = (Shoesize) combo.getSelectedItem(); 
     display.append("\nYou selected the sizecode " + sizecode.label); 
    } 

    public static void main(String[] args) { 
     JFrame fr = new ComboBoxDemo(); 
     fr.setLocationRelativeTo(null); 
     fr.setSize(200, 200); 
     fr.setVisible(true); 
    } 

    class Shoesize { 
     double size; 
     String label; 

     public Shoesize(double size, String label) { 
      super(); 
      this.size = size; 
      this.label = label; 
     } 

     @Override 
     public int hashCode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result + getOuterType().hashCode(); 
      long temp; 
      temp = Double.doubleToLongBits(size); 
      result = prime * result + (int) (temp^(temp >>> 32)); 
      return result; 
     } 

     @Override 
     public boolean equals(Object obj) { 
      if (this == obj) 
       return true; 
      if (obj == null) 
       return false; 
      if (getClass() != obj.getClass()) 
       return false; 
      Shoesize other = (Shoesize) obj; 
      if (!getOuterType().equals(other.getOuterType())) 
       return false; 
      if (Double.doubleToLongBits(size) != Double.doubleToLongBits(other.size)) 
       return false; 
      return true; 
     } 

     private ComboBoxDemo getOuterType() { 
      return ComboBoxDemo.this; 
     } 

     @Override 
     public String toString() { 
      return String.valueOf(size); 
     } 

    } 
} 
+0

これは本当にうまくいったけど、サイズが必要です。 Shoesizeクラスでは文字列にするだけで、変数宣言でデータ型をStringに変更するだけで、コンストラクタは正常ですが、コードはhashcode()で破損します。 コードを壊さずに、サイズ変数をStringに変更するにはどうすればよいですか。 なぜ私はサイズの変数を文字列にしたいのだろうと思っているのでしょうか?後でこれをtoStringメソッドに渡しているからです。 – Mxxm

+0

トップメッセージを無視してください。すべてが正常に動作していますが、間違いがあります。あなたの助けを借りてベニトンに感謝します! – Mxxm

+0

クール。あなたは私の答えを受け入れることができますか?-)? – Beniton

1

あなたは単にボックスからインデックスにアクセスし、次に配列をナビゲートすることができます。

String[] shoeSizes = {"5", "5.5", "6", "6.5", "7", "7.5", "8", "8.5", "9", "9.5", "10", "10.5","11", "11.5", "12.5", "13.5", "14", "14.5", "15.5", "16"}; 
String[] shoeCodes = {"560", "570", "580", "590", "600", "610", "620", "630", "640", "650", "660", "670", "680", "690", "700", "710", "720", "730", "740", "750", "760", "770"}; 

public void actionPerformed(ActionEvent e){ 

    //access on the index not the object 
    int selectedIndex = combo.getSelectedIndex(); 

    //use the index to get values for size/code 
    String size = shoeSizes[selectedIndex]; 
    String code = shoeCodes [selectedIndex]; 

    //bring to front 
    display.append("\nYou selected "+size"+, code is " + code); 
} 

もしあなたの配列が同じ長さ(assert shoeSizes.length == shoeCodes.length;)を持っている場合にのみ機能します

+0

複数の配列を使用してデータを格納しないでください。データが関連している場合は、両方のプロパティを含むカスタムオブジェクトに格納する必要があります。次に、このオブジェクトをコンボボックスに追加します。 – camickr

+0

要点:あなたのアプリケーションによって異なります。他の状況でクラスが必要な場合は、あなたは正しいです。このコンテキストの中で必要なのであれば、クラスを作成するのはむしろコードを膨らませます(私のコードは3行と1行のコメント行を変更します - 初心者の方はもっときれいではありません) –

1

(サイズ、コード)、複数のプロパティを持つカスタムオブジェクトを作成します。このオブジェクトをコンボボックスに追加し、カスタムレンダラーを作成してsizeプロパティを表示します。

class FooRenderer extends BasicComboBoxRenderer 
{ 
    public Component getListCellRendererComponent(
     JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) 
    { 
     super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 

     if (value instanceof Foo) 
     { 
      Foo foo = (Foo)value; 
      setText(foo.getSize()); 
     } 

     return this; 
    } 
} 

チェックアウトの詳細情報や、より完全なソリューションのためのCombo Box With Custom Renderer

基本的なレンダラのためのコードは次のようになります。

関連する問題