/
Maksim_Shchelochok
/
Course_Work
Обзор
Документация
Войти
/
Maksim_Shchelochok
/
Course_Work
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/view/EntityTableView.java
80 строк
3 KB
Maksim
Создание универсального представления для CRUD над любой таблицей
21 июн 2026, 16:03
21 июн 2026, 16:03
3eafc2d
Код
Авторство
О чём код?
package view; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.scene.control.Button; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import model.Entity; import java.util.List; public class EntityTableView extends VBox { private TableView<Object[]> table = new TableView<>(); private ObservableList<Object[]> rows = FXCollections.observableArrayList(); private final Button addButton = new Button("Добавить"); private final Button editButton = new Button("Изменить"); private final Button deleteButton = new Button("Удалить"); private final Button refreshButton = new Button("Обновить"); public EntityTableView(String[] columnNames, boolean canEdit){ this.setSpacing(10); this.setPadding(new Insets(15)); buildColumns(columnNames); table.setItems(rows); HBox buttons = new HBox(10); if(canEdit){ // Кнопки редактирования видны только привилегированным. buttons.getChildren().addAll(addButton, editButton, deleteButton, refreshButton); } else{ buttons.getChildren().add(refreshButton); } this.getChildren().addAll(table, buttons); } // Создает таблицы по из названиеям private void buildColumns(String[] columnNames) { table.getColumns().clear(); for (int i = 0; i < columnNames.length; i++) { final int colIndex = i; TableColumn<Object[], String> col = new TableColumn<>(columnNames[i]); // Для каждой строки (Object[]) берём значение нужного столбца. col.setCellValueFactory(cell -> { Object[] rowData = cell.getValue(); Object value = (colIndex < rowData.length) ? rowData[colIndex] : null; return new javafx.beans.property.SimpleStringProperty( value == null ? "" : value.toString()); }); table.getColumns().add(col); } } // Заполняет таблицу данными из списка сущностей. public void setData(List<? extends Entity> entities) { rows.clear(); for (Entity e : entities) { rows.add(e.getRowValues()); } } // Возвращает индекс выбранной строки public int getSelectedIndex() { return table.getSelectionModel().getSelectedIndex(); } // Возвращает значения выбранной строки public Object[] getSelectedRow() { return table.getSelectionModel().getSelectedItem(); } public Button getAddButton() {return addButton;} public Button getEditButton() {return editButton;} public Button getDeleteButton() {return deleteButton;} public Button getRefreshButton() {return refreshButton;} }