下面列出了javafx.scene.control.TableColumn#setReorderable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** @param index Column index, -1 to add to end
* @param header Header text
*/
private void createTableColumn(final int index, final String header)
{
final TableColumn<List<ObservableCellValue>, CellValue> table_column = new TableColumn<>(header);
table_column.setCellValueFactory(VALUE_FACTORY);
// Prevent column re-ordering
// (handled via moveColumn which also re-orders the data)
table_column.setReorderable(false);
// By default, use text field editor. setColumnOptions() can replace
table_column.setCellFactory(list -> new StringTextCell());
table_column.setOnEditStart(event -> editing = true);
table_column.setOnEditCommit(event ->
{
editing = false;
final int col = event.getTablePosition().getColumn();
List<ObservableCellValue> row = event.getRowValue();
if (row == MAGIC_LAST_ROW)
{
// Entered in last row? Create new row
row = createEmptyRow();
final List<List<ObservableCellValue>> data = table.getItems();
data.add(data.size()-1, row);
}
row.get(col).setValue(event.getNewValue());
fireDataChanged();
// Automatically edit the next row, same column
editCell(event.getTablePosition().getRow() + 1, table_column);
});
table_column.setOnEditCancel(event -> editing = false);
table_column.setSortable(false);
if (index >= 0)
table.getColumns().add(index, table_column);
else
table.getColumns().add(table_column);
}
public SQLColumnSettingsComponent() {
columnsTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
value = new SQLColumnSettings();
TableColumn<SQLRowObject,String> columnName=new TableColumn<SQLRowObject,String> (value.getColumnName(0));
TableColumn<SQLRowObject,SQLExportDataType> columnType=new TableColumn<SQLRowObject,SQLExportDataType> (value.getColumnName(1));
TableColumn<SQLRowObject,String> columnValue= new TableColumn<SQLRowObject,String> (value.getColumnName(2));
columnName.setCellValueFactory(new PropertyValueFactory<>("Name")); //this is needed during connection to a database
columnType.setCellValueFactory(new PropertyValueFactory<>("Type"));
columnValue.setCellValueFactory(new PropertyValueFactory<>("Value"));
columnsTable.getColumns().addAll(columnName,columnType,columnValue); //added all the columns in the table
setValue(value);
columnsTable.setStyle("-fx-selection-bar: #3399FF; -fx-selection-bar-non-focused: #E3E3E3;"); //CSS color change on selection of row
columnsTable.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
columnName.setSortable(false);
columnValue.setSortable(false);
columnType.setSortable(false);
columnName.setReorderable(false);
columnValue.setReorderable(false);
columnType.setReorderable(false);
columnsTable.setPrefSize(550, 220);
columnsTable.setFixedCellSize(columnsTable.getFixedCellSize()+20);
columnsTable.setStyle("-fx-font: 10 \"Plain\"");
//Setting action event on cells
columnsTable.getSelectionModel().setCellSelectionEnabled(true); //individual cell selection enabled
columnsTable.setEditable(true);
//Editors on each cell
columnName.setCellFactory(TextFieldTableCell.<SQLRowObject>forTableColumn());
columnName.setOnEditCommit(event -> {
getValue().setValueAt(event.getNewValue(), event.getTablePosition().getRow(), 0);
setValue(getValue()); //refresh the table
});
columnValue.setCellFactory(TextFieldTableCell.<SQLRowObject>forTableColumn());
columnValue.setOnEditCommit(event -> {
getValue().setValueAt(event.getNewValue().toUpperCase(), event.getTablePosition().getRow(), 2);
setValue(getValue()); //refresh the table
});
ArrayList<SQLExportDataType> exportDataTypeValues=new ArrayList<SQLExportDataType>(Arrays.asList(SQLExportDataType.values()));
columnType.setCellFactory(ComboBoxTableCell.forTableColumn(FXCollections.observableArrayList(SQLExportDataType.values())));
columnType.setOnEditCommit(event -> {
boolean selected = event.getNewValue().isSelectableValue();
if(!selected){ //case of invalid(Title) datatype selection
getValue().setValueAt(exportDataTypeValues.get(exportDataTypeValues.indexOf(event.getNewValue())+1),event.getTablePosition().getRow(),1);
}
else {
getValue().setValueAt(event.getNewValue(), event.getTablePosition().getRow(), 1);
}
setValue(getValue());
});
// Add buttons
VBox buttonsPanel=new VBox(20);
addColumnButton=new Button("Add");
removeColumnButton=new Button("Remove");
addColumnButton.setOnAction(this::actionPerformed);
removeColumnButton.setOnAction(this::actionPerformed);
buttonsPanel.getChildren().addAll(addColumnButton,removeColumnButton);
this.setRight(buttonsPanel);
this.setCenter(columnsTable);
BorderPane.setMargin(buttonsPanel, new Insets(10));
}
private TableView<AlarmInfoRow> createTable(final ObservableList<AlarmInfoRow> rows,
final boolean active)
{
final SortedList<AlarmInfoRow> sorted = new SortedList<>(rows);
final TableView<AlarmInfoRow> table = new TableView<>(sorted);
// Ensure that the sorted rows are always updated as the column sorting
// of the TableView is changed by the user clicking on table headers.
sorted.comparatorProperty().bind(table.comparatorProperty());
TableColumn<AlarmInfoRow, SeverityLevel> sevcol = new TableColumn<>(/* Icon */);
sevcol.setPrefWidth(25);
sevcol.setReorderable(false);
sevcol.setResizable(false);
sevcol.setCellValueFactory(cell -> cell.getValue().severity);
sevcol.setCellFactory(c -> new SeverityIconCell());
table.getColumns().add(sevcol);
final TableColumn<AlarmInfoRow, String> pv_col = new TableColumn<>("PV");
pv_col.setPrefWidth(240);
pv_col.setReorderable(false);
pv_col.setCellValueFactory(cell -> cell.getValue().pv);
pv_col.setCellFactory(c -> new DragPVCell());
pv_col.setComparator(CompareNatural.INSTANCE);
table.getColumns().add(pv_col);
TableColumn<AlarmInfoRow, String> col = new TableColumn<>("Description");
col.setPrefWidth(400);
col.setReorderable(false);
col.setCellValueFactory(cell -> cell.getValue().description);
col.setCellFactory(c -> new DragPVCell());
col.setComparator(CompareNatural.INSTANCE);
table.getColumns().add(col);
sevcol = new TableColumn<>("Alarm Severity");
sevcol.setPrefWidth(130);
sevcol.setReorderable(false);
sevcol.setCellValueFactory(cell -> cell.getValue().severity);
sevcol.setCellFactory(c -> new SeverityLevelCell());
table.getColumns().add(sevcol);
col = new TableColumn<>("Alarm Status");
col.setPrefWidth(130);
col.setReorderable(false);
col.setCellValueFactory(cell -> cell.getValue().status);
col.setCellFactory(c -> new DragPVCell());
table.getColumns().add(col);
TableColumn<AlarmInfoRow, Instant> timecol = new TableColumn<>("Alarm Time");
timecol.setPrefWidth(200);
timecol.setReorderable(false);
timecol.setCellValueFactory(cell -> cell.getValue().time);
timecol.setCellFactory(c -> new TimeCell());
table.getColumns().add(timecol);
col = new TableColumn<>("Alarm Value");
col.setPrefWidth(100);
col.setReorderable(false);
col.setCellValueFactory(cell -> cell.getValue().value);
col.setCellFactory(c -> new DragPVCell());
table.getColumns().add(col);
sevcol = new TableColumn<>("PV Severity");
sevcol.setPrefWidth(130);
sevcol.setReorderable(false);
sevcol.setCellValueFactory(cell -> cell.getValue().pv_severity);
sevcol.setCellFactory(c -> new SeverityLevelCell());
table.getColumns().add(sevcol);
col = new TableColumn<>("PV Status");
col.setPrefWidth(130);
col.setReorderable(false);
col.setCellValueFactory(cell -> cell.getValue().pv_status);
col.setCellFactory(c -> new DragPVCell());
table.getColumns().add(col);
// Initially, sort on PV name
// - restore(Memento) might change that
table.getSortOrder().setAll(List.of(pv_col));
pv_col.setSortType(SortType.ASCENDING);
table.setPlaceholder(new Label(active ? "No active alarms" : "No acknowledged alarms"));
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
createContextMenu(table, active);
// Double-click to acknowledge or un-acknowledge
table.setRowFactory(tv ->
{
final TableRow<AlarmInfoRow> row = new TableRow<>();
row.setOnMouseClicked(event ->
{
if (event.getClickCount() == 2 && !row.isEmpty())
JobManager.schedule("ack", monitor -> client.acknowledge(row.getItem().item, active));
});
return row;
});
return table;
}
/** Create search view
*
* <p>While technically a {@link SplitPane},
* should be treated as generic {@link Node},
* using only the API defined in here
*
* @param model
* @param undo
*/
public SearchView(final Model model, final UndoableActionManager undo)
{
this.model = model;
this.undo = undo;
// Archive List
// Pattern: ____________ [Search]
pattern.setTooltip(new Tooltip(Messages.SearchPatternTT));
pattern.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(pattern, Priority.ALWAYS);
final Button search = new Button(Messages.Search);
search.setTooltip(new Tooltip(Messages.SearchTT));
final HBox search_row = new HBox(5.0, new Label(Messages.SearchPattern), pattern, search);
search_row.setAlignment(Pos.CENTER_LEFT);
pattern.setOnAction(event -> searchForChannels());
search.setOnAction(event -> searchForChannels());
// ( ) Add .. (x) Replace search result
final RadioButton result_add = new RadioButton(Messages.AppendSearchResults);
result_add.setTooltip(new Tooltip(Messages.AppendSearchResultsTT));
result_replace = new RadioButton(Messages.ReplaceSearchResults);
result_replace.setTooltip(new Tooltip(Messages.ReplaceSearchResultsTT));
final ToggleGroup result_handling = new ToggleGroup();
result_add.setToggleGroup(result_handling);
result_replace.setToggleGroup(result_handling);
result_replace.setSelected(true);
final HBox replace_row = new HBox(5.0, result_add, result_replace);
replace_row.setAlignment(Pos.CENTER_RIGHT);
// PV Name | Source
// ---------+--------
// |
final TableColumn<ChannelInfo, String> pv_col = new TableColumn<>(Messages.PVName);
pv_col.setCellValueFactory(cell -> new SimpleStringProperty(cell.getValue().getName()));
pv_col.setReorderable(false);
channel_table.getColumns().add(pv_col);
final TableColumn<ChannelInfo, String> archive_col = new TableColumn<>(Messages.ArchiveName);
archive_col.setCellValueFactory(cell -> new SimpleStringProperty(cell.getValue().getArchiveDataSource().getName()));
archive_col.setReorderable(false);
channel_table.getColumns().add(archive_col);
channel_table.setPlaceholder(new Label(Messages.SearchPatternTT));
// PV name column uses most of the space, archive column the rest
pv_col.prefWidthProperty().bind(channel_table.widthProperty().multiply(0.8));
archive_col.prefWidthProperty().bind(channel_table.widthProperty().subtract(pv_col.widthProperty()));
channel_table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
VBox.setVgrow(channel_table, Priority.ALWAYS);
search_row.setPadding(new Insets(5, 5, 0, 5));
replace_row.setPadding(new Insets(5, 5, 0, 5));
final VBox bottom = new VBox(5, search_row,
replace_row,
channel_table);
setOrientation(Orientation.VERTICAL);
getItems().setAll(archive_list, bottom);
setDividerPositions(0.2f);
final ContextMenu menu = new ContextMenu();
channel_table.setContextMenu(menu);
channel_table.setOnContextMenuRequested(this::updateContextMenu);
setupDrag();
Platform.runLater(() -> pattern.requestFocus());
}