質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaFX

JavaFXとは、Java仮想マシン上で動作するリッチインターネットアプリケーション (RIA) のGUIライブラリです。Swingとは異なり、FXMLと呼ばれる XMLとCSSを併用してデザインを記述します。

Q&A

解決済

2回答

1374閲覧

JavaFXのTableColumnに中身を追加したい

ryu-bamboo

総合スコア9

JavaFX

JavaFXとは、Java仮想マシン上で動作するリッチインターネットアプリケーション (RIA) のGUIライブラリです。Swingとは異なり、FXMLと呼ばれる XMLとCSSを併用してデザインを記述します。

1グッド

0クリップ

投稿2021/04/03 13:45

編集2021/04/05 11:43

FXMLのtablecolumnにデータを追加したいと思い、調べて見たところ、

(columnのfx:id).setCellValueFactory(new PropertyValueFactory<(追加したいリストの型), String>(""));

 の形でcontrollerクラスと追加したいデータのクラスを関連付けることでデータを追加できると理解したのですが、実際に試したところ、引数の型が違うと警告が出てしまいました。
ほかに調べてみても、自分の力ではこれ以上の情報が出てこないので、力を貸していただきたいです。

controller

1/** 2 * Sample Skeleton for 'ScheduleIndividual.fxml' Controller Class 3 */ 4 5package application; 6 7import java.net.URL; 8import java.util.ResourceBundle; 9 10import javafx.fxml.FXML; 11import javafx.scene.control.TableColumn; 12import javafx.scene.control.TableView; 13import javafx.scene.control.cell.PropertyValueFactory; 14 15public class SceduleTableController { 16 17 @FXML // ResourceBundle that was given to the FXMLLoader 18 private ResourceBundle resources; 19 20 @FXML // URL location of the FXML file that was given to the FXMLLoader 21 private URL location; 22 23 @FXML // fx:id="column" 24 private TableColumn<?, ?> column; // Value injected by FXMLLoader 25 26 @FXML // fx:id="tableView1" 27 private TableView<?> tableView1; // Value injected by FXMLLoader 28 29 @FXML // This method is called by the FXMLLoader when initialization is complete 30 void initialize() { 31 32 column.setCellValueFactory(new PropertyValueFactory<ScheduleData, String>("tgtDate")); 33 assert column != null : "fx:id=\"column\" was not injected: check your FXML file 'ScheduleIndividual.fxml'."; 34 assert tableView1 != null : "fx:id=\"tableView1\" was not injected: check your FXML file 'ScheduleIndividual.fxml'."; 35 36 } 37 38 39}

data

1package application; 2 3import javafx.beans.property.SimpleStringProperty; 4import javafx.beans.property.StringProperty; 5 6public class ScheduleData { 7 private StringProperty data; 8 9 public StringProperty dataProperty() { 10 return data; 11 } 12 13 public ScheduleData(String data) { 14 this.data = new SimpleStringProperty(data); 15 } 16} 17

下のコードはコンソールです。

console

1javafx.fxml.LoadException: 2/C:/Users/user/workspace/SceduleTable/bin/application/ScheduleIndividual.fxml 3 4 at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625) 5 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595) 6 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466) 7 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237) 8 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194) 9 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163) 10 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136) 11 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113) 12 at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106) 13 at fxproject/application.SceduleMain.start(SceduleMain.java:14) 14 at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846) 15 at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455) 16 at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428) 17 at java.base/java.security.AccessController.doPrivileged(Native Method) 18 at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427) 19 at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) 20 at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 21 at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174) 22 at java.base/java.lang.Thread.run(Thread.java:834) 23Caused by: java.lang.reflect.InvocationTargetException 24 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 25 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 26 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 27 at java.base/java.lang.reflect.Method.invoke(Method.java:566) 28 at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76) 29 at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) 30 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 31 at java.base/java.lang.reflect.Method.invoke(Method.java:566) 32 at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273) 33 at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83) 34 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591) 35 ... 17 more 36Caused by: java.lang.Error: Unresolved compilation problem: 37 型 TableColumn<capture#1-of ?,capture#2-of ?> のメソッド setCellValueFactory(Callback<TableColumn.CellDataFeatures<capture#1-of ?,capture#2-of ?>,ObservableValue<capture#2-of ?>>) は引数 (PropertyValueFactory<ScheduleData,String>) に適用できません 38 39 at fxproject/application.SceduleTableController.initialize(SceduleTableController.java:32) 40 ... 28 more 41

バージョン
・jdk11
・e(fx)clipse 3.7.0

よろしくお願いします<m(__)m>

TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

わざわざ調べ、考えていただきありがとうございます!

今わかったのですが、コントローラーの、columnの宣言の部分でジェネリクスに型をいれてなかったのが原因みたいでした!

試行錯誤しながらやってたのでおかしなクラスになってたりしたのに色々教えていただいてありがとうございます!Oracleの記事もとても参考になりました!

投稿2021/04/06 13:43

ryu-bamboo

総合スコア9

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

ScheduleDataがちょっとおかしいです。

私もあまりわかっていませんが、↓を注意深く読んでやってみるとこんな感じでしょうか?
13 表ビュー(リリース8)

アイテムの追加・編集まで(編集はEnterで確定)

xml

1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.control.Button?> 4<?import javafx.scene.control.TableColumn?> 5<?import javafx.scene.control.TableView?> 6<?import javafx.scene.control.TextField?> 7<?import javafx.scene.layout.BorderPane?> 8<?import javafx.scene.layout.HBox?> 9 10<BorderPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" 11 fx:controller="sample.SceduleTableController"> 12 <center> 13 <TableView fx:id="tableView1" editable="true"> 14 <columns> 15 <TableColumn fx:id="column" prefWidth="200.0" text="data"/> 16 </columns> 17 </TableView> 18 </center> 19 <bottom> 20 <HBox> 21 <TextField fx:id="textField1"/> 22 <Button onAction="#onButton1Click" text="Add"/> 23 </HBox> 24 </bottom> 25</BorderPane>

Java

1package sample; 2 3import javafx.collections.FXCollections; 4import javafx.collections.ObservableList; 5import javafx.fxml.FXML; 6import javafx.scene.control.TableColumn; 7import javafx.scene.control.TableView; 8import javafx.scene.control.TextField; 9import javafx.scene.control.cell.PropertyValueFactory; 10import javafx.scene.control.cell.TextFieldTableCell; 11 12public class SceduleTableController { 13 @FXML 14 private TableView<ScheduleData> tableView1; 15 @FXML 16 private TableColumn<ScheduleData, String> column; 17 @FXML 18 private TextField textField1; 19 20 private ObservableList<ScheduleData> schedules = FXCollections.observableArrayList(); 21 22 @FXML 23 void initialize() { 24 tableView1.setItems(schedules); 25 26 column.setCellValueFactory(new PropertyValueFactory<>("data")); 27 column.setCellFactory(TextFieldTableCell.forTableColumn()); 28 column.setOnEditCommit((TableColumn.CellEditEvent<ScheduleData, String> t) -> { 29 ScheduleData data = t.getTableView().getItems().get(t.getTablePosition().getRow()); 30 data.setData(t.getNewValue()); 31 }); 32 33 schedules.add(new ScheduleData("aaaa")); 34 schedules.add(new ScheduleData("bbbb")); 35 } 36 37 @FXML 38 protected void onButton1Click() { 39 String data = textField1.getText(); 40 if (0 < data.length()) { 41 schedules.add(new ScheduleData(data)); 42 textField1.clear(); 43 } 44 } 45}

Java

1package sample; 2 3import javafx.beans.property.SimpleStringProperty; 4import javafx.beans.property.StringProperty; 5 6public class ScheduleData { 7 private StringProperty data; 8 9 public ScheduleData(String data) { 10 this.data = new SimpleStringProperty(data); 11 } 12 13 public String getData() { 14 return data.get(); 15 } 16 17 public void setData(String data) { 18 this.data.set(data); 19 } 20}

Java

1package sample; 2 3import javafx.application.Application; 4import javafx.fxml.FXMLLoader; 5import javafx.scene.Parent; 6import javafx.scene.Scene; 7import javafx.stage.Stage; 8 9public class Main extends Application { 10 public static void main(String[] args) { 11 launch(args); 12 } 13 14 @Override 15 public void start(Stage primaryStage) throws Exception { 16 Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 17 primaryStage.setScene(new Scene(root, 300, 275)); 18 primaryStage.show(); 19 } 20}

投稿2021/04/05 13:57

編集2023/07/26 15:47
TN8001

総合スコア9326

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問