FXML Set ButtonType onAction - javafx

I'm building a DialogPane in FXML and I'm trying to figure out how to respond to button presses on the dialog since onAction is not a valid parameter for ButtonType. I've attached my FXML and Controller Class. There is very little documentation about DialogPane and even less about doing it in FXML so I'm not sure how to proceed.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<DialogPane fx:id="loginPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LoginController">
<content>
<GridPane hgap="5.0" vgap="5.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="100.0" prefWidth="300.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Driver Name" />
<TextField fx:id="driverTxt" GridPane.columnIndex="1" />
<Label text="URL" GridPane.rowIndex="1" />
<TextField fx:id="urlTxt" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Username" GridPane.rowIndex="2" />
<TextField fx:id="userTxt" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="Password" GridPane.rowIndex="3" />
<TextField fx:id="passTxt" GridPane.columnIndex="1" GridPane.rowIndex="3" />
</children>
</GridPane>
</content>
<buttonTypes>
<ButtonType fx:id="loginButton" text="Login" />
<ButtonType fx:id="cancelBtn" text="Cancel" />
</buttonTypes>
</DialogPane>
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextField;
public class LoginController {
#FXML
DialogPane loginPane;
#FXML
TextField driverTxt;
#FXML
TextField urlTxt;
#FXML
TextField userTxt;
#FXML
TextField passTxt;
#FXML
ButtonType loginButton;
#FXML
private void loginButtonAction(){
// How do I do something here
}
public void initialize() {
driverTxt.setText("org.postgresql.Driver");
urlTxt.setText("jdbc:postgresql://localhost/postgres");
userTxt.setText("postgres");
passTxt.setText("postgres");
}
}

Typically you wouldn't need to do this. You would usually show a DialogPane in a Dialog<ButtonType> calling its showAndWait() method, which returns an Optional<ButtonType> representing the button pressed (if any). So normal usage would be something like
public class LoginController {
public static final ButtonType LOGIN = new ButtonType("Login");
#FXML
DialogPane loginPane;
#FXML
TextField driverTxt;
#FXML
TextField urlTxt;
#FXML
TextField userTxt;
#FXML
TextField passTxt;
public void initialize() {
driverTxt.setText("org.postgresql.Driver");
urlTxt.setText("jdbc:postgresql://localhost/postgres");
userTxt.setText("postgres");
passTxt.setText("postgres");
}
public String getDriver() {
return driverTxt.getText();
}
public String getUrl() {
return urlTxt.getText();
}
public String getUser() {
return userTxt.getText();
}
public String getPass() {
return pass.getText();
}
}
and make the following changes to your FXML file:
<buttonTypes>
<LoginController fx:constant="LOGIN" />
<ButtonType fx:constant="CANCEL" />
</buttonTypes>
Then you would use this with:
Dialog<ButtonType> dialog = new Dialog<>();
FXMLLoader dialogLoader = new FXMLLoader(getClass().getResource("Login.fxml"));
dialog.setDialogPane(dialogLoader.load());
LoginController controller = dialogLoader.getController();
dialog.showAndWait().filter(LoginController.LOGIN::equals)
.ifPresent(button -> {
String driver = controller.getDriver();
// etc etc
// process login...
});
As an alternative to exposing the text from the text fields, you could define a processLogin() method in the controller itself that read the text fields and did whatever you need to do:
public class LoginController {
public static final ButtonType LOGIN = new ButtonType("Login");
#FXML
DialogPane loginPane;
#FXML
TextField driverTxt;
#FXML
TextField urlTxt;
#FXML
TextField userTxt;
#FXML
TextField passTxt;
public void initialize() {
driverTxt.setText("org.postgresql.Driver");
urlTxt.setText("jdbc:postgresql://localhost/postgres");
userTxt.setText("postgres");
passTxt.setText("postgres");
}
public void processLogin() {
String driver = driverTxt.getText();
// etc...
// process login...
}
}
then just do
// ...
dialog.showAndWait().filter(LoginController.LOGIN::equals)
.ifPresent(button -> controller.processLogin());
If you really need to register an onAction handler with the login button, do it in the initialize() method in the controller:
public void initialize() {
driverTxt.setText("org.postgresql.Driver");
urlTxt.setText("jdbc:postgresql://localhost/postgres");
userTxt.setText("postgres");
passTxt.setText("postgres");
Button login = (Button) loginPane.lookupButton(loginButton);
login.setOnAction(e -> { /* ... */ });
}
but this is really against the intended use of the dialog pane API.
One final alternative would be to override the createButton method in DialogPane. To do this, you'd need a subclass of DialogPane, which would mean using the FXML custom component pattern.
So this would look something like:
public class LoginPane extends DialogPane {
public static final ButtonType LOGIN = new ButtonType("Login");
#FXML
TextField driverTxt;
#FXML
TextField urlTxt;
#FXML
TextField userTxt;
#FXML
TextField passTxt;
public LoginPane() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("LoginPane.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
} catch (IOException exc) {
// bad if you get here...
throw new UncheckedIOException(exc);
}
}
#Override
public Node createButton(ButtonType buttonType) {
Node button = super.createButton(buttonType);
if (buttonType == LOGIN) {
((Button) button).setOnAction(e -> processLogin());
}
return button ;
}
public void initialize() {
driverTxt.setText("org.postgresql.Driver");
urlTxt.setText("jdbc:postgresql://localhost/postgres");
userTxt.setText("postgres");
passTxt.setText("postgres");
}
public void processLogin() {
String driver = driverTxt.getText();
// etc...
// process login...
}
}
and the FXML would then look like
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ButtonType?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<fx:root type="DialogPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1">
<content>
<GridPane hgap="5.0" vgap="5.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="100.0" prefWidth="300.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Driver Name" />
<TextField fx:id="driverTxt" GridPane.columnIndex="1" />
<Label text="URL" GridPane.rowIndex="1" />
<TextField fx:id="urlTxt" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="Username" GridPane.rowIndex="2" />
<TextField fx:id="userTxt" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="Password" GridPane.rowIndex="3" />
<TextField fx:id="passTxt" GridPane.columnIndex="1" GridPane.rowIndex="3" />
</children>
</GridPane>
</content>
<buttonTypes>
<LoginPane fx:constant="LOGIN" />
<ButtonType fx:constant="CANCEL" />
</buttonTypes>
</fx:root>
You would use this version with
Dialog dialog = new Dialog();
dialog.setDialogPane(new LoginPane());
dialog.showAndWait();
so if you are looking to encapsulate as much as possible into the login pane and fxml, this is probably the cleanest option.
Note that the usage of DialogPane is fairly completely documented in the API docs for Dialog and DialogPane.

Related

Is there a concept of growth priority in javafx?

How does JavaFX renderer decides which component grows when pref sizes sum of 2 components is lower than the overall scene size ?
Is there any way to decide which component should grow first and how more than the other component as flexbox does in regular css ?
here is a css code that describes what i want to do in my javafx app :
.my-container {
display: flex;
width: 1400px;
}
.my-component-1 {
/*pref width is 800*/
flex: 2;
}
.my-component-2 {
/*pref width is 300*/
flex: 1;
}
for example, in the above case, component 1 will grow up to 1000px and component 2 up to 400px.
what is the javafx equivalent to the flex property ?
Here is a small example how a GridPane layout with "Percent Width" works:
Preview:
Controller Class:
package sample;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
public class WidthController implements Initializable {
#FXML
private Pane
redPane,
bluePane;
#FXML
private Label
stageWidthLabel,
redPaneWidthLabel,
bluePaneWidthLabel;
private DoubleProperty
stageWidth = new SimpleDoubleProperty(),
redPaneWidth = new SimpleDoubleProperty(),
bluePaneWidth = new SimpleDoubleProperty();
#Override
public void initialize(URL location, ResourceBundle resources) {
stageWidthLabel.textProperty().bind(stageWidth.asString());
redPaneWidthLabel.textProperty().bind(redPaneWidth.asString());
bluePaneWidthLabel.textProperty().bind(bluePaneWidth.asString());
redPaneWidth.bind(redPane.widthProperty());
bluePaneWidth.bind(bluePane.widthProperty());
}
public void setStage(Stage stage) {
stageWidth.bind(stage.widthProperty());
}
}
FXML File:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane prefHeight="400.0" prefWidth="1100.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.WidthController">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" percentWidth="72.73" />
<ColumnConstraints hgrow="SOMETIMES" percentWidth="27.27" />
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints vgrow="NEVER" />
</rowConstraints>
<children>
<Pane fx:id="bluePane" style="-fx-background-color: blue;" />
<Pane fx:id="redPane" style="-fx-background-color: red;" GridPane.columnIndex="1" />
<GridPane hgap="3.0" vgap="3.0" GridPane.columnSpan="2" GridPane.rowIndex="1">
<columnConstraints>
<ColumnConstraints fillWidth="false" halignment="RIGHT" hgrow="NEVER" />
<ColumnConstraints fillWidth="false" halignment="LEFT" hgrow="NEVER" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints fillHeight="false" valignment="CENTER" vgrow="NEVER" />
<RowConstraints fillHeight="false" valignment="CENTER" vgrow="NEVER" />
<RowConstraints fillHeight="false" valignment="CENTER" vgrow="NEVER" />
<RowConstraints fillHeight="false" valignment="CENTER" vgrow="NEVER" />
</rowConstraints>
<children>
<Label text="Width information" underline="true" />
<Label text="Stage:" GridPane.rowIndex="1" />
<Label text="Blue Pane:" GridPane.rowIndex="2" />
<Label text="Red Pane:" GridPane.rowIndex="3" />
<Label fx:id="stageWidthLabel" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label fx:id="bluePaneWidthLabel" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label fx:id="redPaneWidthLabel" GridPane.columnIndex="1" GridPane.rowIndex="3" />
</children>
<padding>
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
</padding>
</GridPane>
</children>
</GridPane>
Application Class:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("width.fxml"));
Parent root = null;
try {
root = loader.load();
} catch (IOException ex) {
ex.printStackTrace();
}
if (root == null) return;
WidthController controller = loader.getController();
primaryStage.setScene(new Scene(root));
primaryStage.show();
controller.setStage(primaryStage);
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX how I Can Get value form TableView to textfield

Someone help me, How I can pass a value from TableView(TableView show data from database) to textField and then the same value save to database??
Regards
ps I can Show the source code;
I think I m not able to explain it, I show u piece of code
this is my 1 st fxml file where I take data from user:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<HBox 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="mojprogram.elkosz.controllers.AddCashRegisterController">
<children>
<GridPane prefHeight="398.0" prefWidth="624.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="193.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="449.0" minWidth="10.0" prefWidth="407.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Model Kasy Fiskalnej" GridPane.rowIndex="1" />
<Label text="Numer Seryjny" GridPane.rowIndex="2" />
<Label text="Data Zakupu" GridPane.rowIndex="3" />
<Label text="Data Pierwszego Przeglądu" GridPane.rowIndex="4" />
<Button fx:id="choisebutton" mnemonicParsing="false" onAction="#choiceCompany" text="Firma" />
<Label text="Data Następnego Przeglądu" GridPane.rowIndex="5" />
<TextField fx:id="companytextfieldCH" GridPane.columnIndex="1" />
<TextField fx:id="cashMachinetextfield" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="serialnumber" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<DatePicker fx:id="buydate" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<DatePicker fx:id="firstcheck" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<DatePicker fx:id="nextchech" GridPane.columnIndex="1" GridPane.rowIndex="5" />
<Button fx:id="addmachine" mnemonicParsing="false" onAction="#addCashRegister" text="Dodaj" GridPane.columnIndex="1" GridPane.rowIndex="6" />
</children>
</GridPane>
</children>
</HBox>
This is Controller:
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
public class AddCashRegisterController {
#FXML
private Button addRegister;
#FXML
private TextField cashMachinetextfield;
#FXML
private TextField serialnumber;
#FXML
private DatePicker buydate;
#FXML
private DatePicker firstcheck;
#FXML
private DatePicker nextcheck;
#FXML
private TextField companytextfieldCH;
private CashRegisterModel cashRegisterModel;
public void choiceCompany() {
Stage stage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/ChoiceCompany.fxml"));
try {
Parent parent = loader.load();
ChoiceCompanyController controller = loader.getController();
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.UNDECORATED);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
#FXML
private void initialize(){
this.cashRegisterModel = new CashRegisterModel();
}
public void addCashRegister() {
}
}
This is fxml file with Tableview
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="choiceCompany" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="487.0" prefWidth="743.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mojprogram.elkosz.controllers.ChoiceCompanyController">
<children>
<TableView fx:id="CompanyTableView" onMouseClicked="#choice" prefHeight="446.0" prefWidth="743.0">
<columns>
<TableColumn fx:id="companycolumn" prefWidth="120.0" text="Nazwa Firmy" />
<TableColumn fx:id="NIPcolumn" prefWidth="116.0" text="NIP" />
<TableColumn fx:id="citycolumn" minWidth="0.0" prefWidth="115.0" text="Miejscowość" />
<TableColumn fx:id="streetcolum" minWidth="0.0" prefWidth="113.0" text="Ulica" />
<TableColumn fx:id="contactcolumn" prefWidth="124.0" text="Osoba Kontaktowa" />
<TableColumn fx:id="phonecolumn" prefWidth="153.0" text="Numer Telefonu" />
</columns>
</TableView>
<HBox alignment="CENTER_RIGHT" prefHeight="100.0" prefWidth="200.0">
<children>
<Button fx:id="closeButton" mnemonicParsing="false" onAction="#closeButtonaction" prefHeight="25.0" prefWidth="75.0" text="Ok" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<VBox.margin>
<Insets right="35.0" />
</VBox.margin>
</HBox>
</children>
</VBox>
This is controller of this
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import mojprogram.elkosz.modelFX.CompanyFx;
import mojprogram.elkosz.modelFX.CompanyListModel;
import org.omg.CORBA.portable.ApplicationException;
import java.awt.*;
public class ChoiceCompanyController {
#FXML
public Button closeButton;
public VBox getChoiceCompany() {
return choiceCompany;
}
#FXML
private VBox choiceCompany;
#FXML
private TableView<CompanyFx> CompanyTableView;
#FXML
private TableColumn<CompanyFx, String> companycolumn;
#FXML
private TableColumn<CompanyFx, String> NIPcolumn;
#FXML
private TableColumn<CompanyFx, String> citycolumn;
#FXML
private TableColumn<CompanyFx, String> streetcolum;
#FXML
private TableColumn<CompanyFx, String> contactcolumn;
#FXML
private TableColumn<CompanyFx, String> phonecolumn;
private CompanyListModel companyListModel;
#FXML
private void initialize(){
this.companyListModel = new CompanyListModel();
try {
this.companyListModel.iniati();
} catch (ApplicationException e) {
e.printStackTrace();
}
this.CompanyTableView.setItems(this.companyListModel.getCompanyFxObservableList());
this.companycolumn.setCellValueFactory(cellData -> cellData.getValue().companyNameFxProperty());
this.NIPcolumn.setCellValueFactory(cellData -> cellData.getValue().NIPfxProperty());
this.citycolumn.setCellValueFactory(cellData -> cellData.getValue().cityfxProperty());
this.streetcolum.setCellValueFactory(cellData -> cellData.getValue().streetfxProperty());
this.contactcolumn.setCellValueFactory(cellData -> cellData.getValue().contactpersonfxProperty());
phonecolumn.setCellValueFactory(cellData -> cellData.getValue().phonenumberfxProperty());
}
#FXML
public void choice() {
//CompanyTableView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
CompanyFx selected = CompanyTableView.getSelectionModel().getSelectedItem();
System.out.print(selected.toString());
}
#FXML
public void closeButtonaction() {
Stage stage = (Stage) closeButton.getScene().getWindow();
stage.close();
}
}
From this tableview I try to pass value to textfield and then I want to save it in new table of database
I don't understant what you need exactly ,but i think that you desire to get value from row of TableView and pass value of cell to TextField, you can do this if you acces to rows and get values from cells one by one.
PersonView.setRowFactory(event -> {
TableRow<Person> row = new TableRow<>();
row.setOnMouseEntered(event -> {
try {
if (row.isSelected) {
if (row.getItem().getId() != null && row.getItem().getName() != null) {
String Id=row.getItem.getId();
String Name=row.getItem.getId();
IdTextField.setText(Id);
NameTextField.setText(Name);
}
});
return row;
});
I hope this code can help you

JavaFX blur underneath area from pane

Is there any way to add a blur effect only to a part of an image defined by the area of a Pane? If you blur the pane over the image only the pane get blurred, but is there a way to blur the picture part underneath?
Here's an example of what I mean: It's simply just an image overlayed with a pane which has backgrouud and opacity set. Now I want to blur the part the pane lays on.
Main.java:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
GridPane root = (GridPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,500,500);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView fitHeight="495.0" fitWidth="535.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER">
<image>
<Image url="#../../../../Downloads/ZGEX2eX.jpg" />
</image>
</ImageView>
<Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #012E56; -fx-opacity: 0.666;" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
</children>
</GridPane>
You can use the Clip of an ImageView to apply an effect only to a specific area of your image:
public class BlurredImageTest extends Application {
private DoubleProperty effectHeight = new SimpleDoubleProperty(80);
#Override
public void start(Stage primaryStage) {
Image image = new Image("yourImage.jpg");
ImageView img = new ImageView(image);
Rectangle clip = new Rectangle();
clip.widthProperty().bind(image.widthProperty());
clip.heightProperty().bind(image.heightProperty().subtract(effectHeight));
img.setClip(clip);
ImageView imgEffect = new ImageView(image);
Rectangle clipEffect = new Rectangle();
clipEffect.widthProperty().bind(image.widthProperty());
clipEffect.heightProperty().bind(effectHeight);
clipEffect.translateYProperty().bind(image.heightProperty().subtract(effectHeight));
imgEffect.setClip(clipEffect);
imgEffect.setEffect(new GaussianBlur());
StackPane root = new StackPane(img, imgEffect);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public final void setEffectHeight(double value) {
effectHeight.set(value);
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX: How can another tab(containing a textarea) be added when a new user is added?

In this simple JavaFX application, when a new user is added, text that says "New User added" is printed into a text area in the first tab. How can an additional tab be added and text "New User added" printed into a text area in it each time a new user is added?
Any help would be greatly appreciated.
View > PersonOverview.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<HBox xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.makery.address.PersonOverviewController">
<children>
<TableView fx:id="personTable" prefHeight="298.0" prefWidth="246.0">
<columns>
<TableColumn fx:id="firstNameColumn" prefWidth="75.0" text="First Name" />
<TableColumn fx:id="lastNameColumn" prefWidth="75.0" text="Last Name" />
</columns>
</TableView>
<VBox prefHeight="298.0" prefWidth="271.0">
<children>
<AnchorPane prefHeight="284.0" prefWidth="227.0">
<children>
<Label layoutX="9.0" layoutY="4.0" prefHeight="19.0" prefWidth="96.0" text="Person Details" />
<GridPane layoutX="108.0" layoutY="121.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="30.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="First Name" />
<Label text="Last Name" GridPane.rowIndex="1" />
<Label text="Street" GridPane.rowIndex="2" />
<Label text="City" GridPane.rowIndex="3" />
<Label text="Postal Code" GridPane.rowIndex="4" />
<Label text="Birthday" GridPane.rowIndex="5" />
<Label fx:id="firstNameLabel" text="Label" GridPane.columnIndex="1" />
<Label fx:id="lastNameLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label fx:id="streetLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label fx:id="cityLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label fx:id="postalCodeLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4" />
<Label fx:id="birthdayLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5" />
</children>
</GridPane>
<ButtonBar buttonMinWidth="50.0" layoutX="14.0" layoutY="244.0" prefHeight="40.0" prefWidth="200.0">
<buttons>
<Button mnemonicParsing="false" onAction="#handleNewPerson" text="New" />
<Button mnemonicParsing="false" onAction="#handleEditPerson" text="Edit" />
<Button mnemonicParsing="false" onAction="#handleDeletePerson" text="Delete" />
</buttons>
</ButtonBar>
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</AnchorPane>
</children>
</VBox>
<TabPane prefHeight="296.0" prefWidth="337.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab>
<content>
<TextArea fx:id="textArea" prefHeight="264.0" prefWidth="302.0" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</HBox>
View > PersonOverviewController
package ch.makery.address;
import ch.makery.address.util.DateUtil;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import ch.makery.address.MainApp;
import ch.makery.address.model.Person;
public class PersonOverviewController {
#FXML
private TableView<Person> personTable;
#FXML
private TableColumn<Person, String> firstNameColumn;
#FXML
private TableColumn<Person, 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 TextArea textArea;
// Reference to the main application.
private MainApp mainApp;
/**
* The constructor.
* The constructor is called before the initialize() method.
*/
public PersonOverviewController() {
}
/**
* 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.
showPersonDetails(null);
// Listen for selection changes and show the person details when changed.
personTable.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showPersonDetails(newValue));
}
/**
* Is called by the main application to give a reference back to itself.
*
* #param mainApp
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
// Add observable list data to the table
personTable.setItems(mainApp.getPersonData());
}
/**
* 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 showPersonDetails(Person person) {
if (person != null) {
// Fill the labels with info from the person object.
firstNameLabel.setText(person.getFirstName());
lastNameLabel.setText(person.getLastName());
streetLabel.setText(person.getStreet());
postalCodeLabel.setText(Integer.toString(person.getPostalCode()));
cityLabel.setText(person.getCity());
birthdayLabel.setText(DateUtil.format(person.getBirthday()));
// birthdayLabel.setText(...);
} else {
// Person is null, remove all the text.
firstNameLabel.setText("");
lastNameLabel.setText("");
streetLabel.setText("");
postalCodeLabel.setText("");
cityLabel.setText("");
birthdayLabel.setText("");
}
}
public void print(String message) {
textArea.appendText(message);
}
/**
* Called when the user clicks on the delete button.
*/
#FXML
private void handleDeletePerson() {
int selectedIndex = personTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
personTable.getItems().remove(selectedIndex);
} else {
// Nothing selected.
Alert alert = new Alert(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() {
Person tempPerson = new Person();
boolean okClicked = mainApp.showPersonEditDialog(tempPerson);
if (okClicked) {
mainApp.getPersonData().add(tempPerson);
print("New User added");
}
}
/**
* Called when the user clicks the edit button. Opens a dialog to edit
* details for the selected person.
*/
#FXML
private void handleEditPerson() {
Person selectedPerson = personTable.getSelectionModel().getSelectedItem();
if (selectedPerson != null) {
boolean okClicked = mainApp.showPersonEditDialog(selectedPerson);
if (okClicked) {
showPersonDetails(selectedPerson);
}
} else {
// Nothing selected.
Alert alert = new Alert(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();
}
}
}
In you're handleNewPerson method, you would have to add something like:
Tab tab = new Tab();
tab.setText("TabText");
TextArea ta = new TextArea("New User added");
tab.setContent(ta);
tabPan.getTabs().add(tab);
And add add:
#FXML
private TabPane tabPan;
And in the fxml add
fx:id="tabPan"
To the TabPane
Now I have not been able to test your program due to your program requires a lot of the other classes. But you can also see a good example of somthing similar her.

ListView built with SceneBuilder8 does not show inserted list

I am attempting to create a GUI for the front end of a program that will be used in my office. I have been tasked to use Scene Builder 8 to help create said GUI. This is my first project using JavaFX and Scene Builder so I've had to learn everything from scratch. My problem is that the while the code isn't throwing me any errors, I cannot see the sample data that I've put into the program. It's making me question the program as a whole.
If some of the following code looks superfluous to you, it may be because development for the backend part of the program is happening at the same time by a different person.
Here is the Mainapp:
package simit.gui;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import simit.gui.launch.model.LocalLaunch;
import simit.gui.launch.model.TextAreaOutputStream;
import simit.gui.launch.view.CompleteList;
import simit.gui.launch.view.LocalLaunchController;
import simit.gui.launch.view.MonitorController;
public class Main extends Application {
//Hold onto the main state
Stage primaryStage;
private ObservableList<simit.gui.launch.view.CompleteList> listData = FXCollections.observableArrayList();
public Main(){
listData.add(new CompleteList("sample1"));
listData.add(new CompleteList("sample2"));
}
public ObservableList<CompleteList> getListData(){
return listData;
}
//Run simulation
public void runSimulation(File inputDeck, int procs, int mem){
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("launch/view/Monitor.fxml"));
AnchorPane rootLayout = (AnchorPane) loader.load();
//Get the controller
MonitorController controller = loader.getController();
//Setup the view
List<TextAreaOutputStream> textAreas = controller.SetUpTextAreas(procs);
//Try to launch the code
LocalLaunch launch = new LocalLaunch(inputDeck, procs, mem, textAreas);
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
// try {
// BorderPane root = new BorderPane();
// Scene scene = new Scene(root,400,400);
// scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
// primaryStage.setScene(scene);
// primaryStage.show();
// } catch(Exception e) {
// e.printStackTrace();
// }
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("launch/view/LocalLaunch.fxml"));
AnchorPane rootLayout = (AnchorPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
//Load up the LocalLaunch Controller
LocalLaunchController controller = loader.getController();
controller.SetMain(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Here is the FXML Controller:
package simit.gui.launch.view;
import java.io.File;
import org.controlsfx.control.CheckListView;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import simit.gui.Main;
import simit.gui.launch.model.LocalLaunch;
public class LocalLaunchController {
//Hold onto the required text fields
#FXML
private TextField path;
#FXML
private TextField procs;
#FXML
private TextField mem;
final ObservableList<CompleteList> items = FXCollections.observableArrayList();
#FXML
final public CheckListView<CompleteList> listItems = new CheckListView<CompleteList>();
private Main mainProgram;
public void SetMain(Main mainProgram){
this.mainProgram = mainProgram;
items.addAll(mainProgram.getListData());
listItems.setItems(items);
}
#FXML
private void handleSelect() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("SMT File", "*"+LocalLaunch.PaserExt),
new ExtensionFilter("Resume File", "*"+LocalLaunch.RsmExp),
new ExtensionFilter("All Files", "*.*"));
File selectedFile = fileChooser.showOpenDialog(null);
if (selectedFile != null) {
path.setText(selectedFile.getAbsolutePath());
}
}
#FXML
private void handleRun() {
//Make sure input is valid
if(!isInputValid())
return;
//Get the data
File tmpFile = new File(path.getText());
int proc = Integer.parseInt(procs.getText());
int mem = Integer.parseInt(this.mem.getText());
mainProgram.runSimulation(tmpFile, proc, mem);
}
/**
* Validates the user input in the text fields.
*
* #return true if the input is valid
*/
private boolean isInputValid() {
String errorMessage = "";
if(path.getText().isEmpty()){
errorMessage += "The input deck cannot be empty!\n";
}
//Create a test file
File testFile = new File(path.getText());
if(!testFile.exists())
errorMessage += "The input deck must exisit!\n";
if (procs.getText() == null || procs.getText().length() == 0) {
errorMessage += "No valid procs specified!\n";
} else {
// try to parse the postal code into an int.
try {
int p =Integer.parseInt(procs.getText());
if(p <= 0)
throw new NumberFormatException();
} catch (NumberFormatException e) {
errorMessage += "No valid procs specified (must be a positive integer)!\n";
}
}
if (mem.getText() == null || mem.getText().length() == 0) {
errorMessage += "No valid mem specified!\n";
} else {
// try to parse the postal code into an int.
try {
int p =Integer.parseInt(mem.getText());
if(p <= 0)
throw new NumberFormatException();
} catch (NumberFormatException e) {
errorMessage += "No valid mem specified (must be a positive integer)!\n";
}
}
if (errorMessage.length() == 0) {
return true;
} else {
// Show the error message.
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Invalid Fields");
alert.setHeaderText("Please correct invalid fields");
alert.setContentText(errorMessage);
alert.showAndWait();
return false;
}
}
#FXML
private void initialize() {
}
}
Here is a class I've made for the input variable:
package simit.gui.launch.view;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class CompleteList {
private final StringProperty listDetails;
public CompleteList(){
this(null);
}
public CompleteList(String listDetails){
this.listDetails = new SimpleStringProperty(listDetails);
}
public String getListDetails() {
return listDetails.get();
}
public void setListDetails(String listDetails) {
this.listDetails.set(listDetails);
}
public StringProperty listDetailsProperty(){
return listDetails;
}
}
And finally here is the FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import org.controlsfx.control.*?>
<AnchorPane prefHeight="600.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="simit.gui.launch.view.LocalLaunchController">
<children>
<SplitPane dividerPositions="0.5" layoutX="120.0" layoutY="41.0" orientation="VERTICAL" prefHeight="600.0" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<ImageView fitWidth="600.0" pickOnBounds="true" preserveRatio="true" smooth="false">
<image>
<Image url="#../../resources/SIMITBlastLogo.png" />
</image>
<cursor>
<Cursor fx:constant="DEFAULT" />
</cursor>
</ImageView>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<GridPane layoutX="50.0" layoutY="58.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints fillWidth="false" hgrow="SOMETIMES" minWidth="10.0" prefWidth="20.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints fillWidth="false" hgrow="SOMETIMES" minWidth="10.0" prefWidth="0.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Input Deck">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin></Label>
<Label text="Processors" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin></Label>
<TextField fx:id="procs" text="1" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="path" editable="false" GridPane.columnIndex="1" />
<Button alignment="CENTER_RIGHT" mnemonicParsing="false" onAction="#handleSelect" text="Select" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<GridPane.margin>
<Insets />
</GridPane.margin>
</Button>
<Label text="Mem/Proc" GridPane.rowIndex="2">
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin></Label>
<Label text="GB" GridPane.columnIndex="2" GridPane.rowIndex="2" />
<TextField fx:id="mem" promptText="1" text="1" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
</GridPane>
<ButtonBar layoutX="129.0" layoutY="248.0" prefHeight="40.0" prefWidth="200.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0">
<buttons>
<Button defaultButton="true" mnemonicParsing="false" onAction="#handleRun" text="Run Simulation" />
</buttons>
</ButtonBar>
<CheckListView fx:id="listItems" layoutX="200.0" layoutY="117.0" prefHeight="200.0" prefWidth="200.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
Here is what I'm seeing when I run the program (minus the image on top, I've gotten rid of it for a little anonymity):
Image of the program when ran.
What I need to see is list items populating that white square in the middle.
Any help anyone can give me at all would be greatly appreciated.

Resources