将 jlist 选择转换为字符串

发布于 2024-08-08 08:38:47 字数 222 浏览 2 评论 0原文

我试图弄清楚如何让程序根据选择的 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 技术交流群。

扫码二维码加入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 (like toString() if it has been properly implemented by the class).

仙气飘飘 2024-08-15 08:38:48

看起来您正在获取正确的对象,因此您需要在 customerInfoUI 类中创建一个 toString() 方法。

 public String toString(){
    return "String that describes my object";
 }

然后您的代码将打印从 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.

 public String toString(){
    return "String that describes my object";
 }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文