我从另一个控制器更改标签时遇到了一些麻烦,这里是我的项目的结构,我有一个带有tableview的OverviewController类,所以我想使用在那个中选择的对象tableview,因为我从OverviewController中的按钮打开另一个窗口,我决定使用句柄来创建舞台,以及这样的一切:
public class EditarFilmeController {
@FXML
private CheckBox is3D;
@FXML
private JFXTextField tituloField;
@FXML
private JFXTextField categoriaField;
@FXML
private JFXTextField protagonistaField;
@FXML
private JFXTextField duracaoField;
@FXML
private JFXTextField diretorField;
@FXML
private JFXTextField capacidadeField;
@FXML
private JFXTextField classificacaoField;
@FXML
private JFXTextField salaField;
private Stage dialogStage;
private Filmes filme;
private boolean clicado = false;
@FXML
private void initialize() {
}
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
public void setFilme(Filmes filme) {
this.filme = filme;
tituloField.setText(filme.getNome());
categoriaField.setText(filme.getCategoria());
protagonistaField.setText(filme.getProtagonista());
duracaoField.setText(filme.getDuracao());
diretorField.setText(filme.getDiretor());
classificacaoField.setText(filme.getClassificacao());
salaField.setText(filme.getSalaNumero());
capacidadeField.setText(Integer.toString(filme.getCapacidade()));
}
public boolean clicado() {
return clicado;
}
// se o usuario clicar aceitar:
@FXML
private void handleAceitar() {
if (isInputValido()) {
filme.setNome(tituloField.getText());
filme.setCategoria(categoriaField.getText());
filme.setClassificacao(classificacaoField.getText());
filme.setProtagonista(protagonistaField.getText());
filme.setDuracao(duracaoField.getText());
filme.setDiretor(diretorField.getText());
filme.setSala(salaField.getText());
clicado = true;
dialogStage.close();
}
}
@FXML
void handle3D(ActionEvent event) {
filme.set3D(is3D.isSelected());
}
public boolean getis3D() {
return is3D.isSelected();
}
@FXML
private void handleCancelar() {
dialogStage.close();
}
private boolean isInputValido() {
String mensagemErro = "";
if (tituloField.getText() == null || tituloField.getText().length() == 0) {
mensagemErro += "Titulo invalido ou vazio!\n";
}
if (categoriaField.getText() == null || categoriaField.getText().length() == 0) {
mensagemErro += "Categoria invalida ou vazia!\n";
}
if (classificacaoField.getText() == null || classificacaoField.getText().length() == 0) {
mensagemErro += "Classificacao invalida ou vazia!\n";
}
if (protagonistaField.getText() == null || protagonistaField.getText().length() == 0) {
mensagemErro += "Protagonista invalido ou vazio!\n";
}
if (duracaoField.getText() == null || duracaoField.getText().length() == 0) {
mensagemErro += "Duracao invalido ou vazio!\n";
}
if (diretorField.getText() == null || diretorField.getText().length() == 0) {
mensagemErro += "Diretor invalido ou vazio!\n";
}
if (mensagemErro.length() == 0) {
return true;
} else {
Alert alerta = new Alert(AlertType.WARNING);
alerta.initOwner(dialogStage);
alerta.setHeaderText("Por favor corrija as informações");
alerta.setTitle("Informação inválida");
alerta.setContentText(mensagemErro);
alerta.showAndWait();
return false;
}
}
}
问题是我想根据在tableview(filmeSelecionado)中选择的内容在控制器上设置标签,我无法在stackoverflow上找到解决方案,因为关于这个问题的另一个问题是&n&与我的问题相同,我尝试使用venderIngressoController.setCategoria(filmeSelecionado.getCategoria());
正如你在代码中看到的,但是它给了我一个nullException,这是另一个类中的setCategoria:
public void setCategoria (String nome) {
categoriaLabel.setText(nome);
}
我无法找到改变标签文字的方法,而不会让我感觉不舒服。有谁知道如何解决这一问题?谢谢!
编辑:
package projeto.resources;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import projeto.Filmes;
public class VenderIngressoController {
@FXML
private Label tituloLabel;
@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 e3dLabel;
@FXML
private JFXButton gerarIngresso;
@FXML
private JFXButton cancelarIngresso;
@FXML
private CheckBox meiaEntradaBox;
@FXML
private JFXTextField precoEspecialField;
@FXML
private CheckBox checkPrecoEspecial;
private Filmes filme;
private Stage dialogStage;
private int precoEspecial;
public int getPrecoEspecial() {
return precoEspecial;
}
public void setPrecoEspecial(int precoEspecial) {
this.precoEspecial = precoEspecial;
}
public int getPreco() {
return preco;
}
public void setPreco(int preco) {
this.preco = preco;
}
public int getMeiaEntrada() {
return meiaEntrada;
}
public void setMeiaEntrada(int meiaEntrada) {
this.meiaEntrada = meiaEntrada;
}
private int preco;
private int meiaEntrada;
@FXML
private void initialize() {
}
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
public void VenderIngresso(Filmes filme) {
this.filme = filme;
tituloLabel.setText(filme.getNome());
salaLabel.setText(filme.getSalaNumero());
categoriaLabel.setText(filme.getCategoria());
diretorLabel.setText(filme.getDiretor());
duracaoLabel.setText(filme.getDuracao());
protagonistaLabel.setText(filme.getProtagonista());
classificacaoLabel.setText(filme.getClassificacao());
if (filme.get3D() == true) {
e3dLabel.setText("Sim");
} else {
e3dLabel.setText("Não");
}
}
public void setCategoria (String nome) {
categoriaLabel.setText(nome);
}
@FXML
void handleCancelarIngresso(ActionEvent event) {
dialogStage.close();
}
@FXML
void handleGerarIngresso(ActionEvent event) {
}
}
错误StackTrace:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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$MethodHandler.invoke(FXMLLoader.java:1771)
... 52 more
Caused by: java.lang.NullPointerException
at projeto.resources.EditarFilmeController.setFilme(EditarFilmeController.java:65)
at projeto.MainApp.showEditarFilmeDialog(MainApp.java:105)
at projeto.resources.FilmeOverviewController.handleEditarFilme(FilmeOverviewController.java:145)
... 62 more