Issue
I need help with an empty tableview. When I put items in an ObservableList and do setItems with this list, the tableview doesn't show the items from the list.
I have included Minimal, Complete, and Verifiable example(I think).
Main class.
public class GeneralController implements Initializable {
@FXML
private TableView<Subvencio> tabla;
@FXML
private TableColumn<Subvencio, Double> ctotal;
@FXML
private TableColumn<Subvencio, String> cesport;
ObservableList<Subvencio> subs = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
cesport.setCellValueFactory(new PropertyValueFactory<Subvencio, String>("Esport"));
ctotal.setCellValueFactory(new PropertyValueFactory<Subvencio, Double>("Total"));
tabla.setItems(subs);
}
//Mètode per a afegir una subvenció a la llista(Mitjansant un scene diferent i un showandWait).
@FXML
private void afegir(ActionEvent event) throws IOException {
//Carregem el controlador i inicialitzem la finestra a obrir.
FXMLLoader loader = new FXMLLoader(getClass().getResource("/model/Afegir.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
Stage ventana2= new Stage();
ventana2.setMinHeight(380);
ventana2.getIcons().add(new Image("/estil/Boo.png"));
ventana2.setMinWidth(770);
ventana2.setTitle("Afegir dades");
ventana2.initModality(Modality.APPLICATION_MODAL);
ventana2.setScene(scene);
//Fem que espere fins a ser tancada i coloquem els elements a la taula.subs = new ObservableList<Subvencio>();
ventana2.showAndWait();
tabla.setItems(subs);
}
Afegir Class.
public class AfegirController implements Initializable {
@FXML
private TextField esport;
@FXML
private TextField equips;
@FXML
private TextField despeses;
@FXML
private TextField esportistes;
@FXML
private TextField puntuacio;
@FXML
private TextField socis;
@FXML
private CheckBox festes;
@FXML
private CheckBox disiplina;
@FXML
private CheckBox voluntari;
@FXML
private CheckBox instalacions;
@FXML
private Button bafegir;
Subvencio sub = new Subvencio();
//Metode per a afegir una subvencio a la llista de elements.
@FXML
private void afegir(ActionEvent event) {
//Comprobem si tots els textfielsd estan omplits, sino es així, mostrem un error.
if ((!equips.getText().isEmpty())
&& (equips.getText().trim().length()!=0)
&& (!despeses.getText().isEmpty())
&& (despeses.getText().trim().length()!=0)
&& (!esportistes.getText().isEmpty())
&& (esportistes.getText().trim().length()!=0)
&& (!puntuacio.getText().isEmpty())
&& (puntuacio.getText().trim().length()!=0)
&& (!socis.getText().isEmpty())
&& (socis.getText().trim().length()!=0)
&& (!esport.getText().isEmpty())
&& (esport.getText().trim().length()!=0))
{
//Passem els valors de el textfield als seus corresponents formats.
int aux1 = Integer.parseInt(equips.getText());
double aux2 = Double.parseDouble(despeses.getText());
int aux3 = Integer.parseInt(esportistes.getText());
int aux4 = Integer.parseInt(puntuacio.getText());
int aux5 = Integer.parseInt(socis.getText());
boolean aux6 = festes.isSelected();
boolean aux7 = disiplina.isSelected();
boolean aux8 = voluntari.isSelected();
boolean aux9 = instalacions.isSelected();
String aux10 = esport.getText();
//Carregem un controllardor per poder crear subvencions i la creeem. en cabar aques proces la finestra es tanca.
GeneralController aux = new GeneralController();
sub.setEsport(aux10);
sub.setEquips(aux1);
sub.setEsportistes(aux3);
sub.setFestes(aux6);
sub.setFormacio(aux7);
sub.setInstalacions(aux9);
sub.setPuntuacio(aux4);
sub.setSocis(aux5);
sub.setVoluntari(aux8);
sub.setdespeses(aux2);
aux.subs.add(sub);
Node minodo = (Node) event.getSource();
minodo.getScene().getWindow().hide();
}
else {
Alert alert = new Alert(Alert.AlertType.ERROR);
//Apliquem el css cridant al metode dialogpane
DialogPane dialogPane = alert.getDialogPane();
dialogPane.getStylesheets().add(getClass().getResource("/estil/flatterfx.css").toExternalForm());
dialogPane.getStyleClass().add("button");
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.getIcons().add(
new Image(this.getClass().getResource("/estil/Boo.png").toString()));
alert.setTitle("Error");
alert.setHeaderText("Error al afegir una subvenció.");
alert.setContentText("Deus emplenar tots els cuadres de text per poder afegir una subvenció.");
Optional<ButtonType> result = alert.showAndWait();
}
}
public void inicializaSubvencio (Subvencio aux){
sub = aux;
String aux1 = String.valueOf(aux.getEsport());
String aux2 = String.valueOf(aux.getEquips());
String aux3 = String.valueOf(aux.getDespeses());
String aux4 = String.valueOf(aux.getEsportistes());
String aux5 = String.valueOf(aux.getEsportistes());
String aux6 = String.valueOf(aux.getPuntuacio());
String aux7 = String.valueOf(aux.getSocis());
String aux8 = String.valueOf(aux.getTotal());
double total = aux.getTotal();
esport.setText(aux1);
equips.setText(aux2);
despeses.setText(aux3);
esportistes.setText(aux5);
puntuacio.setText(aux6);
socis.setText(aux7);
festes.setSelected(aux.getFestes());
disiplina.setSelected(aux.getFormacio());
voluntari.setSelected(aux.getVoluntari());
instalacions.setSelected(aux.getInstalacions());
}
}
And subvencio class.
public class Subvencio {
String esport;
int equips;
double despeses;
int esportistes;
int puntuacio;
int socis;
boolean festes;
boolean formacio;
boolean voluntari;
boolean instalacions;
double total;
//Mètodes generals de creacio d'una subvenció.
public Subvencio() {
this(null, 0, 0, 0, 0, 0, 0, false, false, false, false);
}
public Subvencio(String esport, double total, double despeses, int equips, int esportistes, int puntuacio,
int socis, boolean festes, boolean formacio,
boolean voluntari, boolean instalacions) {
this.esport = esport;
this.total = 0;
this.despeses = despeses;
this.equips = equips;
this.esportistes = esportistes;
this.puntuacio = puntuacio;
this.socis = socis;
this.festes = festes;
this.formacio = formacio;
this.instalacions = instalacions;
this.voluntari = voluntari;
}
//Mètodes getters i setters per poder treballar amb els valors que contè una subvenció.
public String getEsport() {
return esport;
}
public void setEsport(String esport) {
this.esport = esport;
}
}
Edit: @James_D : I use an instance of one of your controllers because if i don't do this, netbeans say me "non-static variable subs can not be referenced from a static context" and my professor at university say me that this rapair that error. I have other example form a pc store and i use this fix and this erros isn't appear in the example but in this app it appears.
Mi idea for this app is like this: When a push a button and textfields are fill the program close that windows and put a subvencio in a tableview at the 2nd window.My problem is when i do that the tableview don't show any item on tableview.
Added a mega link with my app for check all of code and test the error. Subvencio App
I added a link because i don't know if in my minimal code can test the error. To reproduce the error you can open the app push afegir button and fill the textfields.After that, push afegir again. Thanks for helpping me.
Solution
To solve this question you can do this: In Main class load the correct controller when you open the 2 window:
private void afegir(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/model/Afegir.fxml"));
Parent root = loader.load();
//load the correct controller
AfegirController controller = loader.getController();
Scene scene = new Scene(root);
Stage ventana2= new Stage();
ventana2.setMinHeight(380);
ventana2.getIcons().add(new Image("/estil/Boo.png"));
ventana2.setMinWidth(770);
ventana2.setTitle("Afegir dades");
ventana2.initModality(Modality.APPLICATION_MODAL);
ventana2.setScene(scene);
taula.subs = new ObservableList<Subvencio>();
ventana2.showAndWait();
//Add the sub to Observable list here and add the observableList to tableview.
subs.add(controller.getSub());
tabla.setItems(subs);
}
Next in Afegir class do this:
public Subvencio getSub() {
return sub;
}
This is a public method to get acces to the sub that i like add to the list. and finally remove this line at afegir Class
GeneralController aux = new GeneralController();
aux.subs.add(sub);
I do this and my tableview show the items correctly. Thanks for help me.
Answered By - jose climent penades
Answer Checked By - Terry (JavaFixing Volunteer)