Hallo, ich beschäftige mich seit kurzen mit JavaFX und möchte eine kleine Anwendung erstellen, welche ganz simpel Aufgaben notiert. Oben befindet sich eine Menüleiste und in der Mitte des Fensters eine Tabelle mit jeweils einer Aufgabe in einer Zeile. Wenn man nun in der Menüleiste auf "new" oder "delete" klickt soll die entsprechende Aktion in der Tabelle durchgeführt werden. In SWING hätte ich mir eine GuiManager Klasse und eine controllerklasse für die Aufgabenliste gemacht und diese im Konstruktor übergeben, damit ich auf die Attribute zugreifen und Änderungen vornehmen kann. Beim initialisieren etwas in die Tabelle zu bekommen ist kein Problem. Meine Frage ist nun: Wie kann ich über eine Aktion aus der Menübar auf meine Liste mit Aufgaben zugreifen (löschen, erstelle, editieren etc.)? Ich habe im Controller der TableView versucht statische Methoden zu erstellen aber dadurch sind andere Probleme entstanden und ich glaube, dass ist auch keine vernünftige Lösung. Außerdem konnte ich so nicht auf den "selectedIndex" zugreifen. Ich dachte mir, dass beim Klick auf ein Feld der Menübar, der MenübarController eine Methode aus dem TableViewController aufruft aber das scheint nicht zu funktionieren weil er ihn nicht kennt und ich weiß nicht genau wie ich die Verknüpfung herstellen soll da der Controller der xml in der xml zugewiesen wird.
Hier ist mein Code:
MainApp.java:
RootLayout.fxml
TaskView.fxml
RootController.java
TaskController.java
Hier ist mein Code:
MainApp.java:
Java:
package task_reminder;
import java.io.IOException;
public class MainApp extends Application {
private BorderPane rootLayout;
private Stage primaryStage;
@Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
primaryStage.setTitle("Task Reminder");
initRootLayout();
showTaskTable();
}
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("/view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
scene.getStylesheets().add("/css/tableStyle.css");
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showTaskTable() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("/view/TaskView.fxml"));
GridPane taskOverview = (GridPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(taskOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
RootLayout.fxml
Java:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="control.RootController">
<top><MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="load list of tasks" fx:id="load"/>
</items>
<items>
<MenuItem mnemonicParsing="false" text="export as..." fx:id="export"/>
</items>
<items>
<MenuItem mnemonicParsing="false" text="Close" fx:id="close" onAction="#closeApplication"/>
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit" fx:id="edit">
<items>
<MenuItem mnemonicParsing="false" text="New" fx:id="new" onAction="#addTask"/>
</items>
<items>
<MenuItem mnemonicParsing="false" text="Delete" fx:id="delete"/>
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help" fx:id="help">
<items>
<MenuItem mnemonicParsing="false" text="?" fx:id="item_help" onAction="#showHelp"/>
</items>
<items>
<MenuItem mnemonicParsing="false" text="About" fx:id="info" onAction="#showInfo"/>
</items>
</Menu>
</menus>
</MenuBar>
</top></BorderPane>
TaskView.fxml
Java:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.GridPane?>
<GridPane alignment="CENTER" hgap="5.0" vgap="10.0" xmlns:fx="http://javafx.com/fxml" xmlns="http://javafx.com/javafx/8" fx:controller="control.TaskController">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
<Label style="-fx-font: NORMAL 20 Tahoma;" text="Tasks" GridPane.columnIndex="0" GridPane.rowIndex="0" >
</Label>
<TableView fx:id="tableView" GridPane.columnIndex="0" GridPane.rowIndex="1" style="-fx-background-color: 'DBDBDB'">
<columns>
<TableColumn text="Name" prefWidth="200" fx:id="col_name">
</TableColumn>
<TableColumn text="Due Date" prefWidth="150" fx:id="col_dueDate">
</TableColumn>
<TableColumn text="Prio" prefWidth="100" fx:id="col_prio">
</TableColumn>
</columns>
</TableView>
</GridPane>
RootController.java
Java:
package control;
import java.net.URL;
public class RootController{
@FXML
protected void showInfo(){
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("about us");
alert.setHeaderText("Task Reminder");
alert.setContentText("\u00a9 Testname, \ncontact: [email]test@gmx.de[/email]");
alert.initModality(Modality.APPLICATION_MODAL);
alert.showAndWait();
}
@FXML protected void showHelp() {
}
@FXML protected void closeApplication() {
}
@FXML protected void addTask() {
}
}
TaskController.java
Java:
package control;
import java.net.URL;
public class TaskController implements Initializable {
@FXML
TableView<Task> tableView;
@FXML
TableColumn col_name;
@FXML
TableColumn col_dueDate;
@FXML
TableColumn col_prio;
ObservableList<Task> data;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// Set up the table data
col_name.setCellValueFactory(
new PropertyValueFactory<Task,String>("name")
);
col_dueDate.setCellValueFactory(
new PropertyValueFactory<Task,String>("dueDate")
);
col_prio.setCellValueFactory(
new PropertyValueFactory<Task,String>("prio")
);
data = FXCollections.observableArrayList();
tableView.setItems(data);
Task t = new Task("Einkaufen","20.03.2015","mittel");
data.add(t);
Task r = new Task("Aufräumen","04.04.2015","hoch");
data.add(r);
tableView.setItems(data);
}
protected void addTask() {
//???
}
protected void deleteTask() {
//???
}
}