-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShoppingList.java
More file actions
172 lines (142 loc) · 5.56 KB
/
ShoppingList.java
File metadata and controls
172 lines (142 loc) · 5.56 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package ShoppingList;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ShoppingList extends JFrame {
private DefaultListModel<Item> sListModel;
private JList<Item> sList;
private JTextField newItemNameField;
private JTextField newItemInfoField;
private JButton addButton;
private JButton removeButton;
private JButton increaseButton;
private JButton decreaseButton;
public ShoppingList() {
setTitle("Shopping List");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
sListModel = new DefaultListModel<>();
sList = new JList<>(sListModel);
JScrollPane scrollPane = new JScrollPane(sList);
add(scrollPane, BorderLayout.CENTER);
newItemNameField = new JTextField();
newItemInfoField = new JTextField();
addButton = new JButton("Add");
removeButton = new JButton("Remove");
increaseButton = new JButton("+");
decreaseButton = new JButton("-");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String itemName = newItemNameField.getText();
String itemInfo = newItemInfoField.getText();
if (!itemName.isEmpty()) {
Item newItem = new Item(itemName, itemInfo);
sListModel.addElement(newItem);
newItemNameField.setText("");
newItemInfoField.setText("");
}
}
});
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedIndex = sList.getSelectedIndex();
if (selectedIndex != -1) {
sListModel.remove(selectedIndex);
}
}
});
increaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedIndex = sList.getSelectedIndex();
if (selectedIndex != -1) {
Item selectedItem = sListModel.getElementAt(selectedIndex);
selectedItem.incrementQuantity();
sList.repaint();
}
}
});
decreaseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedIndex = sList.getSelectedIndex();
if (selectedIndex != -1) {
Item selectedItem = sListModel.getElementAt(selectedIndex);
selectedItem.decrementQuantity();
sList.repaint();
}
}
});
JPanel newItemPanel = new JPanel(new BorderLayout());
newItemPanel.add(new JLabel("Name:"), BorderLayout.WEST);
newItemPanel.add(newItemNameField, BorderLayout.CENTER);
newItemPanel.add(new JLabel("Info:"), BorderLayout.SOUTH);
newItemPanel.add(newItemInfoField, BorderLayout.SOUTH);
JPanel buttonPanel = new JPanel(new GridLayout(2, 2));
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
buttonPanel.add(increaseButton);
buttonPanel.add(decreaseButton);
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(newItemPanel, BorderLayout.CENTER);
inputPanel.add(buttonPanel, BorderLayout.SOUTH);
add(inputPanel, BorderLayout.SOUTH);
sList.setCellRenderer(new ItemRenderer());
sList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setVisible(true);
}
private class Item {
private String name;
private int quantity;
private String info;
public Item(String name, String info) {
this.name = name;
this.quantity = 1;
this.info = info;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public void incrementQuantity() {
quantity++;
}
public void decrementQuantity() {
if (quantity > 1) {
quantity--;
}
}
public String getInfo() {
return info;
}
@Override
public String toString() {
return name + " (" + quantity + ") " + info;
}
}
private class ItemRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
label.setText(((Item) value).toString());
label.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
return label;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ShoppingList();
}
});
}
}