質問するログイン新規登録

回答編集履歴

1

質問者様追加コメントへの補足とサンプル追加

2018/01/29 10:31

投稿

whistyun
whistyun

スコア149

answer CHANGED
@@ -142,4 +142,131 @@
142
142
  }
143
143
  ```
144
144
 
145
- ![イメージ説明](2285bd2135f0e153a2c8552fcabd6b6b.png)
145
+ ![イメージ説明](2285bd2135f0e153a2c8552fcabd6b6b.png)
146
+
147
+ ---
148
+
149
+ ## コメントへの追記 @ 1/29
150
+
151
+ 私が試した環境(JRE 1.8.0_151および、JDK 9.0.4)だけかもしれませんが、
152
+ 表のデータ数(行数)に合わせて、ボタンは自動で追加・削除されます。
153
+
154
+ 上記のサンプルでは、表のデータを4件(26行目で設定)としていますが、
155
+ 後から、データを追加したり、削除しても問題ありませんでした(全件入れ直しも同様)。
156
+ 表データを追加・削除するサンプルを記載しましたので、あわせてご確認ください。
157
+
158
+ ```java
159
+ import javafx.application.Application;
160
+ import javafx.beans.property.SimpleStringProperty;
161
+ import javafx.collections.FXCollections;
162
+ import javafx.collections.ObservableList;
163
+ import javafx.scene.Scene;
164
+ import javafx.scene.control.Button;
165
+ import javafx.scene.control.TableColumn;
166
+ import javafx.scene.control.TableView;
167
+ import javafx.scene.control.TextArea;
168
+ import javafx.scene.control.cell.PropertyValueFactory;
169
+ import javafx.scene.layout.BorderPane;
170
+ import javafx.scene.layout.HBox;
171
+ import javafx.stage.Stage;
172
+
173
+ public class ButtonTableAp2 extends Application {
174
+ private ObservableList<Entity> tableData;
175
+ private TextArea console;
176
+
177
+ public static void main(String[] args) {
178
+ launch(args);
179
+ }
180
+
181
+ @Override
182
+ public void start(Stage stage) {
183
+ stage.setWidth(400);
184
+ stage.setHeight(300);
185
+
186
+ // 表の作成
187
+ TableView<Entity> table = new TableView<>();
188
+ table.setItems(tableData=FXCollections.observableArrayList(
189
+ new Entity("名前1", "詳細1"),
190
+ new Entity("名前2", "詳細2"),
191
+ new Entity("名前3", "詳細3"),
192
+ new Entity("名前4", "詳細4")));
193
+
194
+ // 1列名: 名前
195
+ TableColumn firstNameCol = new TableColumn("名前");
196
+ firstNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
197
+ table.getColumns().addAll(firstNameCol);
198
+
199
+ // 2列名: ボタン
200
+ TableColumn actionCol = new TableColumn("詳細");
201
+ //actionCol.setCellValueFactory(new PropertyValueFactory<>("DUMMY")); 設定しなくても動く
202
+ actionCol.setCellFactory(new ButtonCellFactory<Entity>("ボタン", e -> onTableButtonClick(e)));
203
+ table.getColumns().addAll(actionCol);
204
+
205
+ // 表データを変更するためのボタン
206
+ Button addBtn =new Button("表データ追加");
207
+ Button removeBtn =new Button("表データ削除");
208
+ addBtn.setOnAction(event -> addToTable());
209
+ removeBtn.setOnAction(event -> removeFromTable());
210
+
211
+ HBox box=new HBox();
212
+ box.getChildren().addAll(addBtn, removeBtn);
213
+
214
+
215
+ // レイアウト
216
+ console = new TextArea();
217
+ BorderPane pane = new BorderPane();
218
+ pane.setTop(box);
219
+ pane.setLeft(table);
220
+ pane.setCenter(console);
221
+
222
+ Scene scene = new Scene(pane);
223
+ stage.setScene(scene);
224
+ stage.show();
225
+ }
226
+
227
+ private void addToTable(){
228
+ int index=tableData.size()+1;
229
+ tableData.add(new Entity("追加名:"+index, "ボタンで追加されたデータです@"+index));
230
+ }
231
+
232
+ private void removeFromTable(){
233
+ if(tableData.size()>0){
234
+ tableData.remove(tableData.size()-1);
235
+ }
236
+ }
237
+
238
+ /** 表のボタン押下時の処理 */
239
+ private void onTableButtonClick(Entity selected) {
240
+ console.setText("名前:\r\n\t" + selected.getName() + "\r\n" + "詳細:\r\n\t" + selected.getDetails());
241
+ }
242
+
243
+ /** 表の行のデータ */
244
+ public static class Entity {
245
+ private final SimpleStringProperty name;
246
+ private final SimpleStringProperty details;
247
+
248
+ private Entity(String name, String details) {
249
+ this.name = new SimpleStringProperty(name);
250
+ this.details = new SimpleStringProperty(details);
251
+ }
252
+
253
+ public String getName() {
254
+ return name.get();
255
+ }
256
+
257
+ public void setName(String nm) {
258
+ name.set(nm);
259
+ }
260
+
261
+ public String getDetails() {
262
+ return details.get();
263
+ }
264
+
265
+ public void setDetails(String dt) {
266
+ details.set(dt);
267
+ }
268
+ }
269
+ }
270
+ ```
271
+ ![追加前](3c416b2fe081cdadf47a7ec6537756fd.png)
272
+ ![追加後](3a1de8a1d3519dc0948987eb7897881f.png)