首页 > 代码库 > JavaFX学习之TableView

JavaFX学习之TableView

package application;


import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class MainController implements Initializable{
    @FXML private TableView<info> table;
    @FXML private TableColumn<info,Integer> id;
    @FXML private TableColumn<info,String> url;
    @FXML private TableColumn<info,Integer> code;
    @FXML private TableColumn<info,String> server;
    @FXML private TableColumn<info,String> title;
    
    
    public ObservableList<info> List = FXCollections.observableArrayList(
            
            new info(1, "http://www.baidu.com", 200,"nginx", "baidu")
            
            
            );
    
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        id.setCellValueFactory(new PropertyValueFactory<info,Integer>("id"));
        url.setCellValueFactory(new PropertyValueFactory<info,String>("url"));
        code.setCellValueFactory(new PropertyValueFactory<info,Integer>("code"));
        server.setCellValueFactory(new PropertyValueFactory<info,String>("server"));
        title.setCellValueFactory(new PropertyValueFactory<info,String>("title"));
        
        
        table.setItems(List);
        
    }
        

}

 

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



public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
            Scene scene = new Scene(loader.load());
            
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

 

package application;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class info {

    
    private SimpleIntegerProperty id;
    private SimpleStringProperty url;
    private SimpleIntegerProperty code;
    private SimpleStringProperty server;
    private SimpleStringProperty title;
    
    public info(Integer id, String url, Integer code, String server, String title) {
        super();
        this.id = new SimpleIntegerProperty(id);
        this.url = new SimpleStringProperty(url);
        this.code = new SimpleIntegerProperty(code);
        this.server = new SimpleStringProperty(server);
        this.title = new SimpleStringProperty(title);
    }

    public Integer getId() {
        return id.get();
    }

    public void setId(Integer id) {
        this.id.set(id);
    }

    public String getUrl() {
        return url.get();
    }

    public void setUrl(String url) {
        this.url.set(url);
    }

    public Integer getCode() {
        return code.get();
    }

    public void setCode(Integer code) {
        this.code.set(code);;
    }

    public String getServer() {
        return server.get();
    }

    public void setServer(String server) {
        this.server.set(server);
    }

    public String getTitle() {
        return title.get();
    }

    public void setTitle(String title) {
        this.title.set(title);
    }
    
    
    
    
    
    
    
    
}
<?xml version="1.0" encoding="UTF-8"?>

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

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <TableView fx:id="table" prefHeight="400.0" prefWidth="600.0">
        <columns>
          <TableColumn fx:id="id" prefWidth="34.0" text="id " />
          <TableColumn fx:id="url" prefWidth="186.0" text="url" />
            <TableColumn fx:id="code" minWidth="0.0" prefWidth="55.0" text="code" />
            <TableColumn fx:id="server" prefWidth="108.0" text="server" />
            <TableColumn fx:id="title" prefWidth="216.0" text="title" />
        </columns>
      </TableView>
   </children>
</Pane>

 

JavaFX学习之TableView