我正在创建一个JavaFX应用程序,该程序使用户可以从ResultSet中选择列并将所选列显示到新表中。 我想通过复选框选择列,最好将整个选定的列突出显示。
我可以轻松地在Java中执行此操作,因为Java具有getSelectedColumns()方法,但是自从几天前才开始尝试使用JavaFX以来,我不知道如何执行该操作。
在Java中,它是这样的:
int[] colIndices2 = table.getSelectedColumns();
int colCount = table.getSelectedColumnCount();
int rowCount = table.getRowCount();
for(int i=0; i<colIndices2.length; i++){
colNames.addElement(table.getColumnName(colIndices2[i]));
}
for(int i=0; i<rowCount; i++){
Vector<Object> row = new Vector<>(colCount);
for (int j=0; j<colIndices2.length; j++){
row.addElement(table.getValueAt(i,colIndices2[j]));
}
colData.addElement(row);
}
DefaultTableModel model = new DefaultTableModel(colData, colNames);
table2.setModel(model);
//table - populated with data from database
//table2 - table containing the selected columns from table1
我想将这些代码转换成JavaFX,但是我很难过。
答案 0 :(得分:0)
这应该让您入门...
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SelectTest extends Application
{
@Override
public void start(Stage primaryStage)
{
TableView<LineItem> table = new TableView<>();
table.setItems(FXCollections.observableArrayList(new LineItem()));
TableColumn<LineItem, String> column1 = new TableColumn<>("Test1");
column1.setCellValueFactory(cellData -> cellData.getValue().string1Property());
column1.setEditable(true);
CheckBox selectCol1 = new CheckBox();
column1.setGraphic(selectCol1);
column1.setSortable(false);
selectCol1.setOnAction((ActionEvent e) ->
{
selectColumn(column1, selectCol1.isSelected());
table.refresh();
});
table.getColumns().add(column1);
TableColumn<LineItem, String> column2 = new TableColumn<>("Test2");
column2.setCellValueFactory(cellData -> cellData.getValue().string2Property());
column2.setEditable(true);
CheckBox selectCol2 = new CheckBox();
column2.setGraphic(selectCol2);
column2.setSortable(false);
selectCol2.setOnAction((ActionEvent e) ->
{
selectColumn(column2, selectCol2.isSelected());
table.refresh();
});
table.getColumns().add(column2);
TableColumn<LineItem, String> column3 = new TableColumn<>("Test3");
column3.setCellValueFactory(cellData -> cellData.getValue().string3Property());
column3.setEditable(true);
CheckBox selectCol3 = new CheckBox();
column3.setGraphic(selectCol3);
column3.setSortable(false);
selectCol3.setOnAction((ActionEvent e) ->
{
selectColumn(column3, selectCol3.isSelected());
table.refresh();
});
table.getColumns().add(column3);
StackPane root = new StackPane();
root.getChildren().add(table);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
private static void selectColumn(TableColumn column, boolean select)
{
if (select)
{
column.setStyle("-fx-background-color: rgba(3, 169, 244, 0.5)");
} else
{
column.setStyle(null);
}
}