java 如何使用哈希图或任何列表而不是以下格式的可观察列表填充 javafx tableview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13006386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to populate javafx tableview with a hashmap or any List instead of observable list in following format
提问by kevin
How can I just use a List
, ArrayList
or HashMap
in the the observable list where it uses <Person>
and every time a new Person()
initialization and same for table column PropertyValueFactory
.
我怎么能只使用List
,ArrayList
或者HashMap
在它使用的可观察列表中<Person>
,每次新的Person()
初始化和表列相同PropertyValueFactory
。
How can I just use :
我该如何使用:
private final ObservableList<Person> data =
FXCollections.observableArrayList( MyList/HashMap/ArrayList Here);
I am having a tough time to figure out where I am making a mistake.
我很难弄清楚我在哪里犯了错误。
Oracle docs give an example here
Oracle 文档在这里给出了一个例子
private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith", "[email protected]"),
new Person("Isabella", "Johnson", "[email protected]"),
new Person("Ethan", "Williams", "[email protected]"),
new Person("Emma", "Jones", "[email protected]"),
new Person("Michael", "Brown", "[email protected]"));
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));
table.setItems(data);
Here is my code:
这是我的代码:
@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;
}
The data doesn't appears, but the table shows with blank columns
数据不显示,但表格显示空白列
回答by jewelsea
To populate a TableView from a HashMap, see the TableView tutorial section Adding Maps of Data to the Table.
要从 HashMap 填充 TableView,请参阅 TableView 教程部分将数据映射添加到表。
For the example you have provided in your question, it is probably best to change the signature of the getAllCOA()
method to:
对于您在问题中提供的示例,最好将getAllCOA()
方法的签名更改为:
public static ObservableList<COA> getAllCOA()
Also, when you use the getAllCOA()
method, instead have:
此外,当您使用该getAllCOA()
方法时,请改为:
final ObservableList<COA> data = getAllCOA();
And when you define your table, use instead:
当您定义表时,请改用:
private TableView<COA> tableViewCOA = new TableView<COA>();
In this way you will be working with a list of COA objects, rather than just a list of lists. This will allow the cell value factories you have created to work. Your cell value factories are not working currently because they are built to introspect on the properties of a COA object and you are not placing COA objects in your table data.
通过这种方式,您将使用 COA 对象列表,而不仅仅是列表列表。这将允许您创建的单元格值工厂工作。您的单元格值工厂当前不工作,因为它们是为内省 COA 对象的属性而构建的,并且您没有将 COA 对象放置在表数据中。
回答by amru
Try to do type casting
尝试进行类型转换
private final ObservableList<Person> data = (ObservableList<Person>)
FXCollections.observableArrayList( MyList/HashMap/ArrayList Here);