[英]How do I detect the selected object in a JComboBox?
I have made a JComboBox and populated it with the result of toString of an object type which I created myself. 我创建了一个JComboBox,并使用我自己创建的对象类型的toString结果填充它。
I want to be able to call upon the selected object from the JComboBox and then use a method from its class. 我希望能够从JComboBox调用所选对象,然后使用其类中的方法。 However, even though my JComboBox only contains one object type (The one I made), Java doesn't know this and therefore I can;t use something like...
但是,即使我的JComboBox只包含一个对象类型(我制作的那个),Java也不知道这个,因此我可以使用类似...
MyObject selectedObject = MyComboBox.getSelectedItem();
as Java cannot accept that only a "MyObject" will ever be selected. 因为Java不能接受只选择“MyObject”。
How am I supposed to get the user to select an object of my own? 我该如何让用户选择我自己的对象?
I think you need cast it, like 我觉得你需要施展它,就像
MyObject selectedObject = (MyObject) MyComboBox.getSelectedItem(); MyObject selectedObject =(MyObject)MyComboBox.getSelectedItem();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.