如何将ComboBox的选定项设置为另一个值?

时间:2016-06-23 10:27:36

标签: java swing

我目前正在尝试将我在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);
  }
}

3 个答案:

答案 0 :(得分: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);
        }

    }
}

答案 1 :(得分: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;

时才有效

答案 2 :(得分: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以获取更多信息和更完整的解决方案。