鱼C论坛

 找回密码
 立即注册
查看: 2045|回复: 12

[已解决]javafx scene 和 tableView 数据导入 不成功

[复制链接]
发表于 2023-7-5 12:38:34 | 显示全部楼层 |阅读模式
60鱼币
本帖最后由 wow7jiao 于 2023-7-5 12:45 编辑

附件已经打包了,很简单的数据插入,但是我不会,实在弄不好了,求有缘人解答
  1. package application;
  2.        
  3. import javafx.application.Application;
  4. import javafx.beans.property.IntegerProperty;
  5. import javafx.beans.property.SimpleIntegerProperty;
  6. import javafx.beans.property.SimpleStringProperty;
  7. import javafx.beans.property.StringProperty;
  8. import javafx.collections.FXCollections;
  9. import javafx.collections.ObservableList;
  10. import javafx.fxml.FXML;
  11. import javafx.fxml.FXMLLoader;
  12. import javafx.stage.Stage;
  13. import javafx.scene.Scene;
  14. import javafx.scene.control.TableColumn;
  15. import javafx.scene.control.TableView;
  16. import javafx.scene.layout.BorderPane;
  17. import javafx.scene.layout.Pane;


  18. public class Main<T> extends Application {
  19.        
  20.         @FXML
  21.         private TableView<Person> table;
  22.         @FXML
  23.         private TableColumn<Person, Integer> ageCol;
  24.         @FXML
  25.         private TableColumn<Person, String> nameCol;
  26.    
  27.   //(1)创建并初始化数据
  28.     private final ObservableList<Person> cellData = FXCollections.observableArrayList();

  29.         public static void main(String[] args) {
  30.                 launch(args);
  31.         }

  32.         @Override
  33.         public void start(Stage primaryStage) {

  34.             cellData.add(new Person(20,"Jack"));
  35.             cellData.add(new Person(18,"Jerry"));
  36.             cellData.add(new Person(21,"Mike"));
  37.              
  38.             //(2)配置 TableColumn 列
  39.             ageCol.setCellValueFactory(cellData -> cellData.getValue().getAge().asObject());

  40.             
  41.             nameCol.setCellValueFactory(cellData -> cellData.getValue().getName());
  42.              
  43.             //(3)为 TableView 添加数据
  44.             table.setItems(cellData);
  45.             
  46.                 try {
  47. //                        BorderPane root = new BorderPane();
  48.                         Pane root = (Pane) FXMLLoader.load(getClass().getResource("Sample.fxml"));
  49.                         Scene scene = new Scene(root,400,400);
  50.                         scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  51.                         primaryStage.setScene(scene);
  52.                         primaryStage.show();
  53.                 } catch(Exception e) {
  54.                         e.printStackTrace();
  55.                 }
  56.         }
  57.        
  58.     public class Person {
  59.             private IntegerProperty age;
  60.             private StringProperty name;
  61.                            
  62.             public Person(Integer age, String name) {
  63.                     super();
  64.                     this.age = new SimpleIntegerProperty(age);
  65.                     this.name = new SimpleStringProperty(name);
  66.             }
  67.                            
  68.             public IntegerProperty getAge() {
  69.                     return age;
  70.             }
  71.             public void setAge(Integer age) {
  72.                     this.age = new SimpleIntegerProperty(age);
  73.             }
  74.             public StringProperty getName() {
  75.                     return name;
  76.             }
  77.             public void setName(String name) {
  78.                     this.name = new SimpleStringProperty(name);
  79.             }
  80.     }
  81.        

  82.    
  83.        

  84. }
复制代码
最佳答案
2023-7-5 12:38:35
你的代码中问题出现在  start()  方法中。你在加载  Sample.fxml  之前就为  TableView  添加了数据。但是此时,FXML还没有被加载,因此  TableView  及其列还是  null 。当你尝试调用  setItems()  或者  setCellValueFactory()  时,它们会触发空指针异常。

解决这个问题的一个方法是在FXML文件中设置  initialize()  方法,并在此方法中进行列的初始化和数据的添加。请注意,这需要在你的FXML文件的控制器中进行。

修改后的代码可能是这样的:

  1. package application;

  2. import javafx.fxml.FXML;
  3. import javafx.fxml.Initializable;
  4. import javafx.collections.FXCollections;
  5. import javafx.collections.ObservableList;
  6. import javafx.scene.control.TableColumn;
  7. import javafx.scene.control.TableView;
  8. import javafx.beans.property.*;

  9. import java.net.URL;
  10. import java.util.ResourceBundle;

  11. public class MainController implements Initializable {
  12.     @FXML
  13.     private TableView<Person> table;
  14.     @FXML
  15.     private TableColumn<Person, Integer> ageCol;
  16.     @FXML
  17.     private TableColumn<Person, String> nameCol;

  18.     // 创建并初始化数据
  19.     private final ObservableList<Person> cellData = FXCollections.observableArrayList();

  20.     @Override
  21.     public void initialize(URL url, ResourceBundle rb) {
  22.         cellData.add(new Person(20,"Jack"));
  23.         cellData.add(new Person(18,"Jerry"));
  24.         cellData.add(new Person(21,"Mike"));

  25.         // 配置 TableColumn 列
  26.         ageCol.setCellValueFactory(cellData -> cellData.getValue().getAge().asObject());
  27.         nameCol.setCellValueFactory(cellData -> cellData.getValue().getName());

  28.         // 为 TableView 添加数据
  29.         table.setItems(cellData);
  30.     }

  31.     public class Person {
  32.         private IntegerProperty age;
  33.         private StringProperty name;

  34.         public Person(Integer age, String name) {
  35.             super();
  36.             this.age = new SimpleIntegerProperty(age);
  37.             this.name = new SimpleStringProperty(name);
  38.         }

  39.         public IntegerProperty getAge() {
  40.             return age;
  41.         }

  42.         public void setAge(Integer age) {
  43.             this.age = new SimpleIntegerProperty(age);
  44.         }

  45.         public StringProperty getName() {
  46.             return name;
  47.         }

  48.         public void setName(String name) {
  49.             this.name = new SimpleStringProperty(name);
  50.         }
  51.     }
  52. }
复制代码


然后在  Main  类中只加载FXML文件:

  1. package application;

  2. import javafx.application.Application;
  3. import javafx.fxml.FXMLLoader;
  4. import javafx.stage.Stage;
  5. import javafx.scene.Scene;
  6. import javafx.scene.layout.Pane;

  7. public class Main extends Application {
  8.     @Override
  9.     public void start(Stage primaryStage) {
  10.         try {
  11.             Pane root = (Pane) FXMLLoader.load(getClass().getResource("Sample.fxml"));
  12.             Scene scene = new Scene(root,400,400);
  13.             scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  14.             primaryStage.setScene(scene);
  15.             primaryStage.show();
  16.         } catch(Exception e) {
  17.             e.printStackTrace();
  18.         }
  19.     }

  20.     public static void main(String[] args) {
  21.         launch(args);
  22.     }
  23. }
复制代码


在这个修改后的代码中, MainController  类负责处理所有与TableView相关的操作。并且你需要在  Sample.fxml  文件中将  MainController  设置为控制器:

  1. <AnchorPane fx:controller="application.MainController">
  2.     <!-- Rest of your FXML file -->
  3. </AnchorPane>
复制代码


如果你的  Sample.fxml  文件没有在  MainController  中指定,需要修改为上述的形式。其中  fx:controller  的值应该是  MainController  类的完整名称。这样,当FXML文件被加载时,JavaFX将会自动创建一个  MainController  实例,并将所有带  @FXML  注解的字段与FXML文件中对应的元素绑定起来。

test.zip

7.5 KB, 下载次数: 2

最佳答案

查看完整内容

你的代码中问题出现在 start() 方法中。你在加载 Sample.fxml 之前就为 TableView 添加了数据。但是此时,FXML还没有被加载,因此 TableView 及其列还是 null 。当你尝试调用 setItems() 或者 setCellValueFactory() 时,它们会触发空指针异常。 解决这个问题的一个方法是在FXML文件中设置 initialize() 方法,并在此方法中进行列的初始化和数据的添加。请注意,这需要在你的FXML文件的控制器中进行。 修改后 ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-5 12:38:35 | 显示全部楼层    本楼为最佳答案   
你的代码中问题出现在  start()  方法中。你在加载  Sample.fxml  之前就为  TableView  添加了数据。但是此时,FXML还没有被加载,因此  TableView  及其列还是  null 。当你尝试调用  setItems()  或者  setCellValueFactory()  时,它们会触发空指针异常。

解决这个问题的一个方法是在FXML文件中设置  initialize()  方法,并在此方法中进行列的初始化和数据的添加。请注意,这需要在你的FXML文件的控制器中进行。

修改后的代码可能是这样的:

  1. package application;

  2. import javafx.fxml.FXML;
  3. import javafx.fxml.Initializable;
  4. import javafx.collections.FXCollections;
  5. import javafx.collections.ObservableList;
  6. import javafx.scene.control.TableColumn;
  7. import javafx.scene.control.TableView;
  8. import javafx.beans.property.*;

  9. import java.net.URL;
  10. import java.util.ResourceBundle;

  11. public class MainController implements Initializable {
  12.     @FXML
  13.     private TableView<Person> table;
  14.     @FXML
  15.     private TableColumn<Person, Integer> ageCol;
  16.     @FXML
  17.     private TableColumn<Person, String> nameCol;

  18.     // 创建并初始化数据
  19.     private final ObservableList<Person> cellData = FXCollections.observableArrayList();

  20.     @Override
  21.     public void initialize(URL url, ResourceBundle rb) {
  22.         cellData.add(new Person(20,"Jack"));
  23.         cellData.add(new Person(18,"Jerry"));
  24.         cellData.add(new Person(21,"Mike"));

  25.         // 配置 TableColumn 列
  26.         ageCol.setCellValueFactory(cellData -> cellData.getValue().getAge().asObject());
  27.         nameCol.setCellValueFactory(cellData -> cellData.getValue().getName());

  28.         // 为 TableView 添加数据
  29.         table.setItems(cellData);
  30.     }

  31.     public class Person {
  32.         private IntegerProperty age;
  33.         private StringProperty name;

  34.         public Person(Integer age, String name) {
  35.             super();
  36.             this.age = new SimpleIntegerProperty(age);
  37.             this.name = new SimpleStringProperty(name);
  38.         }

  39.         public IntegerProperty getAge() {
  40.             return age;
  41.         }

  42.         public void setAge(Integer age) {
  43.             this.age = new SimpleIntegerProperty(age);
  44.         }

  45.         public StringProperty getName() {
  46.             return name;
  47.         }

  48.         public void setName(String name) {
  49.             this.name = new SimpleStringProperty(name);
  50.         }
  51.     }
  52. }
复制代码


然后在  Main  类中只加载FXML文件:

  1. package application;

  2. import javafx.application.Application;
  3. import javafx.fxml.FXMLLoader;
  4. import javafx.stage.Stage;
  5. import javafx.scene.Scene;
  6. import javafx.scene.layout.Pane;

  7. public class Main extends Application {
  8.     @Override
  9.     public void start(Stage primaryStage) {
  10.         try {
  11.             Pane root = (Pane) FXMLLoader.load(getClass().getResource("Sample.fxml"));
  12.             Scene scene = new Scene(root,400,400);
  13.             scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  14.             primaryStage.setScene(scene);
  15.             primaryStage.show();
  16.         } catch(Exception e) {
  17.             e.printStackTrace();
  18.         }
  19.     }

  20.     public static void main(String[] args) {
  21.         launch(args);
  22.     }
  23. }
复制代码


在这个修改后的代码中, MainController  类负责处理所有与TableView相关的操作。并且你需要在  Sample.fxml  文件中将  MainController  设置为控制器:

  1. <AnchorPane fx:controller="application.MainController">
  2.     <!-- Rest of your FXML file -->
  3. </AnchorPane>
复制代码


如果你的  Sample.fxml  文件没有在  MainController  中指定,需要修改为上述的形式。其中  fx:controller  的值应该是  MainController  类的完整名称。这样,当FXML文件被加载时,JavaFX将会自动创建一个  MainController  实例,并将所有带  @FXML  注解的字段与FXML文件中对应的元素绑定起来。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-5 12:38:46 | 显示全部楼层
您在使用JavaFX创建一个TableView并导入数据时遇到问题,以下是可能导致导入不成功的原因和解决方案:

问题:无法成功导入数据到TableView

解决方案:
1. 确保您已经正确配置FXML文件,并将TableView、TableColumn以及其他相关控件与FXML文件中的元素正确关联。
2. 确保FXML文件的路径和名称与代码中的路径和名称匹配,以确保可以正确加载FXML文件。
3. 检查Sample.fxml中的TableView、TableColumn和其他相关控件的ID是否与Main类中的FXML注解相匹配,确保它们正确绑定。
4. 确保您已经正确设置TableColumn的单元格值工厂,以从数据对象中获取正确的属性值。

根据您提供的代码,看起来您已经正确地创建了数据对象Person,并将其添加到ObservableList中。您还正确地设置了TableColumn的CellValueFactory,以从数据对象中获取年龄和姓名属性值。

如果您仍然遇到导入数据不成功的问题,可以进一步检查以下可能的问题:
- 检查FXML文件中TableView和TableColumn的定义是否正确,包括命名、类型和绑定。
- 检查TableColumn的类型参数是否与Person类的类型参数匹配。
- 确保您没有在其他地方覆盖了table.setItems()方法,确保它只被调用一次。

希望这些解决方案对您有帮助!如果问题仍然存在,请提供更多的错误信息和上下文,以便我能够更好地帮助您解决问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-5 12:55:50 | 显示全部楼层
陶远航 发表于 2023-7-5 12:38
您在使用JavaFX创建一个TableView并导入数据时遇到问题,以下是可能导致导入不成功的原因和解决方案:

问 ...

附件已经打包好, 求大师看一眼
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-5 12:57:52 | 显示全部楼层
根据你提供的代码,我看到你在JavaFX中创建了一个TableView并向其中插入了一些数据。如果你想向表格中插入更多数据,你可以通过以下方式进行:

1. 在`ObservableList<Person> cellData` 中添加新的 Person 对象。
2. 新增行的数据源后,刷新 TableView 的显示。

下面是一个示例,演示如何插入新的 Person 对象并刷新 TableView:

  1. // 创建一个新的 Person 对象
  2. Person newPerson = new Person(25, "Tom");

  3. // 向数据源 ObservableList 添加新的 Person 对象
  4. cellData.add(newPerson);

  5. // 刷新 TableView 显示
  6. table.refresh();
复制代码

将上述代码添加到你的实际应用程序中的适当位置,这样就可以将新数据插入到 TableView 中并更新界面显示。
完整的代码如下所示:

  1. package application;

  2. import javafx.application.Application;
  3. import javafx.beans.property.IntegerProperty;
  4. import javafx.beans.property.SimpleIntegerProperty;
  5. import javafx.beans.property.SimpleStringProperty;
  6. import javafx.beans.property.StringProperty;
  7. import javafx.collections.FXCollections;
  8. import javafx.collections.ObservableList;
  9. import javafx.fxml.FXML;
  10. import javafx.fxml.FXMLLoader;
  11. import javafx.stage.Stage;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.TableColumn;
  14. import javafx.scene.control.TableView;
  15. import javafx.scene.layout.Pane;

  16. public class Main extends Application {

  17.         @FXML
  18.         private TableView<Person> table;
  19.         @FXML
  20.         private TableColumn<Person, Integer> ageCol;
  21.         @FXML
  22.         private TableColumn<Person, String> nameCol;

  23.         // 创建并初始化数据
  24.         private final ObservableList<Person> cellData = FXCollections.observableArrayList();

  25.         public static void main(String[] args) {
  26.                 launch(args);
  27.         }

  28.         @Override
  29.         public void start(Stage primaryStage) {

  30.                 cellData.add(new Person(20, "Jack"));
  31.                 cellData.add(new Person(18, "Jerry"));
  32.                 cellData.add(new Person(21, "Mike"));

  33.                 // 配置 TableColumn 列
  34.                 ageCol.setCellValueFactory(cellData -> cellData.getValue().getAge().asObject());
  35.                 nameCol.setCellValueFactory(cellData -> cellData.getValue().getName());

  36.                 // 为 TableView 添加数据
  37.                 table.setItems(cellData);

  38.                 try {
  39.                         Pane root = (Pane) FXMLLoader.load(getClass().getResource("Sample.fxml"));
  40.                         Scene scene = new Scene(root, 400, 400);
  41.                         scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  42.                         primaryStage.setScene(scene);
  43.                         primaryStage.show();
  44.                 } catch (Exception e) {
  45.                         e.printStackTrace();
  46.                 }
  47.         }

  48.         public class Person {
  49.                 private IntegerProperty age;
  50.                 private StringProperty name;

  51.                 public Person(Integer age, String name) {
  52.                         super();
  53.                         this.age = new SimpleIntegerProperty(age);
  54.                         this.name = new SimpleStringProperty(name);
  55.                 }

  56.                 public IntegerProperty getAge() {
  57.                         return age;
  58.                 }

  59.                 public void setAge(Integer age) {
  60.                         this.age = new SimpleIntegerProperty(age);
  61.                 }

  62.                 public StringProperty getName() {
  63.                         return name;
  64.                 }

  65.                 public void setName(String name) {
  66.                         this.name = new SimpleStringProperty(name);
  67.                 }
  68.         }
  69. }
复制代码

这是一个完整的JavaFX应用程序,其中包含了一个TableView和相关的列定义。在start方法中,我添加了三个Person对象到TableView中并设置了表格的显示

希望能帮助到你!如果你还有其他问题,欢迎继续提问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-5 13:10:57 | 显示全部楼层
sfqxx 发表于 2023-7-5 12:57
根据你提供的代码,我看到你在JavaFX中创建了一个TableView并向其中插入了一些数据。如果你想向表格中插入 ...

我这个代码跑不起来,上来就报错,在44行, 我看不出来哪里错了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-5 14:38:26 | 显示全部楼层
本帖最后由 wow7jiao 于 2023-7-5 14:56 编辑
isdkz 发表于 2023-7-5 13:37
你的代码中问题出现在  start()  方法中。你在加载  Sample.fxml  之前就为  TableView  添加了数据。但是 ...

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <?import java.lang.*?>
  3. <?import javafx.scene.control.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.scene.layout.BorderPane?>


  6. <BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
  7.    <center>
  8.       <TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
  9.         <columns>
  10.           <TableColumn fx:id="ageCol" prefWidth="75.0" text="C1" />
  11.           <TableColumn fx:id="nameCol" prefWidth="75.0" text="C2" />
  12.         </columns>
  13.       </TableView>
  14.    </center>
  15. </BorderPane>
复制代码


请问一下怎么改,我刚才fx:controller="application.MainController"放在两个尖括号中还是报错 anchorpane 是锚固面板

test.zip

7.6 KB, 下载次数: 0

重新修改的新包

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-5 15:00:37 | 显示全部楼层
数据还没出来 不过能启动了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-5 15:04:13 From FishC Mobile | 显示全部楼层
算了吧,都解决了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-5 15:12:11 | 显示全部楼层
sfqxx 发表于 2023-7-5 15:04
算了吧,都解决了

还差一点没出来
捕获.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-5 16:52:06 | 显示全部楼层
wow7jiao 发表于 2023-7-5 15:12
还差一点没出来

你最佳都给他了,为啥不问他?我回答对我又没好处

最佳赚不到,鱼币赚不到
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-5 16:55:15 | 显示全部楼层
sfqxx 发表于 2023-7-5 16:52
你最佳都给他了,为啥不问他?我回答对我又没好处

最佳赚不到,鱼币赚不到

我又开了一个悬赏,一样的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-5 17:02:25 | 显示全部楼层
wow7jiao 发表于 2023-7-5 16:55
我又开了一个悬赏,一样的

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-3 02:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表