我在主窗口中的ListView
上每个打开的按钮事件中的对象都充满了另一个问题。
我尝试了几种方法,但是除非我在ListView
所在的实际窗口中对每个按钮事件进行操作,否则它似乎永远无法工作。我想知道是否与在主窗口中使用控制器实例有关,但是我是JavaFX的新手,我真的不知道自己在做什么,tbh。
一些其他信息:
信息是一个存储信息的类,我需要在多个控制器类中访问该信息;
gastConrim
是在另一个打开的窗口(AddEditGastController
)中调用的方法;
最后一部分是可以工作的部分(在ListView
所在的主窗口中,根据按钮事件进行更新),与“ update ListView”按钮类似,但是如果您必须进行更新,这有点不方便手动ListView
。
public abstract class Infos {
public static String ID;
static Hotelsystem ht = new Hotelsystem();
public static Hotelsystem getHt(){
return ht;
}
static void alert(String titel, String content) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(titel);
alert.setHeaderText(null);
alert.setContentText(content);
alert.show();
}
static boolean idAbfragen(String titel, String content, String alertContent) {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle(titel);
dialog.setHeaderText(null);
dialog.setContentText(content);
Optional<String> input = dialog.showAndWait();
if (!input.isPresent() || input.get().trim().isEmpty()) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(titel);
alert.setHeaderText(null);
alert.setContentText(alertContent);
alert.showAndWait();
return false;
}
else {
ID = input.get();
return true;
}
}
static ObservableList<Gast> gastData = FXCollections.observableArrayList();
public static void updateGast() {
gastData.setAll(ht.getGastList());
}
}
@FXML
void gastConfirm(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader= new FXMLLoader(getClass().getResource("../gui/mainWindowGast.fxml"));
Parent root = (Parent) fxmlLoader.load();
MWControllerGast controller = (MWControllerGast) fxmlLoader.getController();
double rabatt = 0.0;
if (gastNameInput.getText().trim().isEmpty() || anredeToggle.getSelectedToggle() == null || gastGB.getValue() == null || gastTfInput.getText().trim().isEmpty() ||
gastStrasseInput.getText().trim().isEmpty() || gastStadtInput.getText().trim().isEmpty() || gastLandInput.getText().trim().isEmpty()) {
Infos.alert("Fehler", "Es fehlen Informationen.");
}
else {
if (gastStatus.isSelected()) {
if (!gastRabattInput.getText().trim().isEmpty()) {
String rabattInput = gastRabattInput.getText();
if (rabattInput.contains(","))
rabattInput.replace(",", ".");
rabatt = Double.parseDouble(rabattInput);
Infos.getHt().getHinzufuegen().vipHinzufuegen(gastNameInput.getText(), gastAnredeRadio1.getText(), gastGB.getValue(), gastTfInput.getText(), gastStrasseInput.getText(), gastStadtInput.getText(), gastLandInput.getText(), rabatt);
}
Infos.ht.getHinzufuegen().vipHinzufuegen(gastNameInput.getText(), gastAnredeRadio1.getText(), gastGB.getValue(), gastTfInput.getText(), gastStrasseInput.getText(), gastStadtInput.getText(), gastLandInput.getText(), rabatt);
} else
Infos.ht.getHinzufuegen().gastHinzufügen(gastNameInput.getText(), gastAnredeRadio1.getText(), gastGB.getValue(), gastTfInput.getText(), gastStrasseInput.getText(), gastStadtInput.getText(), gastLandInput.getText());
System.out.println(rabatt);
gastNameInput.clear();
gastAnredeRadio1.setSelected(false);
gastAnredeRadio2.setSelected(false);
gastGB.setValue(null);
gastTfInput.clear();
gastStrasseInput.clear();
gastStadtInput.clear();
gastLandInput.clear();
gastStatus.setSelected(false);
gastRabattInput.clear();
Infos.updateGast();
controller.gastTable.getItems().setAll(Infos.gastData);
}
@FXML
ListView<Gast> gastTable;
public ListView<Gast> getGastTable() {
return gastTable;
}
@FXML
void sortieren(ActionEvent event) {
Infos.updateGast();
gastTable.getItems().setAll(Infos.gastData);
}
预期结果:每当我添加新对象时,都更新ListView
中的gastConfirm
。
实际结果:什么也没有发生。在控制台或用户界面中至少看不到任何东西。只是不会将对象添加到ListView
。
答案 0 :(得分:0)
您需要查看James D答案here。我创建了James答案的精简版本。代码中的注释。
主要
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication124 extends Application
{
@Override
public void start(Stage primaryStage)
{
try
{
//Load both Stages while passing the model to each.
FXMLLoader listLoader = new FXMLLoader(getClass().getResource("ListFXML.fxml"));
Scene scene = new Scene(listLoader.load());
ListController listController = listLoader.getController();
primaryStage.setScene(scene);
primaryStage.setTitle("Hello World!");
FXMLLoader editorLoader = new FXMLLoader(getClass().getResource("EditorFXML.fxml"));
Stage secondStage = new Stage();
secondStage.setScene(new Scene(editorLoader.load()));
EditorController editorController = editorLoader.getController();
DataModel model = new DataModel();
listController.initModel(model);
editorController.initModel(model);
primaryStage.show();
secondStage.show();
}
catch (IOException ex)
{
System.out.println(ex.toString());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
列表控制器
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
/**
* FXML Controller class
*
* @author blj0011
*/
public class ListController implements Initializable {
@FXML ListView<TestObject> listView;
DataModel model;
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
//Use to set model
public void initModel(DataModel model) {
if (this.model != null) {
throw new IllegalStateException("Model can only be initialized once");
}
this.model = model ;
//Set ListView cell factory.
listView.setCellFactory((ListView<TestObject> param) -> {
ListCell<TestObject> cell = new ListCell<TestObject>() {
@Override
protected void updateItem(TestObject item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getName());
} else {
setText("");
}
}
};
return cell;
});
listView.setItems(model.getTestObjectsList());//Set List items
}
}
列出FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="498.0" prefWidth="755.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="javaapplication12.ListController">
<children>
<ListView fx:id="listView" layoutX="234.0" layoutY="53.0" prefHeight="393.0" prefWidth="288.0" />
</children>
</AnchorPane>
编辑器控制器
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
/**
* FXML Controller class
*
* @author blj0011
*/
public class EditorController implements Initializable {
@FXML Button btn;
@FXML TextField tf;
DataModel model;
/**
* Initializes the controller class.
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
//use to set model
public void initModel(DataModel model) {
if (this.model != null) {
throw new IllegalStateException("Model can only be initialized once");
}
this.model = model ;
//set button action
btn.setOnAction((event) -> {
TestObject testObject = new TestObject(tf.getText().isBlank() ? "Dummy Object" : tf.getText());
model.getTestObjectsList().add(testObject);
});
}
}
编辑器FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="javaapplication12.EditorController">
<children>
<TextField fx:id="tf" layoutX="226.0" layoutY="94.0" />
<Button fx:id="btn" layoutX="274.0" layoutY="175.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>
型号
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
/**
*
* @author blj0011
*/
public class DataModel {
private final ObservableList<TestObject> testObjects = FXCollections.observableArrayList();
public ObservableList<TestObject> getTestObjectsList() {
return testObjects ;
}
}
TestObject
/**
*
* @author blj0011
*/
class TestObject {
private String name;
public TestObject(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "TestObject{" + "name=" + name + '}';
}
}
答案 1 :(得分:-2)
我不明白的是为什么要加载控制器并且不进一步使用它...
FXMLLoader fxmlLoader= new FXMLLoader(getClass().getResource("../gui/mainWindowGast.fxml"));
Parent root = (Parent) fxmlLoader.load();
MWControllerGast controller = (MWControllerGast) fxmlLoader.getController();
您加载了新的Controller,但是您既未使用该Controller也未使用所加载的根目录在任何地方实际显示View。不知道您是否在其他地方执行此操作(实际上您不能因为它是一个局部变量),因为我看不到您的完整代码,但显然那里有些错误。 我缺少类似的东西:
someParent.add(root);