具有不同单元格类型和未知大小的JavaFX TableView
我试图从各种csv文件中创建一个tableview,这些文件包含字符串、双精度字符和未知数量的列 通过一些小改动,我可以使用这里的答案创建动态表视图: 除此之外,使用本教程,我还可以创建一个包含字符串和按列加倍的表视图:具有不同单元格类型和未知大小的JavaFX TableView,java,javafx-8,Java,Javafx 8,我试图从各种csv文件中创建一个tableview,这些文件包含字符串、双精度字符和未知数量的列 通过一些小改动,我可以使用这里的答案创建动态表视图: 除此之外,使用本教程,我还可以创建一个包含字符串和按列加倍的表视图: 但是,由于我不知道有多少列,我无法创建一个类来定义我拥有的数据,并且由于我需要能够基于双精度和字符串进行排序,因此我无法使用第一个解决方案,该解决方案使用StringProperty的ObservableList来定义数据。有没有人有这方面的经验,或者对我可以选择的路线有什么
但是,由于我不知道有多少列,我无法创建一个类来定义我拥有的数据,并且由于我需要能够基于双精度和字符串进行排序,因此我无法使用第一个解决方案,该解决方案使用StringProperty的ObservableList来定义数据。有没有人有这方面的经验,或者对我可以选择的路线有什么建议?谢谢这里唯一真正的策略是读取字符串中的所有数据,然后检查每列中的数据以确定类型 然后可以根据检测到的类型动态创建列 对于表的
项
,使用列表
作为每行的表示,然后使用检测到的类型将正确类型的对象放入行中的每个插槽中
下面是一个简单的例子:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class DynamicTableWithTypes extends Application {
private final Pattern intPattern = Pattern.compile("-?[0-9]+");
// this could probably be improved: demo purposes only
private final Pattern doublePattern = Pattern.compile("-?(([0-9]+)|([0-9]*\\.[0-9]+))");
@Override
public void start(Stage primaryStage) {
TableView<List<Object>> table = new TableView<>();
String[][] rawData = generateTestData();
int numCols = computeMaxRowLength(rawData);
Class<?>[] types = new Class<?>[numCols];
for (int columnIndex = 0 ; columnIndex < numCols ; columnIndex++) {
String[] column = extractColumn(rawData, columnIndex);
types[columnIndex] = deduceColumnType(column);
table.getColumns().add(createColumn(types[columnIndex], columnIndex));
}
for (int rowIndex = 0 ; rowIndex < rawData.length ; rowIndex++) {
List<Object> row = new ArrayList<>();
for (int columnIndex = 0 ; columnIndex < numCols ; columnIndex++) {
row.add(getDataAsType(rawData[rowIndex], types[columnIndex], columnIndex));
}
table.getItems().add(row);
}
Scene scene = new Scene(table, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
private Object getDataAsType(String[] row, Class<?> type, int columnIndex) {
if (type == Integer.class) {
if (columnIndex < row.length) {
return Integer.valueOf(row[columnIndex]);
} else {
return new Integer(0);
}
} else if (type == Double.class) {
if (columnIndex < row.length) {
return Double.valueOf(row[columnIndex]);
} else {
return new Double(0.0);
}
} else {
if (columnIndex < row.length) {
return row[columnIndex];
} else {
return "" ;
}
}
}
private TableColumn<List<Object>, ?> createColumn(Class<?> type, int index) {
String text = "Column "+(index+1);
if (type==Integer.class) {
TableColumn<List<Object>, Number> col = new TableColumn<>(text);
col.setCellValueFactory(data -> new SimpleIntegerProperty((Integer)data.getValue().get(index)));
return col ;
} else if (type == Double.class) {
TableColumn<List<Object>, Number> col = new TableColumn<>(text);
col.setCellValueFactory(data -> new SimpleDoubleProperty((Double)data.getValue().get(index)));
return col ;
} else {
TableColumn<List<Object>, String> col = new TableColumn<>(text);
col.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().get(index).toString()));
return col ;
}
}
private Class<?> deduceColumnType(String[] column) {
boolean allInts = true ;
boolean allDoubles = true ;
for (int i = 0 ; i < column.length ; i++) {
allInts = allInts && intPattern.matcher(column[i]).matches() ;
allDoubles = allDoubles && doublePattern.matcher(column[i]).matches() ;
}
if (allInts) {
return Integer.class ;
}
if (allDoubles) {
return Double.class ;
}
return String.class ;
}
private int computeMaxRowLength(Object[][] array) {
int maxLength = Integer.MIN_VALUE ;
for (int i = 0 ; i < array.length ; i++) {
if (array[i].length > maxLength) {
maxLength = array[i].length ;
}
}
return maxLength ;
}
private String[] extractColumn(String[][] data, int columnIndex) {
String[] column = new String[data.length];
for (int i = 0 ; i < data.length ; i++) {
if (columnIndex < data[i].length) {
column[i]=data[i][columnIndex];
} else {
column[i]="";
}
}
return column ;
}
private String[][] generateTestData() {
// in real life, read from CSV file/database, etc...
return new String[][] {
{"A1", "B2", "10", "-5.3"},
{"A2", "B1", "15", "12.6"},
{"A3", "B3", "5", "10.2"}
};
}
public static void main(String[] args) {
launch(args);
}
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.regex.Pattern;
导入javafx.application.application;
导入javafx.beans.property.SimpleDoubleProperty;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.scene.scene;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.stage.stage;
公共类DynamicTableWithTypes扩展了应用程序{
私有最终模式intPattern=Pattern.compile(“-?[0-9]+”);
//这可能需要改进:仅用于演示目的
私有最终模式doublePattern=Pattern.compile(“-?([0-9]+)|([0-9]*\\.[0-9]+)”;
@凌驾
公共无效开始(阶段primaryStage){
TableView table=新TableView();
字符串[][]rawData=generateTestData();
int numCols=computeMaxRowLength(rawData);
类[]类型=新类[numCols];
对于(int columnIndex=0;columnIndex新建SimpleIntegerProperty((整数)data.getValue().get(索引));
返回列;
}else if(type==Double.class){
TableColumn col=新的TableColumn(文本);
col.setCellValueFactory(数据->新建SimpleDoubleProperty((双精度)data.getValue().get(索引));
返回列;
}否则{
TableColumn col=新的TableColumn(文本);
col.setCellValueFactory(数据->新建SimpleStringProperty(data.getValue().get(index.toString()));
返回列;
}
}
私有类ColumnType(字符串[]列){
布尔allint=true;
布尔值allDoubles=true;
for(int i=0;imaxLength){
maxLength=数组[i]。长度;
}
}
返回最大长度;
}
私有字符串[]提取列(字符串[][]数据,int columnIndex){
String[]column=新字符串[data.length];
对于(int i=0;i
如你所见,这