I have the class below, the listarGeneroTreeView () method, captures the values I have in the database. However, I am not able to display this data in the treeview. I know only that it works by a message that I inserted to know if it was right.
public class RootLayoutController implements Initializable {
@FXML
private TextField txtPesquisa = new TextField();
@FXML
private Label lblStatus = new Label();
@FXML
private ProgressBar pbProgressBar = new ProgressBar();
@FXML
private TreeView<String> tvGenero;
@FXML
private TableView<DiscoTabela> tbvDiscos = new TableView();
@FXML
private TableColumn<DiscoTabela, Integer> clId;
@FXML
private TableColumn<DiscoTabela, String> clArtista;
@FXML
private TableColumn<DiscoTabela, String> clAlbum;
@FXML
private TableColumn<DiscoTabela, String> clAno_Gravado;
@FXML
private TableColumn<DiscoTabela, String> clGenero;
@FXML
private Button btnPesquisar = new Button();
@FXML
private Button btnDeletar = new Button();
@FXML
private Button btnEditar = new Button();
@FXML
private Button btnNovo = new Button();
@FXML
private Button btnNovo_Genero = new Button();
@FXML
private Button btnEditar_Genero = new Button();
@FXML
private Button btnDeletar_Genero = new Button();
@SuppressWarnings("unused")
private static final Logger logger = Logger.getLogger(Main.class.getName());
//Parametros para a clase disco
private DiscoDao discoDao = new DiscoDao();
private List<Disco> discoList = discoDao.listDisco();
private ObservableList<DiscoTabela> listDiscoTabela = FXCollections.observableArrayList();
//Parametros para a classe genero
private GeneroDao generoDao = new GeneroDao();
private List<Genero> generoList = generoDao.listGenero();
private ObservableList<String> listGeneroTabela = FXCollections.observableArrayList();
//Parametros para TreeView
private Node rootIcon = null;
private String T;
private TreeItem<String> rootNode = new TreeItem<String> ("Generos");
public void initialize(URL location, ResourceBundle resourcebundle){
//iniciaTreeView();
listarGeneroTreeView();
listarDiscos();
}
@FXML
public void novo(ActionEvent event) throws IOException{
Disco tempDisco = new Disco();
//Disco criteria = discoList.listIterator().next();
//int nextID = criteria.getId();
if(showAddDialog(tempDisco)){
discoList = discoDao.listDisco();
listarDiscos();
}
}
@FXML
public void novoGenero(ActionEvent event) throws IOException{
Genero tempGenero = new Genero();
//int crit = generoList.listIterator().next().getID();
//int nextID = crit;
if(showAddGeneroDialog(tempGenero)){
logger.log(Level.ALL,"");
generoList = generoDao.listGenero();
listarGeneroTreeView();
}
}
@FXML
public void editar(ActionEvent event) throws IOException{
DiscoTabela disco = tbvDiscos.getSelectionModel().getSelectedItem();
int id = disco.getID();
String artista = disco.getArtista();
String album = disco.getDisco();
String anoGravado = disco.getAnoGravado();
String genero = disco.getGenero();
if(showEditDialog(id, artista, album, anoGravado, genero)){
discoList = discoDao.listDisco();
listarDiscos();
}
}
@FXML
private void editaGenero(ActionEvent event) throws IOException {
}
@FXML
public void deletar(ActionEvent event) throws IOException{
DiscoTabela disco = tbvDiscos.getSelectionModel().getSelectedItem();
int id = disco.getID();
listDiscoTabela.remove(disco);
discoDao.delete(id);
discoList = discoDao.listDisco();
listarDiscos();
}
@FXML
private void deletaGenero(ActionEvent event) throws IOException{
}
@FXML
public void pesquisar(ActionEvent event) throws IOException{
}
private boolean showAddGeneroDialog(Genero genero) {
try{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(this.getClass().getResource("/fxml/AdicionaGenero.fxml"));
BorderPane addGenero = (BorderPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Nova Genero");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(null);
Scene cene = new Scene(addGenero);
dialogStage.setScene(cene);
//Define a pessoa no Controller
AdicionaGeneroController controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setGenero(genero);
dialogStage.showAndWait();
return controller.isOkClicked();
}catch (IOException ioe){
ioe.printStackTrace();
return false;
}
}
private boolean showAddDialog(Disco disco){
try{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(this.getClass().getResource("/fxml/Adiciona.fxml"));
BorderPane addLayout = (BorderPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Nova entrada");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(null);
Scene cene = new Scene(addLayout);
dialogStage.setScene(cene);
//Define a pessoa no Controller
AdicionaController controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setDisco(disco);
dialogStage.showAndWait();
return controller.isOkClicked();
}catch (IOException e){
e.printStackTrace();
return false;
}
}
private boolean showEditDialog(int id, String artista, String album, String anoGravado, String genero){
try{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(this.getClass().getResource("/fxml/Edita.fxml"));
BorderPane editLayout = (BorderPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Editar Artista");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(null);
Scene cene = new Scene(editLayout);
dialogStage.setScene(cene);
EditaController controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setDisco(id, artista, album, anoGravado, genero);
dialogStage.showAndWait();
return controller.isOkClicked();
}catch(IOException e){
e.printStackTrace();
return false;
}
}
public void listarDiscos(){
if(!listDiscoTabela.isEmpty()){
listDiscoTabela.clear();
System.out.print("Limpou tabela");
}
for(Disco disco: discoList){
try {
DiscoTabela d = new DiscoTabela(disco.getId(), disco.getArtista(), disco.getDisco(), disco.getAnoGravado(), disco.getGenero());
listDiscoTabela.add(d);
} catch (Exception e) {
e.printStackTrace();
}
}
clId.setCellValueFactory(new PropertyValueFactory<DiscoTabela, Integer>("id"));
clAlbum.setCellValueFactory(new PropertyValueFactory<DiscoTabela, String>("disco"));
clArtista.setCellValueFactory(new PropertyValueFactory<DiscoTabela, String>("artista"));
clAno_Gravado.setCellValueFactory(new PropertyValueFactory<DiscoTabela, String>("anoGravado"));
clGenero.setCellValueFactory(new PropertyValueFactory<DiscoTabela, String>("genero"));
tbvDiscos.setItems(listDiscoTabela);
}
public void listarGeneroTreeView(){
rootNode.setExpanded(true);
if(!listGeneroTabela.isEmpty()){
listGeneroTabela.clear();
System.out.print("Limpou tabela GeneroTabela");
}
for(Genero genero: generoList){
try{
GeneroTabela g = new GeneroTabela(genero.getID(), genero.getGenero());
listGeneroTabela.add(g.getGenero());
}catch(Exception e){
e.printStackTrace();
}
}
for(String genero: listGeneroTabela){
TreeItem<String> generoLeaf = new TreeItem<>(genero, insertIcon(genero, rootIcon));
rootNode.getChildren().add(generoLeaf);
}
Alert alert = new Alert(AlertType.INFORMATION);
alert.setContentText(rootNode.getChildren().toString());
alert.showAndWait();
tvGenero = new TreeView<String>(rootNode);
tvGenero.setShowRoot(true);
tvGenero.setEditable(false);
}
private Node insertIcon(String T, Node rootIcon){
String imagePath = "/icones/music folders/"+T+"/PNG/"+T+" 1.png";
Alert alert = new Alert(AlertType.INFORMATION);
rootIcon = new ImageView(
new Image(getClass().getResourceAsStream(imagePath)));
alert.setGraphic(rootIcon);
alert.showAndWait();
return rootIcon;
}
private void showGeneroDetails(Genero genero){
if(genero != null){
}
}
}