鱼C论坛

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

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

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

附件已经打包了,很简单的数据插入,但是我不会,实在弄不好了,求有缘人解答
package application;
        
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;


public class Main<T> extends Application {
        
        @FXML
        private TableView<Person> table;
        @FXML
        private TableColumn<Person, Integer> ageCol;
        @FXML
        private TableColumn<Person, String> nameCol;
    
  //(1)创建并初始化数据
    private final ObservableList<Person> cellData = FXCollections.observableArrayList();

        public static void main(String[] args) {
                launch(args);
        }

        @Override
        public void start(Stage primaryStage) {

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

            
            nameCol.setCellValueFactory(cellData -> cellData.getValue().getName());
             
            //(3)为 TableView 添加数据
            table.setItems(cellData);
            
                try {
//                        BorderPane root = new BorderPane();
                        Pane root = (Pane) FXMLLoader.load(getClass().getResource("Sample.fxml"));
                        Scene scene = new Scene(root,400,400);
                        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                        primaryStage.setScene(scene);
                        primaryStage.show();
                } catch(Exception e) {
                        e.printStackTrace();
                }
        }
        
    public class Person {
            private IntegerProperty age;
            private StringProperty name;
                            
            public Person(Integer age, String name) {
                    super();
                    this.age = new SimpleIntegerProperty(age);
                    this.name = new SimpleStringProperty(name);
            }
                            
            public IntegerProperty getAge() {
                    return age;
            }
            public void setAge(Integer age) {
                    this.age = new SimpleIntegerProperty(age);
            }
            public StringProperty getName() {
                    return name;
            }
            public void setName(String name) {
                    this.name = new SimpleStringProperty(name);
            }
    }
        

    
        

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

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

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

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.beans.property.*;

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

public class MainController implements Initializable {
    @FXML
    private TableView<Person> table;
    @FXML
    private TableColumn<Person, Integer> ageCol;
    @FXML
    private TableColumn<Person, String> nameCol;

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

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

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

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

    public class Person {
        private IntegerProperty age;
        private StringProperty name;

        public Person(Integer age, String name) {
            super();
            this.age = new SimpleIntegerProperty(age);
            this.name = new SimpleStringProperty(name);
        }

        public IntegerProperty getAge() {
            return age;
        }

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

        public StringProperty getName() {
            return name;
        }

        public void setName(String name) {
            this.name = new SimpleStringProperty(name);
        }
    }
}

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

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Pane root = (Pane) FXMLLoader.load(getClass().getResource("Sample.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

在这个修改后的代码中, MainController  类负责处理所有与TableView相关的操作。并且你需要在  Sample.fxml  文件中将  MainController  设置为控制器:
<AnchorPane fx:controller="application.MainController">
    <!-- Rest of your FXML file -->
</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文件的控制器中进行。 修改后 ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

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

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.beans.property.*;

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

public class MainController implements Initializable {
    @FXML
    private TableView<Person> table;
    @FXML
    private TableColumn<Person, Integer> ageCol;
    @FXML
    private TableColumn<Person, String> nameCol;

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

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

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

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

    public class Person {
        private IntegerProperty age;
        private StringProperty name;

        public Person(Integer age, String name) {
            super();
            this.age = new SimpleIntegerProperty(age);
            this.name = new SimpleStringProperty(name);
        }

        public IntegerProperty getAge() {
            return age;
        }

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

        public StringProperty getName() {
            return name;
        }

        public void setName(String name) {
            this.name = new SimpleStringProperty(name);
        }
    }
}

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

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Pane root = (Pane) FXMLLoader.load(getClass().getResource("Sample.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

在这个修改后的代码中, MainController  类负责处理所有与TableView相关的操作。并且你需要在  Sample.fxml  文件中将  MainController  设置为控制器:
<AnchorPane fx:controller="application.MainController">
    <!-- Rest of your FXML file -->
</AnchorPane>

如果你的  Sample.fxml  文件没有在  MainController  中指定,需要修改为上述的形式。其中  fx:controller  的值应该是  MainController  类的完整名称。这样,当FXML文件被加载时,JavaFX将会自动创建一个  MainController  实例,并将所有带  @FXML  注解的字段与FXML文件中对应的元素绑定起来。
想知道小甲鱼最近在做啥?请访问 -> 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()方法,确保它只被调用一次。

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

使用道具 举报

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

问 ...

附件已经打包好, 求大师看一眼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

下面是一个示例,演示如何插入新的 Person 对象并刷新 TableView:
// 创建一个新的 Person 对象
Person newPerson = new Person(25, "Tom");

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

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

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.Pane;

public class Main extends Application {

        @FXML
        private TableView<Person> table;
        @FXML
        private TableColumn<Person, Integer> ageCol;
        @FXML
        private TableColumn<Person, String> nameCol;

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

        public static void main(String[] args) {
                launch(args);
        }

        @Override
        public void start(Stage primaryStage) {

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

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

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

                try {
                        Pane root = (Pane) FXMLLoader.load(getClass().getResource("Sample.fxml"));
                        Scene scene = new Scene(root, 400, 400);
                        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                        primaryStage.setScene(scene);
                        primaryStage.show();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public class Person {
                private IntegerProperty age;
                private StringProperty name;

                public Person(Integer age, String name) {
                        super();
                        this.age = new SimpleIntegerProperty(age);
                        this.name = new SimpleStringProperty(name);
                }

                public IntegerProperty getAge() {
                        return age;
                }

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

                public StringProperty getName() {
                        return name;
                }

                public void setName(String name) {
                        this.name = new SimpleStringProperty(name);
                }
        }
}
这是一个完整的JavaFX应用程序,其中包含了一个TableView和相关的列定义。在start方法中,我添加了三个Person对象到TableView中并设置了表格的显示

希望能帮助到你!如果你还有其他问题,欢迎继续提问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

我这个代码跑不起来,上来就报错,在44行, 我看不出来哪里错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>


<BorderPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
   <center>
      <TableView fx:id="table" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn fx:id="ageCol" prefWidth="75.0" text="C1" />
          <TableColumn fx:id="nameCol" prefWidth="75.0" text="C2" />
        </columns>
      </TableView>
   </center>
</BorderPane>

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

test.zip

7.6 KB, 下载次数: 0

重新修改的新包

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-5 15:00:37 | 显示全部楼层
数据还没出来 不过能启动了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-7-5 15:04:13 From FishC Mobile | 显示全部楼层
算了吧,都解决了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

还差一点没出来
捕获.PNG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

最佳赚不到,鱼币赚不到
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

最佳赚不到,鱼币赚不到

我又开了一个悬赏,一样的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 08:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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