回答編集履歴

1

見直しキャンペーン中

2023/07/26 15:47

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,253 +1,128 @@
1
1
  `ScheduleData`がちょっとおかしいです。
2
2
 
3
-
4
-
5
3
  私もあまりわかっていませんが、↓を注意深く読んでやってみるとこんな感じでしょうか?
6
-
7
4
  [13 表ビュー(リリース8)](https://docs.oracle.com/javase/jp/8/javafx/user-interface-tutorial/table-view.htm)
8
-
9
-
10
5
 
11
6
  アイテムの追加・編集まで(編集はEnterで確定)
12
7
 
13
-
14
-
15
- ```fxml
8
+ ```xml
16
-
17
9
  <?xml version="1.0" encoding="UTF-8"?>
18
10
 
19
-
20
-
21
11
  <?import javafx.scene.control.Button?>
22
-
23
12
  <?import javafx.scene.control.TableColumn?>
24
-
25
13
  <?import javafx.scene.control.TableView?>
26
-
27
14
  <?import javafx.scene.control.TextField?>
28
-
29
15
  <?import javafx.scene.layout.BorderPane?>
30
-
31
16
  <?import javafx.scene.layout.HBox?>
32
17
 
33
-
34
-
35
18
  <BorderPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
36
-
37
19
  fx:controller="sample.SceduleTableController">
38
-
39
20
  <center>
40
-
41
21
  <TableView fx:id="tableView1" editable="true">
42
-
43
22
  <columns>
44
-
45
23
  <TableColumn fx:id="column" prefWidth="200.0" text="data"/>
46
-
47
24
  </columns>
48
-
49
25
  </TableView>
50
-
51
26
  </center>
52
-
53
27
  <bottom>
54
-
55
28
  <HBox>
56
-
57
29
  <TextField fx:id="textField1"/>
58
-
59
30
  <Button onAction="#onButton1Click" text="Add"/>
60
-
61
31
  </HBox>
62
-
63
32
  </bottom>
64
-
65
33
  </BorderPane>
66
-
67
34
  ```
68
35
 
69
-
70
-
71
36
  ```Java
72
-
73
37
  package sample;
74
38
 
75
-
76
-
77
39
  import javafx.collections.FXCollections;
78
-
79
40
  import javafx.collections.ObservableList;
80
-
81
41
  import javafx.fxml.FXML;
82
-
83
42
  import javafx.scene.control.TableColumn;
84
-
85
43
  import javafx.scene.control.TableView;
86
-
87
44
  import javafx.scene.control.TextField;
88
-
89
45
  import javafx.scene.control.cell.PropertyValueFactory;
90
-
91
46
  import javafx.scene.control.cell.TextFieldTableCell;
92
47
 
93
-
94
-
95
48
  public class SceduleTableController {
96
-
97
49
  @FXML
98
-
99
50
  private TableView<ScheduleData> tableView1;
100
-
101
51
  @FXML
102
-
103
52
  private TableColumn<ScheduleData, String> column;
104
-
105
53
  @FXML
106
-
107
54
  private TextField textField1;
108
-
109
-
110
55
 
111
56
  private ObservableList<ScheduleData> schedules = FXCollections.observableArrayList();
112
57
 
58
+ @FXML
59
+ void initialize() {
60
+ tableView1.setItems(schedules);
113
61
 
62
+ column.setCellValueFactory(new PropertyValueFactory<>("data"));
63
+ column.setCellFactory(TextFieldTableCell.forTableColumn());
64
+ column.setOnEditCommit((TableColumn.CellEditEvent<ScheduleData, String> t) -> {
65
+ ScheduleData data = t.getTableView().getItems().get(t.getTablePosition().getRow());
66
+ data.setData(t.getNewValue());
67
+ });
68
+
69
+ schedules.add(new ScheduleData("aaaa"));
70
+ schedules.add(new ScheduleData("bbbb"));
71
+ }
114
72
 
115
73
  @FXML
74
+ protected void onButton1Click() {
75
+ String data = textField1.getText();
76
+ if (0 < data.length()) {
77
+ schedules.add(new ScheduleData(data));
78
+ textField1.clear();
79
+ }
80
+ }
81
+ }
82
+ ```
116
83
 
84
+ ```Java
117
- void initialize() {
85
+ package sample;
118
86
 
87
+ import javafx.beans.property.SimpleStringProperty;
119
- tableView1.setItems(schedules);
88
+ import javafx.beans.property.StringProperty;
120
89
 
90
+ public class ScheduleData {
91
+ private StringProperty data;
121
92
 
122
-
123
- column.setCellValueFactory(new PropertyValueFactory<>("data"));
124
-
125
- column.setCellFactory(TextFieldTableCell.forTableColumn());
126
-
127
- column.setOnEditCommit((TableColumn.CellEditEvent<ScheduleData, String> t) -> {
128
-
129
- ScheduleData data = t.getTableView().getItems().get(t.getTablePosition().getRow());
130
-
131
- data.setData(t.getNewValue());
132
-
133
- });
134
-
135
-
136
-
137
- schedules.add(new ScheduleData("aaaa"));
93
+ public ScheduleData(String data) {
138
-
139
- schedules.add(new ScheduleData("bbbb"));
94
+ this.data = new SimpleStringProperty(data);
140
-
141
95
  }
142
96
 
143
-
144
-
145
- @FXML
146
-
147
- protected void onButton1Click() {
148
-
149
- String data = textField1.getText();
150
-
151
- if (0 < data.length()) {
97
+ public String getData() {
152
-
153
- schedules.add(new ScheduleData(data));
154
-
155
- textField1.clear();
98
+ return data.get();
156
-
157
- }
158
-
159
99
  }
160
100
 
101
+ public void setData(String data) {
102
+ this.data.set(data);
103
+ }
161
104
  }
162
-
163
105
  ```
164
106
 
107
+ ```Java
108
+ package sample;
165
109
 
110
+ import javafx.application.Application;
111
+ import javafx.fxml.FXMLLoader;
112
+ import javafx.scene.Parent;
113
+ import javafx.scene.Scene;
114
+ import javafx.stage.Stage;
166
115
 
167
- ```Javapackage sample;
168
-
169
-
170
-
171
- import javafx.beans.property.SimpleStringProperty;
172
-
173
- import javafx.beans.property.StringProperty;
174
-
175
-
176
-
177
- public class ScheduleData {
116
+ public class Main extends Application {
178
-
179
- private StringProperty data;
180
-
181
-
182
-
183
- public ScheduleData(String data) {
117
+ public static void main(String[] args) {
184
-
185
- this.data = new SimpleStringProperty(data);
118
+ launch(args);
186
-
187
119
  }
188
120
 
189
-
121
+ @Override
190
-
191
- public String getData() {
122
+ public void start(Stage primaryStage) throws Exception {
192
-
123
+ Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
124
+ primaryStage.setScene(new Scene(root, 300, 275));
193
- return data.get();
125
+ primaryStage.show();
194
-
195
126
  }
196
-
197
-
198
-
199
- public void setData(String data) {
200
-
201
- this.data.set(data);
202
-
203
- }
204
-
205
127
  }
206
-
207
128
  ```
208
-
209
-
210
-
211
- ```Java
212
-
213
- package sample;
214
-
215
-
216
-
217
- import javafx.application.Application;
218
-
219
- import javafx.fxml.FXMLLoader;
220
-
221
- import javafx.scene.Parent;
222
-
223
- import javafx.scene.Scene;
224
-
225
- import javafx.stage.Stage;
226
-
227
-
228
-
229
- public class Main extends Application {
230
-
231
- public static void main(String[] args) {
232
-
233
- launch(args);
234
-
235
- }
236
-
237
-
238
-
239
- @Override
240
-
241
- public void start(Stage primaryStage) throws Exception {
242
-
243
- Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
244
-
245
- primaryStage.setScene(new Scene(root, 300, 275));
246
-
247
- primaryStage.show();
248
-
249
- }
250
-
251
- }
252
-
253
- ```