━━━━ ◇ ━━━━
Java/JavaFx

JavaFX 23. TableView

1. Main.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;
 
 
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("Main.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);
    }
}
 
cs


2. Main.fxml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
 
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
 
 
<AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.MainController">
   <children>
      <TableView fx:id="table" layoutX="14.0" layoutY="14.0" prefHeight="500.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columns>
          <TableColumn fx:id="id" prefWidth="99.0" text="Id" />
          <TableColumn fx:id="name" prefWidth="133.0" text="Name" />
            <TableColumn fx:id="surname" prefWidth="147.0" text="Surname" />
            <TableColumn fx:id="age" prefWidth="120.0" text="Age" />
        </columns>
      </TableView>
   </children>
</AnchorPane>
 
cs


3. Student.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package application;
 
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
 
public class Student {
 
    private final SimpleIntegerProperty id;
    private final SimpleStringProperty name;
    private final SimpleStringProperty surname;
    private final SimpleIntegerProperty age;
    public Student(Integer id, String name, String surname, Integer age) {
        super();
        this.id = new SimpleIntegerProperty(id);
        this.name = new SimpleStringProperty (name);
        this.surname = new SimpleStringProperty (surname);
        this.age = new SimpleIntegerProperty(age);
    }
    public Integer getId() {
        return id.get();
    }
    public String getName() {
        return name.get();
    }
    public String getSurname() {
        return surname.get();
    }
    public Integer getAge() {
        return age.get();
    }
 
    
}
 
cs


4. MainController.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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<Student> table;
    @FXML private TableColumn<Student, Integer> id;
    @FXML private TableColumn<Student, String> name;
    @FXML private TableColumn<Student, String> surname;
    @FXML private TableColumn<Student, Integer> age;
    
    public ObservableList<Student> list = FXCollections.observableArrayList(
                new Student(1"Mark""surname1"22),
                new Student(2"Tom""surname2"30),
                new Student(3"Ben""surname3"15),
                new Student(4"John""surname4"65),
                new Student(5"Tomy""surname5"44),
                new Student(6"Jack""surname6"22)
            );
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        id.setCellValueFactory(new PropertyValueFactory<Student, Integer>("id"));
        name.setCellValueFactory(new PropertyValueFactory<Student, String>("name"));
        surname.setCellValueFactory(new PropertyValueFactory<Student, String>("surname"));
        age.setCellValueFactory(new PropertyValueFactory<Student, Integer>("age"));
        table.setItems(list);
        
    }
    
}
 
cs



'Java > JavaFx' 카테고리의 다른 글

JavaFX 25. WebView  (0) 2016.08.25
JavaFX 24. DatePicker  (0) 2016.08.25
JavaFX 22. RadioButton  (0) 2016.08.25
JavaFX 21. CheckBox  (0) 2016.08.25
JavaFX 20. Menu, MenuBar, MenuItem and SeparatorMenuItem  (0) 2016.08.25
COMMENT