Comment faire pour remplir javafx tableview avec une table de hachage ou dans une Liste au lieu de les observables liste dans le format suivant
Comment puis-je utiliser un List
, ArrayList
ou HashMap
dans le observables liste où il utilise <Person>
et chaque fois qu'un nouveau Person()
d'initialisation et de même pour la colonne de la table PropertyValueFactory
.
Comment puis-je utiliser :
private final ObservableList<Person> data =
FXCollections.observableArrayList( MyList/HashMap/ArrayList Here);
Je vais avoir un moment difficile de comprendre où je suis de faire une erreur.
Oracle docs donner un exemple ici
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);
Voici mon 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;
}
Les données ne s'affiche, mais le montre le tableau avec les colonnes vides
- iam l'attente d'une réponse
InformationsquelleAutor kevin | 2012-10-22
Vous devez vous connecter pour publier un commentaire.
Pour remplir une TableView à partir d'une table de hachage, voir la TableView section tutoriel L'ajout de Cartes de Données de la Table.
Pour l'exemple que vous avez fourni dans votre question, il est probablement préférable de modifier la signature de la
getAllCOA()
méthode:Aussi, lorsque vous utilisez le
getAllCOA()
méthode, au lieu d'avoir:Et lorsque vous définissez votre table, utiliser à la place:
De cette façon, vous allez travailler avec une liste de COA des objets, plutôt que juste une liste de listes. Cela permettra à la valeur de la cellule usines que vous avez créé pour le travail. Votre valeur de la cellule usines ne travaillent pas actuellement parce qu'ils sont construits à l'introspection sur les propriétés d'une COA de l'objet et de vous de ne pas placer COA objets dans votre tableau de données.
Essayer de faire la conversion de type