我怎样才能只使用 List
, ArrayList
或HashMap
在它使用 <Person>
的可观察列表中每次都是新的Person()
初始化与表列相同 PropertyValueFactory
.
我怎样才能使用:
private final ObservableList<Person> data =
FXCollections.observableArrayList( MyList/HashMap/ArrayList Here);
我很难弄清楚我在哪里犯了错误。
Oracle文档给出了一个例子here
private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7ddd6d4d8d599c4dadec3dff7d2cfd6dac7dbd299d4d8da" rel="noreferrer noopener nofollow">[email protected]</a>"),
new Person("Isabella", "Johnson", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9d0cad8dbdcd5d5d897d3d6d1d7cad6d7f9dcc1d8d4c9d5dc97dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>"),
new Person("Ethan", "Williams", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6c3d2cec7c888d1cfcacacfc7cbd5e6c3dec7cbd6cac388c5c9cb" rel="noreferrer noopener nofollow">[email protected]</a>"),
new Person("Emma", "Jones", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="70151d1d115e1a1f1e1503301508111d001c155e131f1d" rel="noreferrer noopener nofollow">[email protected]</a>"),
new Person("Michael", "Brown", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f22262c272e2a23612d3d2038210f2a372e223f232a612c2022" rel="noreferrer noopener nofollow">[email protected]</a>"));
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));
table.setItems(data);
这是我的代码:
@FXML
private TableView<ObservableList> tableViewCOA = new TableView<ObservableList>();
@FXML
private TableColumn superTypeNameCol = new TableColumn();
@FXML
private TableColumn catagoryNameCol = new TableColumn();
@FXML
private TableColumn accountNameCol = new TableColumn();
@FXML
private TableColumn accountIDCol = new TableColumn();
final ObservableList<ObservableList> data = FXCollections.observableArrayList(
getAllCOA() //<-- myObservableListHere (refer to last method of jdbc)
);
superTypeNameCol.setCellValueFactory(
new PropertyValueFactory<COA,String>("superNameCol")
);
catagoryNameCol.setCellValueFactory(
new PropertyValueFactory<COA,String>("catagoryNameCol")
);
accountIDCol.setCellValueFactory(
new PropertyValueFactory<COA,String>("accountIDCol")
);
accountNameCol.setCellValueFactory(
new PropertyValueFactory<COA,String>("accountNameCol")
);
tableViewCOA.setItems(data);
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package accountsMain;
import javafx.beans.property.SimpleStringProperty;
/**
*
* @author u1
*/
public class COA{
private final SimpleStringProperty superNameCol;
private final SimpleStringProperty catagoryNameCol;
private final SimpleStringProperty accountNameCol;
private final SimpleStringProperty accountIDCol;
public COA(String superName,
String catagoryName,
String accountName,
String accountID
){
this.superNameCol = new SimpleStringProperty(superName);
this.catagoryNameCol = new SimpleStringProperty(catagoryName);
this.accountIDCol = new SimpleStringProperty(accountID);
this.accountNameCol = new SimpleStringProperty(accountName);
}
public String getSuperNameCol() {
return superNameCol.get();
}
public void setSuperNameCol(String superName) {
superNameCol.set(superName);
}
public String getCatagoryNameCol() {
return catagoryNameCol.get();
}
public void setCatagoryNameCol(String catagoryName) {
catagoryNameCol.set(catagoryName);
}
public String getAccountIDCol() {
return accountIDCol.get();
}
public void setAccountIDCol(String accountID) {
accountIDCol.set(accountID);
}
public String getAccountNameCol() {
return accountNameCol.get();
}
public void setAccountNameCol(String accountName) {
accountNameCol.set(accountName);
}
}
///////////////////////////// this is what returns -----myObservableList-------///////////////////////////////
public static ObservableList<ObservableList> getAllCOA() {
Connection con = null;
// HashMap rows = new HashMap();
ObservableList<ObservableList> rows = FXCollections.observableArrayList();
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://"+host+":3306/"+DBname, user, password);
PreparedStatement pStmt = con.prepareStatement(
"select "
+ "supercoa.superTypeName,"
+ "catagorycoa.catagoryName,"
+ "accountscoa.accountName,"
+ "accountscoa.account_id"
+ " FROM"
+ " supercoa,catagorycoa,accountscoa"
+ " WHERE"
+ " accountscoa.catagory_id = catagorycoa.catagory_id"
+ " group by accountscoa.accountName");
ResultSet rs = null;
rs = pStmt.executeQuery();
int i = 1;
while (rs.next()) {
// List singleRow = new ArrayList();
ObservableList<String> singleRow = FXCollections.observableArrayList();
String supertypeName = rs.getString("supertypeName"); singleRow.add(supertypeName);
String catagoryName = rs.getString("catagoryName"); singleRow.add(catagoryName);
String accountName = rs.getString("accountName"); singleRow.add(accountName);
String account_id = rs.getString("account_id"); singleRow.add(account_id);
//rows.put(i,singleRow);
//i++;
rows.add(singleRow);
}
pStmt.close();
con.close();
} catch (ClassNotFoundException | SQLException e) {
main.ErrorLogging.errorLog(e.toString());
}
return rows;
}
数据未显示,但表格显示空白列
最佳答案
要从 HashMap 填充 TableView,请参阅 TableView 教程部分 Adding Maps of Data to the Table .
对于您在问题中提供的示例,最好将 getAllCOA()
方法的签名更改为:
public static ObservableList<COA> getAllCOA()
此外,当您使用 getAllCOA()
方法时,请改为:
final ObservableList<COA> data = getAllCOA();
当您定义表格时,请使用:
private TableView<COA> tableViewCOA = new TableView<COA>();
通过这种方式,您将使用 COA 对象的列表,而不仅仅是列表的列表。这将使您创建的单元格值工厂能够工作。您的单元格值工厂当前无法工作,因为它们是为了内省(introspection) COA 对象的属性而构建的,并且您没有将 COA 对象放置在表数据中。
关于java - 如何使用 hashmap 或任何列表而不是以下格式的可观察列表填充 javafx tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13006386/