简体   繁体   English

如何在Java的JList中显示图标和字符串?

[英]How can I show an Icon and a String in a JList in Java?

I have a class Test whose field can be changed in the program when selected in a JList. 我有一个Test类,在JList中选择它的字段可以在程序中更改。 I've tried: 我试过了:

model = new DefaultListModel();
list = new JList(model);
model.addElement(new Test(1.0f));

but then I get Test@44322 in my JList. 但随后我的JList中得到Test @ 44322。 I want a black square and the String "Test" in my JList. 我想要一个黑色正方形和JList中的字符串“ Test”。 What's the best way to achieve that? 实现此目标的最佳方法是什么?

I think the approach is quite OK, next step is to tell the list how to render an object Test, ie 我认为该方法还可以,下一步是告诉列表如何渲染对象Test,即

list.setCellRenderer(...)

my 0.02€ 我的0.02€

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM