Repository navigation
Expand file tree
/
Copy pathJListTutorial.java
More file actions
82 lines (62 loc) · 2.08 KB
/
Copy pathJListTutorial.java
File metadata and controls
82 lines (62 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import javax.swing.*;
import java.math.BigDecimal;
public class JListTutorial {
JFrame frame = new JFrame("Storage");
JList<Product> list = new JList<>();
DefaultListModel<Product> model = new DefaultListModel<>();
JLabel label = new JLabel();
JPanel panel = new JPanel();
JSplitPane splitPane = new JSplitPane();
public JListTutorial() {
list.setModel(model);
model.addElement(new Product("Oranges", new BigDecimal("2.00"), "These are fresh oranges"));
model.addElement(new Product("Apples", new BigDecimal("1.50"), "These are fresh apples"));
list.getSelectionModel().addListSelectionListener(e -> {
Product p = list.getSelectedValue();
label.setText("Name: " + p.getName() + " ::: " + p.getPrice().toPlainString() + " ::: " + p.getDesc());
});
splitPane.setLeftComponent(new JScrollPane(list));
panel.add(label);
splitPane.setRightComponent(panel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(splitPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(JListTutorial::new);
}
private class Product {
String name;
BigDecimal price;
String desc;
public Product(String name, BigDecimal price, String desc) {
this.name = name;
this.price = price;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return name;
}
}
}