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

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

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

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

Q&A

解決済

1回答

553閲覧

JavaFXでのTableView作成

junkjunk

総合スコア26

JavaFX

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

0グッド

0クリップ

投稿2019/06/26 01:54

SceneBuilderを使用してTableViewを用いたプログラムを作成したいと思い、こちらのサイトを参考に
TableViewTableViewを表示させたいのですがうまく動きません。
リンク内容

eclipse上ではコントローラークラスでTableColumnのパラメータ化が必要、という警告が表示されますが
ここは直接のエラー原因ではないと思うんですがどうなんでしょうか?

コードとエラー内容を載せさせて頂きますのでわかる方いらっしゃったらご教示お願い致します。
メモ書きが多いのでみにくかったらすみません。

mainクラス

JavaFX

1package application; 2 3import javafx.application.Application; 4import javafx.scene.Scene; 5import javafx.scene.control.Button; 6import javafx.scene.control.Label; 7import javafx.scene.control.TextField; 8import javafx.scene.layout.BorderPane; 9import javafx.scene.layout.Pane; 10import javafx.stage.Stage; 11import javafx.fxml.FXMLLoader; 12 13public class Main extends Application { 14 Stage stage; 15 16 @Override 17 public void start(Stage primaryStage) throws Exception { 18 stage = primaryStage; 19 primaryStage.setTitle("100Reading"); 20 Pane myPane_top = (Pane) 21 FXMLLoader.load(getClass().getResource("Book.fxml")); 22 Scene myScene = new Scene(myPane_top); 23 primaryStage.setScene(myScene); 24 primaryStage.show(); 25 26 } 27 28 public static void main(String[] args) { 29 launch(args); 30 } 31}

Controllerクラス

JavaFX

1package application; 2 3import java.net.URL; 4import java.util.ResourceBundle; 5import javafx.event.ActionEvent; 6import javafx.fxml.FXML; 7import javafx.fxml.Initializable; 8import javafx.scene.control.TableColumn; 9import javafx.scene.control.TableView; 10import javafx.scene.control.cell.PropertyValueFactory;//追加 11 12 13 14/** 15 * 16 * @author Toru Takahashi 17 */ 18public class BookTableViewController implements Initializable { 19 20 //Javaコード側で操作したいUIコントロールのインスタンス 21 @FXML 22 private TableView<Book> table; 23 @FXML 24 private TableColumn idColumn; 25 @FXML 26 private TableColumn nameColumn; 27 @FXML 28 private TableColumn birthYearColumn; 29 30 31 //イベントが発生したときに呼び出してもらうメソッド 32 @FXML 33 private void handleButtonAction(ActionEvent event) { 34 System.out.println("You clicked me!"); 35 } 36 37 //初期化コードを定義するメソッド 38 @Override 39 public void initialize(URL url, ResourceBundle rb) { 40 41 //public PropertyValueFactory(String property) 42 43 //指定されたプロパティ名を使用して、指定されたTableView行アイテムから再帰的に値を抽出するためにデフォルトのPropertyValueFactoryを作成し。 44 //パラメータ:property - 指定されたオブジェクトの対応する値を再帰的に抽出する試行に使用するプロパティの名前。 45 idColumn.setCellValueFactory(new PropertyValueFactory<Book, Integer>("id")); 46 nameColumn.setCellValueFactory(new PropertyValueFactory<Book, String>("name")); 47 birthYearColumn.setCellValueFactory(new PropertyValueFactory<Book, Integer>("birthYear")); 48 49 // サンプルデータを1行追加 50 table.getItems().add(new Book(101, "Lucius Junius Brutus", -550)); 51 } 52}

データクラス

JavaFX

1//aテーブルに表示する3つの属性を定義するクラス 2 3//JavaBeans:銀行、情報を一時的に預かってくれる 4//a・JavaBeansにデータを格納する時は口座を開設する(newする) 5//a・データの出し入れはgetさんとsetさん経由で行う(getter/setter) 6//a・JavaBeansは1件しか保持することができないので、データを複数件格納したい時は名前を変えて複数口座開設する。 7 8//aJavaFXのBeansは、「プロパティ」というクラスを使って値の更新を知るようになっている。 9//aプロパティクラスは、整数、文字列とデータ種類ごとにクラスが用意されている。 10//a整数値であるIDの場合、フィールドに保持するプロパティはIntegerProperty型(抽象クラス)とし、 11//a実装型はここではSimpleIntegerProperty型を使っています 12//aこのフィールドのgetterメソッドは命名規約が定まっており、プロパティ名+Property です。 13 14package application; 15 16import javafx.beans.property.IntegerProperty; 17import javafx.beans.property.SimpleIntegerProperty; 18import javafx.beans.property.SimpleStringProperty; 19import javafx.beans.property.StringProperty; 20 21public class Book { 22 //aラッパークラス:基本データ型の値をラップ(=包み込む)してオブジェクトとして利用できるようにするクラス 23 //Integerがラッパークラス 24 private IntegerProperty id; 25 private StringProperty name; 26 private IntegerProperty birthYear; 27 28 public Book(int anId, String aName, int aBirthYear) { 29 id = new SimpleIntegerProperty(anId); 30 name = new SimpleStringProperty(aName); 31 birthYear = new SimpleIntegerProperty(aBirthYear); 32 } 33 34 //Beansのgetterクラス? 35 public IntegerProperty idProperty() { 36 return id; 37 } 38 39 public StringProperty nameProperty() { 40 return name; 41 } 42 43 public IntegerProperty birthYearProperty() { 44 return birthYear; 45 } 46 47}

FXML

JavaFX

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.layout.AnchorPane?> 7 8<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="BookTableViewController"> 9 <children> 10 <TableView fx:id="table" layoutX="34.0" layoutY="33.0" prefHeight="308.0" prefWidth="521.0"> 11 <columns> 12 <TableColumn fx:id="idColumn" prefWidth="156.0" text="ID" /> 13 <TableColumn fx:id="nameColumn" prefWidth="167.0" text="氏名" /> 14 <TableColumn fx:id="birthYearColumn" prefWidth="197.0" text="誕生年" /> 15 </columns> 16 </TableView> 17 <Button layoutX="503.0" layoutY="354.0" mnemonicParsing="false" text="Click Me!" /> 18 </children> 19</AnchorPane>

エラーコード

JavaFX

1Exception in Application start method 2java.lang.reflect.InvocationTargetException 3 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 4 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 5 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 6 at java.lang.reflect.Method.invoke(Unknown Source) 7 at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 8 at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 9 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 10 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 11 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 12 at java.lang.reflect.Method.invoke(Unknown Source) 13 at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 14Caused by: java.lang.RuntimeException: Exception in Application start method 15 at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 16 at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182) 17 at java.lang.Thread.run(Unknown Source) 18Caused by: javafx.fxml.LoadException: 19/C:/Users/省略 20 21 at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 22 at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103) 23 at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922) 24 at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971) 25 at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220) 26 at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744) 27 at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) 28 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) 29 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 30 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 31 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 32 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 33 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 34 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 35 at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 36 at application.Main.start(Main.java:21) 37 at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863) 38 at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326) 39 at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295) 40 at java.security.AccessController.doPrivileged(Native Method) 41 at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294) 42 at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 43 at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 44 at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177) 45 ... 1 more 46Caused by: java.lang.ClassNotFoundException: BookTableViewController 47 at java.net.URLClassLoader.findClass(Unknown Source) 48 at java.lang.ClassLoader.loadClass(Unknown Source) 49 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 50 at java.lang.ClassLoader.loadClass(Unknown Source) 51 at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920) 52 ... 22 more 53Exception running application application.Main

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

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

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

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

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

guest

回答1

0

ベストアンサー

Caused by: java.lang.ClassNotFoundException: BookTableViewController

とあるので、コントローラクラスの紐付けをしていないのではないですか?

参考にされたリンクの

対応するコントローラクラスの確認・変更は、左側ペイン階層ツリーで最上位のコントロール(このサンプルの場合AnchorPane)を選択状態にし、右側ペインの[コード]を展開し、[コントローラ・クラス]欄で行います。次の画面に示します。

という箇所のように設定しましたか?

投稿2019/06/26 02:32

dice142

総合スコア5158

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

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

junkjunk

2019/06/26 03:38

ありがとうございます! 記事のそこの部分が自分の動かしているScene Builerで見つからなかったのでとばしてたみたいです。 よく探したら違う位置にコントローラークラスを定義する場所があったため定義して実行したところ動きました。
dice142

2019/06/26 03:42

参考にされているサイトがJavaFX2時代のもののようですので、現行のバージョンとは結構違います。 なるべくご使用されているものと同じもの、大抵は新しいものを参考にすると良いかと思います。
junkjunk

2019/06/27 04:06

全然気づきませんでした…。確かにバージョンが違いますね。 ご指摘ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問