Commit 14428d41 authored by Arne Styve's avatar Arne Styve
Browse files

Added an FXML-based GUI side-by-side with the hardcoded GUI. The prototype is...

Added an FXML-based GUI side-by-side with the hardcoded GUI. The prototype is complete, but no functionality yet.
parent 9d6093e0
package no.ntnu.idata2001.contacts.controllers;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;
import no.ntnu.idata2001.contacts.model.ContactDetails;
/**
* The controller class mapped to the FXML main class.
*
* A few words on the @FXML-annotation:
* The @FXML annotation is used to enable initialization of fields performed
* by the FXML-loader. For example, in the code below, there is a field
* for the TableView. The instance to be created, will be created by the FXML loader,
* hence you should NEVER create this instance here in the controller.
*
* Now, if the field (like contactDetailsTableView) is public, you do not need to
* use the annotation @FXML on the field for it to be initialized. BUT if you set it
* to private (which you should, according to good practice), you MUST add the @FXML
* annotation for the field to be initialized by the FXML loader.
* See: https://stackoverflow.com/questions/30210170/is-fxml-needed-for-every-declaration
*/
public class ContactsMain {
@FXML
private TableView contactDetailsTableView;
/**
* This method will be called by the FXML-loader after having loaded
* all the GUI-components and initialized them.
*/
@FXML
private void initialize() {
ObservableList<ContactDetails> observableContactsList = FXCollections.observableArrayList();
observableContactsList.add(new ContactDetails("Arne Styve", "Phone", "Address"));
observableContactsList.add(new ContactDetails("Eskil Styve", "Phone", "Address"));
observableContactsList.add(new ContactDetails("Siw Styve", "Phone", "Address"));
this.contactDetailsTableView.setItems(observableContactsList);
}
public void addContact(ActionEvent actionEvent) {
System.out.println("addContact() was called...");
}
public void importFromCSV(ActionEvent actionEvent) {
System.out.println("importFromCSV() was called...");
}
public void exportToCSV(ActionEvent actionEvent) {
System.out.println("exportToCSV() was called...");
}
public void exitApplication(ActionEvent actionEvent) {
System.out.println("exitApplication() was called...");
}
public void removeContact(ActionEvent actionEvent) {
System.out.println("removeContact() was called...");
}
public void editContact(ActionEvent actionEvent) {
System.out.println("editContact() was called...");
}
public void showAboutDialog(ActionEvent actionEvent) {
System.out.println("showAboutDialog() was called...");
}
public void showLoginDialog(ActionEvent actionEvent) {
System.out.println("showLoginDialog() was called...");
}
}
/*Icons to the menu items.*/
.addContact > .label {
-fx-graphic: url("./icons/add_contact.png");
}
/*Avoid icon on the accelerator text*/
.addContact > .accelerator-text{
-fx-graphic: none;
}
.editContact > .label {
-fx-graphic: url("./icons/edit_contact.png");
}
.editContact > .accelerator-text{
-fx-graphic: none;
}
.removeContact > .label {
-fx-graphic: url("./icons/remove_contact.png");
}
.removeContact > .accelerator-text{
-fx-graphic: none;
}
/*Icons to the button items.*/
.addContactBtn {
-fx-graphic: url("./icons/add_contact.png");
}
.editContactBtn {
-fx-graphic: url("./icons/edit_contact.png");
}
.removeContactBtn {
-fx-graphic: url("./icons/remove_contact.png");
}
\ No newline at end of file
package no.ntnu.idata2001.contacts.views;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ContactsAppFXML extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("ContactsMain.fxml"));
primaryStage.setTitle("Hello World");
Scene scene = new Scene(root, 300, 275);
scene.getStylesheets().add(ContactsAppFXML.class.getResource("ContactsAppFXML.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="no.ntnu.idata2001.contacts.controllers.ContactsMain">
<top>
<VBox>
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" onAction="#importFromCSV" text="Import from .CSV..." />
<MenuItem mnemonicParsing="false" onAction="#exportToCSV" text="Export to .CSV..." />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" onAction="#exitApplication" text="Exit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem accelerator="A" mnemonicParsing="false" onAction="#addContact" styleClass="addContact" text="Add new Contact..." />
<MenuItem accelerator="E" mnemonicParsing="false" onAction="#editContact" styleClass="editContact" text="Edit Selected Contact..." />
<MenuItem accelerator="D" mnemonicParsing="false" onAction="#removeContact" styleClass="removeContact" text="Remove Selected Contact..." />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" onAction="#showAboutDialog" text="About" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Admin">
<items>
<MenuItem mnemonicParsing="false" onAction="#showLoginDialog" text="Log In..." />
</items>
</Menu>
</menus>
</MenuBar>
<ToolBar prefHeight="40.0" prefWidth="200.0">
<items>
<Button mnemonicParsing="false" onAction="#addContact" styleClass="addContactBtn" />
<Button mnemonicParsing="false" onAction="#editContact" styleClass="editContactBtn" />
<Button mnemonicParsing="false" onAction="#removeContact" styleClass="removeContactBtn" />
</items>
</ToolBar>
</children>
</VBox>
</top>
<bottom>
<HBox style="-fx-background-color: #999999;" BorderPane.alignment="CENTER">
<children>
<Text text="Status: OK" />
</children>
</HBox>
</bottom>
<center>
<TableView fx:id="contactDetailsTableView">
<columns>
<TableColumn maxWidth="-1.0" minWidth="200.0" prefWidth="-1.0" text="Name">
<cellValueFactory>
<PropertyValueFactory property="name" />
</cellValueFactory>
</TableColumn>
<TableColumn maxWidth="-1.0" minWidth="200.0" prefWidth="-1.0" text="Address">
<cellValueFactory>
<PropertyValueFactory property="address" />
</cellValueFactory>
</TableColumn>
<TableColumn maxWidth="-1.0" minWidth="200.0" prefWidth="-1.0" text="Phone">
<cellValueFactory>
<PropertyValueFactory property="phone" />
</cellValueFactory>
</TableColumn>
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</center>
</BorderPane>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment