[英]Is there a list gui component in Java 8 Swing?
I would like each element of the list to have multiple components.我希望列表的每个元素都有多个组件。 Maybe a title, some text below it or even a button?
也许是一个标题,它下面的一些文字,甚至是一个按钮? Is that possible?
那可能吗? I'm hoping to find something similar to SwiftUI's list view.
我希望找到类似于 SwiftUI 的列表视图的东西。
Something like this: https://www.appcoda.com/wp-content/uploads/2019/06/image-8-1024x607.png (not enough reputation to add images)像这样: https : //www.appcoda.com/wp-content/uploads/2019/06/image-8-1024x607.png (没有足够的声誉来添加图像)
It is not very detailed, but the Java tutorial mentions that you can use a custom ListCellRenderer .它不是很详细,但Java 教程提到您可以使用自定义ListCellRenderer 。 That should do the job!
那应该做的工作!
Maybe you want to try NetBeans
and it's GUI builder?也许您想尝试使用
NetBeans
和它的 GUI 构建器?
On the right panel you have a Palette
that gives you pretty good overview of components available in Swing
.在右侧面板上,您有一个
Palette
,它可以让您很好地概览Swing
可用的组件。
As for the List
itself, you are probably looking at something like this: List至于
List
本身,您可能正在查看如下内容: List
@ewramner's answer is correct. @ewramner 的回答是正确的。 A custom
ListCellRenderer
will get it done.自定义
ListCellRenderer
将完成它。 Here is an example using a JPanel
as the renderering component which includes a nested JPanel
(named rightPanel
).这是一个使用
JPanel
作为渲染组件的示例,其中包括一个嵌套的JPanel
(名为rightPanel
)。
(Some comments inside code) (代码中的一些注释)
public class MultiComponentListCellRenderer {
public static void main(String[] args) {
Person mike = new Person("Mike", 25);
Person alice = new Person("Alice", 30);
DefaultListModel<Person> model = new DefaultListModel<>();
model.addElement(mike);
model.addElement(alice);
JList<Person> personJList = new JList<>(model);
personJList.setCellRenderer(new PersonListCellRenderer());
JOptionPane.showMessageDialog(null, personJList);
}
public static class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public static class PersonListCellRenderer extends JPanel implements ListCellRenderer<Person> {
private JLabel nameLabel;
private JLabel ageLabel;
private JLabel iconLabel;
private JPanel rightPanel;
public PersonListCellRenderer() {
super(new BorderLayout());
nameLabel = new JLabel();
ageLabel = new JLabel();
iconLabel = new JLabel();
try {
Image personImg = ImageIO.read(new URL("https://cdn2.iconfinder.com/data/icons/people-80/96/Picture1-512.png"));
ImageIcon personIcon = new ImageIcon(personImg.getScaledInstance(20, 20, Image.SCALE_SMOOTH));
iconLabel.setIcon(personIcon);
} catch (IOException e) {
e.printStackTrace();
}
add(iconLabel, BorderLayout.LINE_START);
//Right panel will contain name and age, top and bottom respectively
rightPanel = new JPanel(new BorderLayout());
rightPanel.add(nameLabel, BorderLayout.PAGE_START);
rightPanel.add(ageLabel, BorderLayout.PAGE_END);
add(rightPanel, BorderLayout.CENTER);
setOpaque(true); //for visible backgrounds
rightPanel.setOpaque(true);//for visible backgrounds
}
@Override
public Component getListCellRendererComponent(JList<? extends Person> list, Person value, int index, boolean isSelected,
boolean cellHasFocus) {
nameLabel.setText(value.name);
ageLabel.setText(String.valueOf(value.age));
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
rightPanel.setBackground(list.getSelectionBackground());
rightPanel.setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
rightPanel.setBackground(list.getBackground());
rightPanel.setForeground(list.getForeground());
}
return this;
}
}
}
The result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.