-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
TableViewsController.java
108 lines (93 loc) · 4.61 KB
/
TableViewsController.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright (C) 2022 Parisi Alessandro
* This file is part of MaterialFX (https://github.com/palexdev/MaterialFX).
*
* MaterialFX is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MaterialFX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MaterialFX. If not, see <http://www.gnu.org/licenses/>.
*/
package io.github.palexdev.materialfx.demo.controllers;
import io.github.palexdev.materialfx.controls.MFXPaginatedTableView;
import io.github.palexdev.materialfx.controls.MFXTableColumn;
import io.github.palexdev.materialfx.controls.MFXTableView;
import io.github.palexdev.materialfx.controls.cell.MFXTableRowCell;
import io.github.palexdev.materialfx.demo.model.Device;
import io.github.palexdev.materialfx.demo.model.Model;
import io.github.palexdev.materialfx.demo.model.Person;
import io.github.palexdev.materialfx.filter.EnumFilter;
import io.github.palexdev.materialfx.filter.IntegerFilter;
import io.github.palexdev.materialfx.filter.StringFilter;
import io.github.palexdev.materialfx.utils.others.observables.When;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import java.net.URL;
import java.util.Comparator;
import java.util.ResourceBundle;
public class TableViewsController implements Initializable {
@FXML
private MFXTableView<Person> table;
@FXML
private MFXPaginatedTableView<Device> paginated;
@Override
public void initialize(URL location, ResourceBundle resources) {
setupTable();
setupPaginated();
table.autosizeColumnsOnInitialization();
paginated.autosizeColumnsOnInitialization();
When.onChanged(paginated.currentPageProperty())
.then((oldValue, newValue) -> paginated.autosizeColumns())
.listen();
}
private void setupTable() {
MFXTableColumn<Person> nameColumn = new MFXTableColumn<>("Name", true, Comparator.comparing(Person::getName));
MFXTableColumn<Person> surnameColumn = new MFXTableColumn<>("Surname", true, Comparator.comparing(Person::getSurname));
MFXTableColumn<Person> ageColumn = new MFXTableColumn<>("Age", true, Comparator.comparing(Person::getAge));
nameColumn.setRowCellFactory(person -> new MFXTableRowCell<>(Person::getName));
surnameColumn.setRowCellFactory(person -> new MFXTableRowCell<>(Person::getSurname));
ageColumn.setRowCellFactory(person -> new MFXTableRowCell<>(Person::getAge) {{
setAlignment(Pos.CENTER_RIGHT);
}});
ageColumn.setAlignment(Pos.CENTER_RIGHT);
table.getTableColumns().addAll(nameColumn, surnameColumn, ageColumn);
table.getFilters().addAll(
new StringFilter<>("Name", Person::getName),
new StringFilter<>("Surname", Person::getSurname),
new IntegerFilter<>("Age", Person::getAge)
);
table.setItems(Model.people);
}
private void setupPaginated() {
MFXTableColumn<Device> idColumn = new MFXTableColumn<>("ID", false, Comparator.comparing(Device::getID));
MFXTableColumn<Device> nameColumn = new MFXTableColumn<>("Name", false, Comparator.comparing(Device::getName));
MFXTableColumn<Device> ipColumn = new MFXTableColumn<>("IP", false, Comparator.comparing(Device::getIP));
MFXTableColumn<Device> ownerColumn = new MFXTableColumn<>("Owner", false, Comparator.comparing(Device::getOwner));
MFXTableColumn<Device> stateColumn = new MFXTableColumn<>("State", false, Comparator.comparing(Device::getState));
idColumn.setRowCellFactory(device -> new MFXTableRowCell<>(Device::getID));
nameColumn.setRowCellFactory(device -> new MFXTableRowCell<>(Device::getName));
ipColumn.setRowCellFactory(device -> new MFXTableRowCell<>(Device::getIP) {{
setAlignment(Pos.CENTER_RIGHT);
}});
ownerColumn.setRowCellFactory(device -> new MFXTableRowCell<>(Device::getOwner));
stateColumn.setRowCellFactory(device -> new MFXTableRowCell<>(Device::getState));
ipColumn.setAlignment(Pos.CENTER_RIGHT);
paginated.getTableColumns().addAll(idColumn, nameColumn, ipColumn, ownerColumn, stateColumn);
paginated.getFilters().addAll(
new IntegerFilter<>("ID", Device::getID),
new StringFilter<>("Name", Device::getName),
new StringFilter<>("IP", Device::getIP),
new StringFilter<>("Owner", Device::getOwner),
new EnumFilter<>("State", Device::getState, Device.State.class)
);
paginated.setItems(Model.devices);
}
}