zoukankan      html  css  js  c++  java
  • javafx基于使用fxml布局的tableview数据绑定用法

    来个简单明了的

    fxml的tableview数据绑定和java代码方式的数据绑定很像,不同的在于要有一到映射

    首先看个目录

    1.界面文件Sample.fxml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3 <?import java.lang.*?>
     4 <?import java.util.*?>
     5 <?import javafx.scene.control.*?>
     6 <?import javafx.scene.layout.*?>
     7 <?import javafx.scene.paint.*?>
     8 
     9 <AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" 
    10             xmlns:fx="http://javafx.com/fxml" xmlns="http://javafx.com/javafx/2.2"
    11             fx:controller="test.SampleController" 
    12 >
    13   <children>
    14     <TableView layoutX="132.0" layoutY="33.0" prefHeight="107.0" prefWidth="310.0" fx:id="tview">
    15       <columns>
    16         <TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="137.0" text="用户名" fx:id="colUsername"/>
    17         <TableColumn maxWidth="5000.0" minWidth="10.0" prefWidth="143.0" text="密码" fx:id="colPassword"/>
    18       </columns>
    19     </TableView>
    20   </children>
    21 </AnchorPane>
    View Code

    2.SampleController代码

     1 package test;
     2 
     3 import java.net.URL;
     4 import java.util.ResourceBundle;
     5 import javafx.collections.FXCollections;
     6 import javafx.collections.ObservableList;
     7 import javafx.fxml.FXML;
     8 import javafx.fxml.Initializable;
     9 import javafx.scene.control.TableColumn;
    10 import javafx.scene.control.TableView;
    11 import javafx.scene.control.cell.PropertyValueFactory;
    12 
    13 
    14 public class SampleController implements Initializable {
    15     
    16     @FXML private TableView tview;
    17     @FXML private TableColumn colUsername;
    18     @FXML private TableColumn colPassword;
    19    
    20     
    21     public void showList(){
    22         ObservableList<User> list = FXCollections.observableArrayList();
    23         User user = new User();//构建值对象
    24          user.setUsername("小六");
    25          user.setPassword("123");
    26                  
    27         colUsername.setCellValueFactory(new PropertyValueFactory("username"));//映射          
    28         colPassword.setCellValueFactory(new PropertyValueFactory("password"));          
    29         
    30          list.add(user);        //list添加值对象
    31          tview.setItems(list); //tableview添加list
    32        }
    33    
    34     
    35     @Override
    36     public void initialize(URL url, ResourceBundle rb) {
    37         showList();
    38     }    
    39 }
    View Code

    3.User.java代码

    package test;
    public class User {
        private String username;
        private String password;
    
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
    }
    View Code

    4.Test.java

    package test;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Test extends Application {
        
        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    View Code

    然后达到效果

  • 相关阅读:
    [hdu 2089]简单数位dp
    [fzu 2271]不改变任意两点最短路至多删的边数
    [bzoj 1143]最长反链二分图最大匹配
    [codeforces gym Matrix God]随机矩阵乘法
    [hdu 2298] 物理推导+二分答案
    url编码有个bug,不能直接用decodeURIComponent,如果遇到前面的$会报错。
    设置cookie的保存时间 下一篇
    js操作获取和设置cookie
    简单使用location.hash的方法 ,怎么做,有什么用? 简单的js路由页面方法。
    三个月之内开发项目最好用第三方库
  • 原文地址:https://www.cnblogs.com/xiaoliu66007/p/3304835.html
Copyright © 2011-2022 走看看