Java 将 getSelectedItem() 从 JComboBox 转换为 int 或任何其他东西

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6644945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:35:30  来源:igfitidea点击:

Converting the getSelectedItem() from JComboBox to int or any other thing

javaswingjcombobox

提问by aps

How to convert the getSelectedItem() from JComboBox to int or any other thing? Even converting to string isn't working. Eclipse says " Type mismatch: cannot convert from Object to String" or int or whatever. Any way to achieve this?

如何将 getSelectedItem() 从 JComboBox 转换为 int 或任何其他东西?即使转换为字符串也不起作用。Eclipse 说“类型不匹配:无法从对象转换为字符串”或 int 或其他任何内容。有什么方法可以实现这一目标吗?

回答by Marcelo

You can cast it to (String).

您可以将其投射到(String).

String value = (String) comboBox.getSelectedItem();

回答by Asaph

The answer really depends on what kind of items you placed into the JComboBoxto begin with. Whatever you put into it (eg. with addItem()or insertItemAt()) is what you can get out of it.

答案实际上取决于您JComboBox开始放入的项目类型。无论你投入什么(例如用addItem()insertItemAt()),你都能从中得到什么。

回答by Andrew Thompson

It works just fine here with objects.

它在这里与对象一起工作得很好。

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

class TestCombo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Integer[] numbers = {1,2,3};
                String[] names = {"Ben", "Jill", "Peter"};
                JComboBox numberCombo = new JComboBox(numbers);
                JComboBox nameCombo = new JComboBox(names);
                JPanel p = new JPanel(new GridLayout(0,1,3,3));
                p.add(numberCombo);
                p.add(nameCombo);

                JOptionPane.showMessageDialog(null, p);

                Integer chosenNumber = (Integer)numberCombo.getSelectedItem();
                System.out.println("Chosen Number: " + chosenNumber);
                String chosenName = (String)nameCombo.getSelectedItem();
                System.out.println("Chosen Name: " + chosenName);
            }
        });
    }
}

Typical output:

典型输出:

Chosen Number: 2
Chosen Name: Peter
Press any key to continue . . .

I agree strongly with the comment by LBFF. You need to go back to the basics.

我非常同意 LBFF 的评论。你需要回到基础。

回答by user3140870

//compiled in netbeans

import java.awt.GridLayout;<br>
import javax.swing.JComboBox;<br>
import javax.swing.JOptionPane;<br>
import javax.swing.JPanel;<br>
import javax.swing.SwingUtilities;<br>

class TestCombo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Integer[] numbers = {1,2,3};
                String[] names = {"Ben", "Jill", "Peter"};
                JComboBox numberCombo = new JComboBox(numbers);
                JComboBox nameCombo = new JComboBox(names);
                JPanel p = new JPanel(new GridLayout(0,1,3,3));
                p.add(numberCombo);
                p.add(nameCombo);

                JOptionPane.showMessageDialog(null, p);

                Integer chosenNumber = (Integer)numberCombo.getSelectedItem();
                System.out.println("Chosen Number: " + chosenNumber);
                String chosenName = (String)nameCombo.getSelectedItem();
                System.out.println("Chosen Name: " + chosenName);
            }
        });
    }
}

回答by Rok Strni?a

String value = comboBox.getSelectedItem(comboBox.getSelectedIndex());

No casts are required.

不需要演员表。