我正在开发一个javafx项目,我想从tableview中获取所选数据,但我不想使用类,因为列数不固定。所以,请帮助我。
例如:
public static class IdentifiedName {
private final int id;
private final String name;
private IdentifiedName(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
这个类只能用于两个列表,但在我的项目中客户端可以根据需要添加列,所以请帮我从tableview中获取所选数据
此处有更多代码:
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Map;
import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;
public class DynamicTable extends Application{
Object newValue;
private ObservableList<ObservableList> data;
private TableView<ObservableList> tableview;
public static void main(String[] args) {
launch(args);
}
//CONNECTION DATABASE
public void buildData(){
Connection c ;
data = FXCollections.observableArrayList();
try{
c = DBConnect.connect();
//SQL FOR SELECTING ALL OF CUSTOMER
String SQL = "SELECT * from CUSTOMer";
//ResultSet
ResultSet rs = c.createStatement().executeQuery(SQL);
/**********************************
* TABLE COLUMN ADDED DYNAMICALLY *
**********************************/
for(int i=0 ; i<rs.getMetaData().getColumnCount(); i++){
//We are using non property style for making dynamic table
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i+1));
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList,String>,ObservableValue<String>>(){
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param) {
return new SimpleStringProperty(param.getValue().get(j).toString());
}
});
tableview.getColumns().addAll(col);
tableview.setEditable(true);
Callback<TableColumn<Map, String>, TableCell<Map, String>>
cellFactoryForMap = new Callback<TableColumn<Map, String>,
TableCell<Map, String>>() {
@Override
public TableCell call(TableColumn p) {
return new TextFieldTableCell(new StringConverter() {
@Override
public String toString(Object t) {
return t.toString();
}
@Override
public Object fromString(String string) {
return string;
}
});
}
};
if(j!=1)
col.setCellFactory(cellFactoryForMap);
System.out.println("Column ["+i+"] ");
}
/********************************
* Data added to ObservableList *
********************************/
while(rs.next()){
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
for(int i=1 ; i<=rs.getMetaData().getColumnCount(); i++){
//Iterate Column
row.add(rs.getString(i));
}
System.out.println("Row [1] added "+row );
data.add(row);
}
//FINALLY ADDED TO TableView
tableview.setItems(data);
}catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
@Override
public void start(Stage stage) throws Exception {
//TableView
tableview = new TableView();
buildData();
//Main Scene
Scene scene = new Scene(tableview);
stage.setScene(scene);
stage.show();
tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
//Check whether item is selected and set value of selected item to Label
if(tableview.getSelectionModel().getSelectedItem() != null)
{
TableViewSelectionModel selectionModel = tableview.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();
TablePosition tablePosition = (TablePosition) selectedCells.get(0);
Object val = tablePosition.getTableColumn().getCellData(newValue);
System.out.println("Selected Value" + val);
}
}
});
TableColumn col_action = new TableColumn<>("Action");
col_action.setSortable(false);
col_action.setCellValueFactory(
new Callback<TableColumn.CellDataFeatures<JavaFXDynTable2.Record, Boolean>,
ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<JavaFXDynTable2.Record, Boolean> p) {
return new SimpleBooleanProperty(p.getValue() != null);
}
});
col_action.setCellFactory(
new Callback<TableColumn<JavaFXDynTable2.Record, Boolean>, TableCell<JavaFXDynTable2.Record, Boolean>>() {
@Override
public TableCell<JavaFXDynTable2.Record, Boolean> call(TableColumn<JavaFXDynTable2.Record, Boolean> p) {
return new ButtonCell(tableview);
}
});
tableview.getColumns().add(col_action);
}
private class ButtonCell extends TableCell<JavaFXDynTable2.Record, Boolean> {
final CheckBox cellButton = new CheckBox();
ButtonCell(final TableView tblView){
cellButton.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
}
});
}
//Display button if the row is not empty
@Override
protected void updateItem(Boolean t, boolean empty) {
super.updateItem(t, empty);
if(!empty){
setGraphic(cellButton);
}
}
}
}
答案 0 :(得分:1)
问题不是错误,但在发布代码之前修复这些错误是好的,这是我没有的数据库。
我更改了它,因此您的表格使用ObservableList<String>
,因为这就是您正在使用的内容,最好在表格中指定结构。
只有在路径中有H2 jar或将其设置为项目中的库时,才会运行此操作。您可以删除该部分代码,只使用您的数据库。
package dynamictable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Map;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;
public class DynamicTable extends Application{
private ObservableList<ObservableList<String>> data;
private TableView<ObservableList<String>> tableview;
//CONNECTION DATABASE
public void buildData() {
data = FXCollections.observableArrayList();
try {
DriverManager.registerDriver(new org.h2.Driver());
Connection c = DriverManager.getConnection("jdbc:h2:mem:");
//or you can use derby in your jdk folder
//DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
//Connection c = DriverManager.getConnection("jdbc:derby:memory:myDB;create=true");
Statement stmt = c.createStatement();
String sql = "CREATE TABLE CUSTOMER (num INTEGER, name VARCHAR(255), address VARCHAR(255))";
stmt.executeUpdate(sql);
for (int i = 0; i < 10; i++) {
sql = "INSERT INTO CUSTOMER VALUES (" + i + ",'1st string in row " + i + "','2nd string in row " + i + "')";
stmt.executeUpdate(sql);
}
//c = DBConnect.connect();
//SQL FOR SELECTING ALL OF CUSTOMER
String SQL = "SELECT * from CUSTOMER";
//ResultSet
ResultSet rs = c.createStatement().executeQuery(SQL);
/**
* ********************************
* TABLE COLUMN ADDED DYNAMICALLY *
*********************************
*/
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
//We are using non property style for making dynamic table
final int j = i;
TableColumn col = new TableColumn(rs.getMetaData().getColumnName(i + 1));
col.setCellValueFactory(new Callback<CellDataFeatures<ObservableList<String>, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<ObservableList<String>, String> param) {
return new SimpleStringProperty(param.getValue().get(j));
}
});
tableview.getColumns().add(col);
Callback<TableColumn<Map, String>, TableCell<Map, String>> cellFactoryForMap
= new Callback<TableColumn<Map, String>, TableCell<Map, String>>() {
@Override
public TableCell call(TableColumn p) {
return new TextFieldTableCell(new StringConverter() {
@Override
public String toString(Object t) {
return t.toString();
}
@Override
public Object fromString(String string) {
return string;
}
});
}
};
if (j != 1) {
col.setCellFactory(cellFactoryForMap);
}
System.out.println("Column [" + i + "] ");
}
/**
* ******************************
* Data added to ObservableList *
*******************************
*/
while (rs.next()) {
//Iterate Row
ObservableList<String> row = FXCollections.observableArrayList();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
//Iterate Column
row.add(rs.getString(i));
}
System.out.println("Row [1] added " + row);
data.add(row);
}
rs.close();
c.close();
//FINALLY ADDED TO TableView
tableview.setItems(data);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
@Override
public void start(Stage stage) throws Exception {
//TableView
tableview = new TableView();
tableview.setEditable(true);
tableview.getSelectionModel().setCellSelectionEnabled(true);
buildData();
//Main Scene
Scene scene = new Scene(tableview);
stage.setScene(scene);
stage.show();
tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
//Check whether item is selected and set value of selected item to Label
if (tableview.getSelectionModel().getSelectedItem() != null) {
TableViewSelectionModel selectionModel = tableview.getSelectionModel();
ObservableList selectedCells = selectionModel.getSelectedCells();
TablePosition tablePosition = (TablePosition) selectedCells.get(0);
Object val = tablePosition.getTableColumn().getCellData(newValue);
System.out.println("Selected Value " + val);
System.out.println("Selected row " + newValue);
}
}
});
System.out.println("");
System.out.println("List all data without using table");
for (ObservableList<String> ol : data){
System.out.print("row-> ");
for (String s : ol){
System.out.print(s+" ");
}
System.out.println("");
}
System.out.println("");
System.out.println("Show data at specific location in data");
System.out.println(" row 6 (index 5), column 2(index 1) -> "+data.get(5).get(1));
System.out.println("");
System.out.println("Show data at specific location in table");
System.out.println(" row 2, col 2 -> "+tableview.getItems().get(1).get(1));
}
public static void main(String[] args) {
launch(args);
}
}