3Dayss 2024-05-23 17:49 采纳率: 100%
浏览 10
已结题

怎么在JavaFx的TableView中动态地添加数据。

语言:Java
环境:Java1.8
有关技术栈:JavaFx,gui程序设计,TablView表格组件

现在有个场景,是需要一个表格能够导入多种格式的表格,随机性很大。

但是我查阅资料,TableView组件的数据添加,需要一个实体类做媒介。但是我这情况不合适套用实体类。

所以想请教一下,TableView组件有没有通过List或者Map添加数据的方法?

  • 写回答

17条回答 默认 最新

  • svygh123 2024-05-24 01:05
    关注

    TableView组件的TableColumn是可以通过Map映射数据的,不需要实体类,在实际中可以达到动态添加列的效果:

    import java.util.HashMap;
    import java.util.Map;
    
    import javafx.application.Application;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.geometry.Insets;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.layout.VBox;
    import javafx.scene.text.Font;
    import javafx.stage.Stage;
    
    public class TableViewDemo extends Application {
     
        private final TableView table = new TableView();
        private ObservableList<Map<String, String>> data = FXCollections.observableArrayList();
        
        public static void main(String[] args) {
            launch(args);
        }
     
        @Override
        public void start(Stage stage) {
            Scene scene = new Scene(new Group());
            stage.setTitle("Table View 例子");
            stage.setWidth(400);
            stage.setHeight(500);
     
            final Label label = new Label("地址簿");
            label.setFont(new Font("Arial", 20));
     
            table.setEditable(true);
     
            TableColumn<Map<String, String>, String> firstNameCol = new TableColumn<>("姓");
            firstNameCol.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().get("姓")));
            TableColumn<Map<String, String>, String> lastNameCol = new TableColumn<>("名");
            lastNameCol.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().get("名")));
            TableColumn<Map<String, String>, String> emailCol = new TableColumn<>("邮箱地址");
            emailCol.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().get("邮箱地址")));
                   
            // 添加数据到ObservableList
            Map<String, String> row1 = new HashMap<>();
            row1.put("姓", "张");
            row1.put("名", "三");
            row1.put("邮箱地址", "zhangsan@example.com");
            data.add(row1);
     
            Map<String, String> row2 = new HashMap<>();
            row2.put("姓", "李");
            row2.put("名", "四");
            row2.put("邮箱地址", "lisi@example.com");
            data.add(row2);
            
            table.setItems(data);
            
            table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
            
            final VBox vbox = new VBox();
            vbox.setPrefWidth(380);
            vbox.setSpacing(5);
            vbox.setPadding(new Insets(10, 0, 0, 10));
            vbox.getChildren().addAll(label, table);
     
            ((Group) scene.getRoot()).getChildren().addAll(vbox);
     
            stage.setScene(scene);
            stage.show();
        }
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(16条)

报告相同问题?

问题事件

  • 系统已结题 6月1日
  • 已采纳回答 5月24日
  • 创建了问题 5月23日

悬赏问题

  • ¥15 Java与Hbase相关问题
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开
  • ¥45 工程软件单片机设计课题要求
  • ¥15 在centos7安装conda
  • ¥15 c#调用yolo3 dll文件获取的数据对不上
  • ¥20 WPF 如何实现多语言,label 和cs(live Charts)中是否都能翻译
  • ¥15 STM32F103上电短路问题
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥15 (标签-python|关键词-char)