Witam mam problem mam napisać na zaliczenie aplikację "desktopową" ale mam mały problem. Otóż mam już pomysł na aplikację ale coś nie działa i nie jest w stanie powiedzieć dlaczego. Na moje oko wszystko gra ale dla eclipse nie.
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
public class Main_tescik extends Application {
private static Stage primaryStage = null;
private ObservableList<Employee> employeeData = FXCollections.observableArrayList();
@Override
public void start(Stage primaryStage) {
try {
Main_tescik.primaryStage = primaryStage;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main_tescik.class.getResource("RootLayout.fxml"));
BorderPane rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static Stage getPrimaryStage() {
return primaryStage;
}
public ObservableList<Employee> getEmployeeData() {
return employeeData;
}
public boolean showEmployeeEditDialog(Employee person) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main_tescik.class.getResource("EmployeeEditDialog.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Edit Employee");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
EmployeeEditDialogController controller = loader.getController();
controller.setDialogStage(dialogStage);
controller.setEmployee(person);
dialogStage.showAndWait();
return controller.isOkClicked();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
Application.launch(args);
}
}
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import application.Main_tescik;
import application.Employee;
public class EmployeeOverviewController {
@FXML
private TableView<Employee> personTable;
@FXML
private TableColumn<Employee, String> firstNameColumn;
@FXML
private TableColumn<Employee, String> lastNameColumn;
@FXML
private Label firstNameLabel;
@FXML
private Label lastNameLabel;
@FXML
private Label streetLabel;
@FXML
private Label postalCodeLabel;
@FXML
private Label cityLabel;
@FXML
private Label birthdayLabel;
@FXML
private Label selectionLabel;
@FXML
private Button newButton;
@FXML
private Button editButton;
@FXML
private Button deleteButton;
@FXML
private Label hire_dateLabel;
@FXML
private Label dictionaryLabel;
// Reference to the main application.
private Main_tescik mainApp;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public EmployeeOverviewController() {
}
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
@FXML
private void initialize() {
// Initialize the person table with the two columns.
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
// Clear person details.
showEmployeeDetails(null);
// Listen for selection changes and show the person details when changed.
personTable.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showEmployeeDetails(newValue));
deleteButton.setOnAction(this::handleDeletePerson);
newButton.setOnAction(this::handleNewPerson);
editButton.setOnAction(this::handleEditPerson);
}
/**
* Is called by the main application to give a reference back to itself.
*
* @param mainApp
*/
public void setMainApp(Main_tescik mainApp) {
this.mainApp = mainApp;
// Add observable list data to the table
personTable.setItems(mainApp.getEmployeeData());
}
/**
* Fills all text fields to show details about the person.
* If the specified person is null, all text fields are cleared.
*
* @param person the person or null
*/
private void showEmployeeDetails(Employee person) {
if (person != null) {
// Fill the labels with info from the person object.
firstNameLabel.setText(person.getFirstName());
lastNameLabel.setText(person.getLastName());
selectionLabel.setText(person.getSelection());
streetLabel.setText(person.getStreet());
postalCodeLabel.setText(Integer.toString(person.getPostalCode()));
cityLabel.setText(person.getCity());
birthdayLabel.setText(person.getBirthday().toString());
hire_dateLabel.setText(person.getHire_Date().toString());
} else {
// Person is null, remove all the text.
firstNameLabel.setText("");
lastNameLabel.setText("");
selectionLabel.setText("");
streetLabel.setText("");
postalCodeLabel.setText("");
cityLabel.setText("");
birthdayLabel.setText("");
hire_dateLabel.setText("");
}
}
/**
* Called when the user clicks on the delete button.
*/
@FXML
private void handleDeletePerson(ActionEvent event) {
int selectedIndex = personTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
personTable.getItems().remove(selectedIndex);
} else {
// Nothing selected.
Alert alert = new Alert(AlertType.WARNING);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("No Selection");
alert.setHeaderText("No Person Selected");
alert.setContentText("Please select a person in the table.");
alert.showAndWait();
}
}
/**
* Called when the user clicks the new button. Opens a dialog to edit
* details for a new person.
*/
@FXML
private void handleNewPerson(ActionEvent event) {
Employee tempPerson = new Employee();
boolean okClicked = mainApp.showEmployeeEditDialog(tempPerson);
if (okClicked) {
mainApp.getEmployeeData().add(tempPerson);
}
}
/* @FXML
private void handleNewPerson(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("EmployeeEditDialog.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
} catch (Exception e) {
System.out.println("zle");
}
}*/
@FXML
private void handleEditPerson(ActionEvent event) {
Employee selectedPerson = personTable.getSelectionModel().getSelectedItem();
if (selectedPerson != null) {
boolean okClicked = mainApp.showEmployeeEditDialog(selectedPerson);
if (okClicked) {
showEmployeeDetails(selectedPerson);
}
} else {
// Nothing selected.
Alert alert = new Alert(AlertType.WARNING);
alert.initOwner(mainApp.getPrimaryStage());
alert.setTitle("No Selection");
alert.setHeaderText("No Person Selected");
alert.setContentText("Please select a person in the table.");
alert.showAndWait();
}
}
}
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import application.Employee;
public class EmployeeEditDialogController {
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
@FXML
private TextField streetField;
@FXML
private TextField postalCodeField;
@FXML
private TextField cityField;
@FXML
private TextField selectionField;
@FXML
private DatePicker birthdayField;
@FXML
private Button okButton;
@FXML
private Button cancelButton;
@FXML
private DatePicker hire_dateField;
private Stage dialogStage;
private Employee person;
private boolean okClicked = false;
@FXML
private void initialize() {
okButton.setOnAction(this::handleOk);
cancelButton.setOnAction(this::handleCancel);
}
/**
* Sets the stage of this dialog.
*
* @param dialogStage
*/
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
/**
* Sets the person to be edited in the dialog.
*
* @param person
*/
public void setEmployee(Employee person) {
this.person = person;
firstNameField.setText(person.getFirstName());
lastNameField.setText(person.getLastName());
selectionField.setText(person.getSelection());
streetField.setText(person.getStreet());
postalCodeField.setText(Integer.toString(person.getPostalCode()));
cityField.setText(person.getCity());
birthdayField.setValue(person.getBirthday());
hire_dateField.setValue(person.getHire_Date());
}
/**
* Returns true if the user clicked OK, false otherwise.
*
* @return
*/
public boolean isOkClicked() {
return okClicked;
}
/**
* Called when the user clicks ok.
*/
private void handleOk(ActionEvent event) {
if (isInputValid()) {
person.setFirstName(firstNameField.getText());
person.setLastName(lastNameField.getText());
person.setSelection(selectionField.getText());
person.setStreet(streetField.getText());
person.setPostalCode(Integer.parseInt(postalCodeField.getText()));
person.setCity(cityField.getText());
person.setBirthday(birthdayField.getValue());
person.setHire_Date(hire_dateField.getValue());
okClicked = true;
dialogStage.close();
}
}
/**
* Called when the user clicks cancel.
*/
private void handleCancel(ActionEvent event) {
dialogStage.close();
}
/**
* Validates the user input in the text fields.
*
* @return true if the input is valid
*/
private boolean isInputValid() {
String errorMessage = "";
if (firstNameField.getText() == null || firstNameField.getText().length() == 0) {
errorMessage += "No valid first name!\n";
}
if (lastNameField.getText() == null || lastNameField.getText().length() == 0) {
errorMessage += "No valid last name!\n";
}
if (selectionField.getText() == null || selectionField.getText().length() == 0) {
errorMessage += "No valid last name!\n";
}
if (streetField.getText() == null || streetField.getText().length() == 0) {
errorMessage += "No valid street!\n";
}
if (postalCodeField.getText() == null || postalCodeField.getText().length() == 0) {
errorMessage += "No valid postal code!\n";
} else {
// try to parse the postal code into an int.
try {
Integer.parseInt(postalCodeField.getText());
} catch (NumberFormatException e) {
errorMessage += "No valid postal code (must be an integer)!\n";
}
}
if (cityField.getText() == null || cityField.getText().length() == 0) {
errorMessage += "No valid city!\n";
}
if (birthdayField.getValue() == null ) {
errorMessage += "No valid birthday!\n";
}
if (hire_dateField.getValue() == null ) {
errorMessage += "No valid hide date!\n";
}
if (errorMessage.length() == 0) {
return true;
} else {
// Show the error message.
Alert alert = new Alert(AlertType.ERROR);
alert.initOwner(dialogStage);
alert.setTitle("Invalid Fields");
alert.setHeaderText("Please correct invalid fields");
alert.setContentText(errorMessage);
alert.showAndWait();
return false;
}
}
}
Na razie jest jeden guzik na początku ale w zamyśle ma ich być więcej tylko na razie nie potrafię doprowadzić do działania tego jednego to nie ma sensu robić więcej. Aplikacja ma działać tak że wciskam guzik na RootLayout i przechodzę do EmployeeOverview(do tej pory działa) potem jak kliknę new to otwiera się EmployeeEditDialog i można dodawać dane ale już to n ie działa a ja nie mam pojęcia dlaczego.
Pomocy muszę to zrobić do środy. Tylko bładam bez komentarzy w stylu "tu cos robisz nie optymalnie" albo "to można zrobić lepiej" zależy mi tylko na ty żeby to działało.