回答編集履歴
2
"machine" だらけな感じを簡略化
test
CHANGED
@@ -23,44 +23,44 @@
|
|
23
23
|
setSize(800, 600);
|
24
24
|
setLocationRelativeTo(null);
|
25
25
|
|
26
|
-
MachineModel m
|
26
|
+
MachineModel model = new MachineModel();
|
27
|
-
m
|
27
|
+
model.put(new Machine("機械1", 100, "ABC", 50)); //テストデータ1
|
28
|
-
m
|
28
|
+
model.put(new Machine("機械2", 200, "FRE", 10)); //テストデータ2
|
29
|
-
|
29
|
+
|
30
|
-
JTabbedPane tabbed
|
30
|
+
JTabbedPane tabbedPane = new JTabbedPane();
|
31
|
-
for(int i=0; i<m
|
31
|
+
for(int i=0; i<model.size(); i++) {
|
32
|
-
Machine machine = machineModel.getMachine(i);
|
33
|
-
MachinePanel panel = new MachinePanel(m
|
32
|
+
MachinePanel panel = new MachinePanel(model.get(i).name, model);
|
34
|
-
tabbed
|
33
|
+
tabbedPane.addTab(panel.name, panel);
|
35
|
-
}
|
34
|
+
}
|
36
|
-
tabbed
|
35
|
+
tabbedPane.addTab("情報", new InfoPanel(this, model));
|
37
|
-
|
36
|
+
|
38
|
-
getContentPane().add(tabbed
|
37
|
+
getContentPane().add(tabbedPane);
|
39
38
|
}
|
40
39
|
}
|
41
40
|
//"機械n"タブ
|
42
41
|
class MachinePanel extends JPanel {
|
43
42
|
final String name;
|
44
|
-
private MachineModel m
|
43
|
+
private MachineModel model;
|
45
44
|
private JLabel label;
|
46
45
|
|
47
|
-
MachinePanel(String name, MachineModel m
|
46
|
+
MachinePanel(String name, MachineModel model) {
|
48
47
|
super();
|
49
48
|
setPreferredSize(new Dimension(720, 240));
|
50
49
|
setBorder(new LineBorder(Color.RED, 2, true));
|
51
50
|
|
52
51
|
this.name = name;
|
53
|
-
this.m
|
52
|
+
this.model = model;
|
53
|
+
|
54
|
-
m
|
54
|
+
model.addChangeListener((e) -> setLabels());
|
55
55
|
|
56
56
|
label = new JLabel("sanpuru");
|
57
57
|
add(label);
|
58
58
|
|
59
|
-
s
|
59
|
+
setLabels();
|
60
|
-
}
|
60
|
+
}
|
61
|
-
|
61
|
+
|
62
|
-
private void s
|
62
|
+
private void setLabels() {
|
63
|
-
label.setText(""+m
|
63
|
+
label.setText(""+model.get(name));
|
64
64
|
}
|
65
65
|
}
|
66
66
|
//"情報"タブ
|
@@ -69,14 +69,14 @@
|
|
69
69
|
void rowClicked(int rowIndex);
|
70
70
|
}
|
71
71
|
|
72
|
-
public InfoPanel(Frame frame, MachineModel m
|
72
|
+
public InfoPanel(Frame frame, MachineModel model){
|
73
73
|
super();
|
74
74
|
|
75
|
-
|
75
|
+
InfoTableModel tableModel = new InfoTableModel(model);
|
76
76
|
JComponent tableComponent = createTablePanel(tableModel, (rowIndex) -> {
|
77
77
|
//行クリック
|
78
|
-
Machine machine = tableModel.get
|
78
|
+
Machine machine = tableModel.get(rowIndex);
|
79
|
-
MachineDialog dialog = new MachineDialog(frame, machine, (m)->m
|
79
|
+
MachineDialog dialog = new MachineDialog(frame, machine, (m)->model.put(m));
|
80
80
|
dialog.setLocationRelativeTo(null);
|
81
81
|
dialog.setVisible(true);
|
82
82
|
});
|
@@ -85,7 +85,7 @@
|
|
85
85
|
add(createSouthPanel(), BorderLayout.SOUTH);
|
86
86
|
}
|
87
87
|
|
88
|
-
private JComponent createTablePanel(
|
88
|
+
private JComponent createTablePanel(InfoTableModel tableModel, RowClickListener listener) {
|
89
89
|
JTable table = new JTable(tableModel);
|
90
90
|
table.setRowSelectionAllowed(true);
|
91
91
|
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
@@ -151,16 +151,16 @@
|
|
151
151
|
return machineList.size();
|
152
152
|
}
|
153
153
|
|
154
|
-
Machine get
|
154
|
+
Machine get(int index) {
|
155
155
|
return machineList.get(index);
|
156
156
|
}
|
157
157
|
|
158
|
-
Machine get
|
158
|
+
Machine get(String name) {
|
159
159
|
for(Machine m : machineList) if(m.name.equals(name)) return m;
|
160
160
|
return null;
|
161
161
|
}
|
162
162
|
|
163
|
-
public void put
|
163
|
+
public void put(Machine machine) {
|
164
164
|
for(int i=0; i<machineList.size(); i++) {
|
165
165
|
if(machineList.get(i).name.equals(machine.name)) {
|
166
166
|
machineList.set(i, machine);
|
@@ -188,22 +188,18 @@
|
|
188
188
|
}
|
189
189
|
}
|
190
190
|
//機械情報テーブルモデル
|
191
|
-
class
|
191
|
+
class InfoTableModel extends AbstractTableModel {
|
192
192
|
private static final String[] COLUMN_NAMES = {"", "ワット数", "油の種類", "油残量"};
|
193
193
|
|
194
|
-
private MachineModel m
|
194
|
+
private MachineModel model;
|
195
|
-
|
195
|
+
|
196
|
-
|
196
|
+
InfoTableModel(MachineModel model) {
|
197
|
-
this.m
|
197
|
+
this.model = model;
|
198
|
-
m
|
198
|
+
model.addChangeListener((e) -> fireTableDataChanged());
|
199
|
-
}
|
199
|
+
}
|
200
|
-
|
201
|
-
|
200
|
+
|
202
|
-
return machineModel.size();
|
203
|
-
}
|
204
|
-
|
205
|
-
Machine get
|
201
|
+
Machine get(int index) {
|
206
|
-
return m
|
202
|
+
return model.get(index);
|
207
203
|
}
|
208
204
|
|
209
205
|
@Override
|
@@ -219,7 +215,7 @@
|
|
219
215
|
|
220
216
|
@Override
|
221
217
|
public int getRowCount() {
|
222
|
-
return size();
|
218
|
+
return model.size();
|
223
219
|
}
|
224
220
|
|
225
221
|
@Override
|
@@ -229,7 +225,7 @@
|
|
229
225
|
|
230
226
|
@Override
|
231
227
|
public Object getValueAt(int rowIndex, int columnIndex) {
|
232
|
-
Machine machine = get
|
228
|
+
Machine machine = model.get(rowIndex);
|
233
229
|
switch(columnIndex) {
|
234
230
|
case 0: return machine.name;
|
235
231
|
case 1: return machine.output;
|
@@ -242,10 +238,10 @@
|
|
242
238
|
//機械情報入力ダイアログ
|
243
239
|
class MachineDialog extends JDialog {
|
244
240
|
interface MachineManager {
|
245
|
-
void put
|
241
|
+
void put(Machine machine);
|
246
|
-
}
|
242
|
+
}
|
247
|
-
|
243
|
+
|
248
|
-
MachineDialog(Frame frame, Machine machine, MachineManager ma
|
244
|
+
MachineDialog(Frame frame, Machine machine, MachineManager manager) {
|
249
245
|
super(frame, "詳細入力", true);
|
250
246
|
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
251
247
|
setSize(300, 200);
|
@@ -264,7 +260,7 @@
|
|
264
260
|
int output = Integer.parseInt(outputText.getText());
|
265
261
|
String oilType = oilTypeText.getText();
|
266
262
|
int oilAmount = Integer.parseInt(oilAmountText.getText());
|
267
|
-
ma
|
263
|
+
manager.put(new Machine(machine.name, output, oilType, oilAmount));
|
268
264
|
|
269
265
|
dispose();
|
270
266
|
} catch(NumberFormatException e) {
|
1
TN8001 さん回答のコメントから仕様推測変更
test
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
import java.awt.*;
|
7
7
|
import java.awt.event.*;
|
8
|
-
import java.util.
|
8
|
+
import java.util.*;
|
9
9
|
import java.util.List;
|
10
10
|
|
11
11
|
import javax.swing.*;
|
@@ -23,20 +23,44 @@
|
|
23
23
|
setSize(800, 600);
|
24
24
|
setLocationRelativeTo(null);
|
25
25
|
|
26
|
+
MachineModel machineModel = new MachineModel();
|
27
|
+
machineModel.putMachine(new Machine("機械1", 100, "ABC", 50)); //テストデータ1
|
28
|
+
machineModel.putMachine(new Machine("機械2", 200, "FRE", 10)); //テストデータ2
|
29
|
+
|
26
30
|
JTabbedPane tabbedpane = new JTabbedPane();
|
31
|
+
for(int i=0; i<machineModel.size(); i++) {
|
32
|
+
Machine machine = machineModel.getMachine(i);
|
33
|
+
MachinePanel panel = new MachinePanel(machine.name, machineModel);
|
27
|
-
tabbedpane.addTab(
|
34
|
+
tabbedpane.addTab(panel.name, panel);
|
35
|
+
}
|
28
|
-
tabbedpane.addTab("情報", new InfoPanel(this));
|
36
|
+
tabbedpane.addTab("情報", new InfoPanel(this, machineModel));
|
29
37
|
|
30
38
|
getContentPane().add(tabbedpane);
|
31
39
|
}
|
32
40
|
}
|
33
|
-
//"機械
|
41
|
+
//"機械n"タブ
|
34
|
-
class Machine
|
42
|
+
class MachinePanel extends JPanel {
|
43
|
+
final String name;
|
44
|
+
private MachineModel machineModel;
|
35
|
-
|
45
|
+
private JLabel label;
|
46
|
+
|
47
|
+
MachinePanel(String name, MachineModel machineModel) {
|
48
|
+
super();
|
36
49
|
setPreferredSize(new Dimension(720, 240));
|
37
50
|
setBorder(new LineBorder(Color.RED, 2, true));
|
38
51
|
|
52
|
+
this.name = name;
|
53
|
+
this.machineModel = machineModel;
|
54
|
+
machineModel.addChangeListener((e) -> showMachine());
|
55
|
+
|
39
|
-
a
|
56
|
+
label = new JLabel("sanpuru");
|
57
|
+
add(label);
|
58
|
+
|
59
|
+
showMachine();
|
60
|
+
}
|
61
|
+
|
62
|
+
private void showMachine() {
|
63
|
+
label.setText(""+machineModel.getMachine(name));
|
40
64
|
}
|
41
65
|
}
|
42
66
|
//"情報"タブ
|
@@ -45,17 +69,14 @@
|
|
45
69
|
void rowClicked(int rowIndex);
|
46
70
|
}
|
47
71
|
|
48
|
-
public InfoPanel(Frame frame){
|
72
|
+
public InfoPanel(Frame frame, MachineModel machineModel){
|
49
73
|
super();
|
50
74
|
|
51
|
-
MachineInfoTableModel tableModel = new MachineInfoTableModel();
|
75
|
+
MachineInfoTableModel tableModel = new MachineInfoTableModel(machineModel);
|
52
|
-
tableModel.putMachine(new Machine("機械1", 100, "ABC", 50)); //テストデータ1
|
53
|
-
tableModel.putMachine(new Machine("機械2", 200, "FRE", 10)); //テストデータ2
|
54
|
-
|
55
76
|
JComponent tableComponent = createTablePanel(tableModel, (rowIndex) -> {
|
56
77
|
//行クリック
|
57
78
|
Machine machine = tableModel.getMachine(rowIndex);
|
58
|
-
MachineDialog dialog = new MachineDialog(frame, machine,
|
79
|
+
MachineDialog dialog = new MachineDialog(frame, machine, (m)->machineModel.putMachine(m));
|
59
80
|
dialog.setLocationRelativeTo(null);
|
60
81
|
dialog.setVisible(true);
|
61
82
|
});
|
@@ -92,7 +113,7 @@
|
|
92
113
|
return panel;
|
93
114
|
}
|
94
115
|
}
|
95
|
-
//機械
|
116
|
+
//機械
|
96
117
|
class Machine {
|
97
118
|
final String name;
|
98
119
|
final int output; //ワット
|
@@ -105,12 +126,26 @@
|
|
105
126
|
this.oilType = oilType;
|
106
127
|
this.oilAmount = oilAmount;
|
107
128
|
}
|
129
|
+
|
130
|
+
@Override
|
131
|
+
public String toString() {
|
132
|
+
return "name="+name+", output="+output+", oilType="+oilType+", oilAmount="+oilAmount;
|
108
|
-
}
|
133
|
+
}
|
134
|
+
}
|
109
|
-
//機械
|
135
|
+
//機械モデル
|
136
|
+
class MachineModel {
|
110
|
-
|
137
|
+
interface DataChangeListener extends EventListener {
|
138
|
+
public void dataChanged(DataChangeEvent e);
|
139
|
+
}
|
140
|
+
|
111
|
-
|
141
|
+
static class DataChangeEvent extends EventObject {
|
142
|
+
public DataChangeEvent(Object source) {
|
143
|
+
super(source);
|
144
|
+
}
|
145
|
+
}
|
112
146
|
|
113
147
|
private List<Machine> machineList = new ArrayList<>();
|
148
|
+
private final Set<DataChangeListener> listenerList = new HashSet<>();
|
114
149
|
|
115
150
|
int size() {
|
116
151
|
return machineList.size();
|
@@ -120,17 +155,55 @@
|
|
120
155
|
return machineList.get(index);
|
121
156
|
}
|
122
157
|
|
158
|
+
Machine getMachine(String name) {
|
159
|
+
for(Machine m : machineList) if(m.name.equals(name)) return m;
|
123
|
-
|
160
|
+
return null;
|
161
|
+
}
|
162
|
+
|
124
163
|
public void putMachine(Machine machine) {
|
125
164
|
for(int i=0; i<machineList.size(); i++) {
|
126
165
|
if(machineList.get(i).name.equals(machine.name)) {
|
127
166
|
machineList.set(i, machine);
|
128
|
-
fire
|
167
|
+
fireDataChangeEvent();
|
129
168
|
return;
|
130
169
|
}
|
131
170
|
}
|
132
171
|
machineList.add(machine);
|
172
|
+
fireDataChangeEvent();
|
173
|
+
}
|
174
|
+
|
175
|
+
synchronized void addChangeListener(DataChangeListener l) {
|
176
|
+
if(!listenerList.contains(l)) {
|
177
|
+
listenerList.add(l);
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
133
|
-
|
181
|
+
synchronized void removeChangeListener(DataChangeListener l) {
|
182
|
+
listenerList.remove(l);
|
183
|
+
}
|
184
|
+
|
185
|
+
synchronized protected void fireDataChangeEvent() {
|
186
|
+
DataChangeEvent evt = new DataChangeEvent(this);
|
187
|
+
for(DataChangeListener listener : listenerList) listener.dataChanged(evt);
|
188
|
+
}
|
189
|
+
}
|
190
|
+
//機械情報テーブルモデル
|
191
|
+
class MachineInfoTableModel extends AbstractTableModel {
|
192
|
+
private static final String[] COLUMN_NAMES = {"", "ワット数", "油の種類", "油残量"};
|
193
|
+
|
194
|
+
private MachineModel machineModel;
|
195
|
+
|
196
|
+
MachineInfoTableModel(MachineModel machineModel) {
|
197
|
+
this.machineModel = machineModel;
|
198
|
+
machineModel.addChangeListener((e) -> fireTableDataChanged());
|
199
|
+
}
|
200
|
+
|
201
|
+
int size() {
|
202
|
+
return machineModel.size();
|
203
|
+
}
|
204
|
+
|
205
|
+
Machine getMachine(int index) {
|
206
|
+
return machineModel.getMachine(index);
|
134
207
|
}
|
135
208
|
|
136
209
|
@Override
|
@@ -156,7 +229,7 @@
|
|
156
229
|
|
157
230
|
@Override
|
158
231
|
public Object getValueAt(int rowIndex, int columnIndex) {
|
159
|
-
Machine machine =
|
232
|
+
Machine machine = getMachine(rowIndex);
|
160
233
|
switch(columnIndex) {
|
161
234
|
case 0: return machine.name;
|
162
235
|
case 1: return machine.output;
|