Ola pessoal, eu estou tendo um problema ao dar setText em uma label no javaFX, o que acontece é que não consigo transformar de int para String efetivamente, mesmo utilizando String.valueOf(), ele sempre seta o valor como 0, mesmo não sendo isso, segue a classe do controller:
package projeto.resources;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import projeto.Filmes;
import projeto.MainApp;
public class FilmeOverviewController {
@FXML
private TableView<Filmes> filmeTable;
@FXML
private TableColumn<Filmes, String> nomeColumn;
@FXML
private TableColumn<Filmes, String> categoriaColumn;
@FXML
private TableColumn<Filmes, String> salaColumn;
@FXML
private Label nomeLabel;
@FXML
private Label salaLabel;
@FXML
private Label categoriaLabel;
@FXML
private Label diretorLabel;
@FXML
private Label duracaoLabel;
@FXML
private Label protagonistaLabel;
@FXML
private Label classificacaoLabel;
@FXML
private Label capacidadeLabel;
@FXML
private Label disponivelLabel;
// referencia a classe main
private MainApp mainApp;
public FilmeOverviewController() {
}
@FXML
private void initialize() {
//Inicia a tableview com tres colunas.
nomeColumn.setCellValueFactory(cellData -> cellData.getValue().nomeProperty());
categoriaColumn.setCellValueFactory(cellData -> cellData.getValue().categoriaProperty());
salaColumn.setCellValueFactory(cellData -> cellData.getValue().getSala());
// limpando os detalhes
showFilmeDetails(null);
// adicionando funcao
filmeTable.getSelectionModel().selectedItemProperty()
.addListener((observable, oldValue, newValue) -> showFilmeDetails(newValue));
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
//adiciona uma observable list
filmeTable.setItems(mainApp.getfilmeDados());
}
private void showFilmeDetails(Filmes filme) {
if (filme != null) {
nomeLabel.setText(filme.getNome());
categoriaLabel.setText(filme.getCategoria());
duracaoLabel.setText(filme.getDuracao());
protagonistaLabel.setText(filme.getProtagonista());
classificacaoLabel.setText(filme.getClassificacao());
diretorLabel.setText(filme.getDiretor());
salaLabel.setText(filme.getSalaNumero());
capacidadeLabel.setText(String.valueOf(filme.getCapacidade()));
disponivelLabel.setText(String.valueOf(filme.getDisp()));
} else {
nomeLabel.setText("");
categoriaLabel.setText("");
duracaoLabel.setText("");
protagonistaLabel.setText("");
classificacaoLabel.setText("");
diretorLabel.setText("");
salaLabel.setText("");
capacidadeLabel.setText("");
disponivelLabel.setText("");
}
}
@FXML
private void handleDeletarFilme() {
int selectedIndex = filmeTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
filmeTable.getItems().remove(selectedIndex);
} else {
Alert alerta = new Alert(AlertType.WARNING);
alerta.setTitle("Nenhum filme selecionado");
alerta.setHeaderText("Nenhuma Selecao");
alerta.setContentText("Por favor selecione um filme para deletar");
alerta.showAndWait();
}
}
@FXML
private void handleNovoFilme() {
Filmes tempFilme = new Filmes("Nome","Categoria");
boolean clicado = mainApp.showEditarFilmeDialog(tempFilme);
if (clicado) {
mainApp.getfilmeDados().add(tempFilme);
}
}
@FXML
private void handleEditarFilme() {
Filmes filmeSelecionado = filmeTable.getSelectionModel().getSelectedItem();
if(filmeSelecionado != null) {
boolean clicado = mainApp.showEditarFilmeDialog(filmeSelecionado);
if(clicado) {
showFilmeDetails(filmeSelecionado);
}
}else {
//se nada for selecionado
Alert alerta = new Alert(AlertType.WARNING);
alerta.initOwner(mainApp.getPrimaryStage());
alerta.setTitle("Nenhuma selecao");
alerta.setHeaderText("Nenhum filme selecionado");
alerta.setContentText("Por favor selecione algum filme.");
alerta.showAndWait();
}
}
}
E esta é a classe filmes:
package projeto;
import java.time.LocalDate;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Filmes {
private final StringProperty nome;
private final StringProperty categoria;
private final Sala sala;
private final StringProperty diretor;
private final StringProperty duracao;
private final StringProperty protagonista;
private final StringProperty classificacao;
private final ObjectProperty<LocalDate> data;
public Filmes() {
this(null,null, null);
}
public Filmes(String nome, String categoria) {
this.nome = new SimpleStringProperty(nome);
this.categoria = new SimpleStringProperty(categoria);
this.sala = new Sala(false,30,30,"01");
//dados para inicializar
this.diretor = new SimpleStringProperty("Quentin Tarantino");
this.duracao = new SimpleStringProperty("2 horas");
this.protagonista = new SimpleStringProperty("Jamie Fox");
this.classificacao = new SimpleStringProperty("18");
this.data = new SimpleObjectProperty<LocalDate>(LocalDate.of(1999, 2, 21));
}
public Filmes(String nome, String categoria, Sala sala) {
this.nome = new SimpleStringProperty(nome);
this.categoria = new SimpleStringProperty(categoria);
this.sala = new Sala(false,30,30,"03");
//dados para inicializar
this.diretor = new SimpleStringProperty("Quentin Tarantino");
this.duracao = new SimpleStringProperty("2 horas");
this.protagonista = new SimpleStringProperty("Jamie Fox");
this.classificacao = new SimpleStringProperty("18");
this.data = new SimpleObjectProperty<LocalDate>(LocalDate.of(1999, 2, 21));
}
public String getNome() {
return nome.get();
}
public void setNome(String nome) {
this.nome.set(nome);
}
public StringProperty nomeProperty() {
return nome;
}
//--------------------------------------------------------
public StringProperty getSala() {
return this.sala.numeroProperty();
}
public void set3D(boolean e3d) {
sala.setE3d(e3d);
}
public int getCapacidade() {
return sala.getAssentosMax();
}
public int getDisp() {
return sala.getAssentosDisp();
}
public boolean get3D() {
boolean is3D = sala.is3d();
return is3D;
}
public String getSalaNumero() {
String temp = sala.numeroProperty().get();
return temp;
}
public String getCategoria() {
return categoria.get();
}
public void setCategoria(String nome) {
this.categoria.set(nome);
}
public StringProperty categoriaProperty() {
return categoria;
}
//--------------------------------------------------------
//--------------------------------------------------------
public String getDiretor() {
return diretor.get();
}
public void setDiretor(String diretor) {
this.diretor.set(diretor);
}
public StringProperty diretorProperty() {
return diretor;
}
//--------------------------------------------------------
public String getDuracao() {
return duracao.get();
}
public void setDuracao(String duracao) {
this.duracao.set(duracao);
}
public StringProperty duracaoProperty() {
return duracao;
}
//--------------------------------------------------------
public String getProtagonista() {
return protagonista.get();
}
public void setProtagonista(String protagonista) {
this.protagonista.set(protagonista);
}
public StringProperty protagonistaProperty() {
return protagonista;
}
//--------------------------------------------------------
public String getClassificacao() {
return classificacao.get();
}
public void setClassificacao(String classificacao) {
this.classificacao.set(classificacao);
}
public StringProperty classificacaoProperty() {
return classificacao;
}
//--------------------------------------------------------
public LocalDate getData() {
return data.get();
}
public void setData(LocalDate data) {
this.data.set(data);
}
public ObjectProperty<LocalDate> dataProperty() {
return data;
}
public void setSala(String nome) {
this.sala.setNumero(nome);
}
}
Alguém sabe o que eu estou fazendo de errado? Desculpa se é uma dúvida besta, não consegui achar aqui no fórum, segue o erro:
jun 09, 2018 10:39:20 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.171
jun 09, 2018 10:39:20 AM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.171
javafx.fxml.LoadException:
/C:/Users/Eduardo%20Abreu/Documents/Eclipse-Workspace/UnifacsProjeto/bin/projeto/resources/FilmeOverview.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at projeto.MainApp.showFilmeOverview(MainApp.java:59)
at projeto.MainApp.start(MainApp.java:50)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 13 more
Caused by: java.lang.NullPointerException
at projeto.resources.FilmeOverviewController.showFilmeDetails(FilmeOverviewController.java:89)
at projeto.resources.FilmeOverviewController.initialize(FilmeOverviewController.java:55)
... 23 more
Perdoem minha formatação, não costumo postar aqui, se houver algo de errado por favor me avisem, obrigado!