将 jlist 选择转换为字符串
我试图弄清楚如何让程序根据选择的 jlist 中的项目创建文本字符串。起初我尝试过,
ListModel custTypetxt = custType.getModel();
System.out.println(custTypetxt);
但这只是给了我..
customerInfoUI$3@1820dda
I am trying to figure out how to get the program to create a text string based on which item in a jlist is selected. At first I tried
ListModel custTypetxt = custType.getModel();
System.out.println(custTypetxt);
but that just gave me..
customerInfoUI$3@1820dda
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要首先从列表中进行选择。致电
custType.getSelectedValue()
(或使用
getSelectedValues()
进行多项选择)。这将返回选定的对象。您可以以任何您想要的方式从对象中获取字符串(例如toString()
如果类已正确实现的话)。You need to get the selection from the list first. Call
custType.getSelectedValue()
(or
getSelectedValues()
for multiple selections). That will return the selected object. The you can get the string from the object any way you want (liketoString()
if it has been properly implemented by the class).看起来您正在获取正确的对象,因此您需要在 customerInfoUI 类中创建一个 toString() 方法。
然后您的代码将打印从 toString 方法返回的任何内容。 Object 类中 toString 的默认实现返回
@ hascode,这就是您在运行代码时看到的内容。It looks like you are getting the right object so you need to create a toString() method in the customerInfoUI class.
Then your code will print whatever you return from the toString method. The default implementation of toString in the Object class returns
<classname>
@ hascode which is what you are seeing when you run the code.