SceneBuilderでTableViewを作成して, 次の図のように表示させたいです. コントローラーのinitializeの中でTableColumnの中身を作成し表示させようとしていて, 所望の画面表示はされるのですが, コンソールにDupuilcate TableColumnsと出ます.
TableColumnsが重複して作成されてしまっていると解釈しているのですが, どこに原因があるかわからず困っています. 原因と解決方法を教えていただけないでしょうか. よろしくお願いします.
(質問内容とは関係ありませんが, コンソールの内容は文章か画像のどちらが好ましいかも教えていただけるとうれしいです)
Eclipse4.7.0, SceneBuilder2.0, Mac
ソースコード
Java
1//メイン 2package application; 3 4import javafx.application.Application; 5import javafx.fxml.FXMLLoader; 6import javafx.scene.Scene; 7import javafx.scene.layout.VBox; 8import javafx.stage.Stage; 9 10 11public class Main extends Application { 12 @Override 13 public void start(Stage primaryStage) { 14 try { 15 primaryStage.setTitle("Sample"); 16 17 VBox root = FXMLLoader.load(getClass().getResource("../fxml/sample4.fxml")); 18 19 primaryStage.setScene(new Scene(root)); 20 primaryStage.show(); 21 22 23 } catch(Exception e) { 24 e.printStackTrace(); 25 } 26 } 27 28 public static void main(String[] args) { 29 launch(args); 30 } 31} 32
Java
1//コントローラ 2package application; 3 4import java.net.URL; 5import java.util.ResourceBundle; 6 7import clazz.Person; 8import javafx.collections.FXCollections; 9import javafx.collections.ObservableList; 10import javafx.fxml.FXML; 11import javafx.fxml.Initializable; 12import javafx.scene.control.TableColumn; 13import javafx.scene.control.TableView; 14import javafx.scene.control.cell.PropertyValueFactory; 15 16public class Sample4_controller implements Initializable { 17 @FXML private TableView<Person> tableView; 18 @FXML private TableColumn<Person, String> firstCol; 19 @FXML private TableColumn<Person, String> lastCol; 20 @FXML private TableColumn<Person, String> eMailCol; 21 22 @Override 23 public void initialize(URL location, ResourceBundle resources) { 24 Person h1 = new Person("Jun", "Nakamura", "nakamura@email"); 25 Person h2 = new Person("Ai", "Yamada", "yamada@email"); 26 Person h3 = new Person("Taro", "Tanaka", "tanaka@email"); 27 ObservableList<Person> list = FXCollections.observableArrayList(h1, h2, h3); 28 tableView.setItems(list); 29 30 firstCol.setCellValueFactory(new PropertyValueFactory<Person, String>("first")); 31 lastCol.setCellValueFactory(new PropertyValueFactory<Person, String>("last")); 32 eMailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("eMail")); 33 34 tableView.getColumns().addAll(firstCol, lastCol, eMailCol); 35 } 36 37} 38
FXML
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.control.TableColumn?> 4<?import javafx.scene.control.TableView?> 5<?import javafx.scene.layout.VBox?> 6 7 8<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Sample4_controller"> 9 <children> 10 <TableView fx:id="tableView" prefHeight="400.0" prefWidth="600.0"> 11 <columns> 12 <TableColumn fx:id="firstCol" prefWidth="150.0" text="First" /> 13 <TableColumn fx:id="lastCol" prefWidth="150.0" text="Last" /> 14 <TableColumn fx:id="eMailCol" prefWidth="300.0" text="eMail" /> 15 </columns> 16 </TableView> 17 </children> 18</VBox> 19
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/22 04:50