Can not create clearable textField using ControlsFX - javafx

I have created TextField using Scene Builder but when I tried it with KeyEvent but it doesn't work.
public class FXMLDocumentController implements Initializable {
#FXML
private TextField searchPatient;
#FXML
void keyTyped(KeyEvent event) {
this.searchPatient = TextFields.createClearableTextField();
}
}
Here is main class:
public class MyFX extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is FXMLDocument.fxml:
<AnchorPane id="AnchorPane" prefHeight="597.0" prefWidth="726.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.homeo.home.FXMLDocumentController">
<children>
<TextField fx:id="searchPatient" layoutX="14.0" layoutY="109.0" onKeyTyped="#keyTyped" prefHeight="26.0" prefWidth="372.0" promptText="Search" />
</children>
</AnchorPane>
I don't want to relay on code for designing that's why I want to use ControlsFX with IDE generated code.
I'm expecting textfield must show clear icon as I enter few text in a textfield that's called clearableTextFieldf.
But with the all code above doesn't work as I expected.
It does not create ClearableTextField

You're creating the TextField in the KEY_TYPED event handler of a conventional TextField placed in your scene. The newly created TextField is never added to any scene though.
You could use the fx:factory attribute to use the static method for creating the TextField that FXMLLoader adds to the scene:
...
<?import org.controlsfx.control.textfield.TextFields?>
<AnchorPane id="AnchorPane" prefHeight="597.0" prefWidth="726.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.homeo.home.FXMLDocumentController">
<children>
<TextFields fx:factory="createClearableTextField" fx:id="searchPatient" layoutX="14.0" layoutY="109.0" prefHeight="26.0" prefWidth="372.0" promptText="Search" />
</children>
</AnchorPane>
You could alternatively use the initialize method to create&add your TextField instead:
public class FXMLDocumentController implements Initializable {
...
#FXML
private AnchorPane anchorPane;
#Override
public void initialize(URL location, Resources resources)
searchPatient = TextFields.createClearableTextField();
searchPatient.setLayoutX(14.0);
searchPatient.setLayoutY(109.0);
searchPatient.setPrefSize(372.0, 26.0);
searchPatient.setPromptText("Search");
anchorPane.getChildren().add(searchPatient);
...
}
}
<AnchorPane fx:id="anchorPane" id="AnchorPane" prefHeight="597.0" prefWidth="726.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.homeo.home.FXMLDocumentController" />

Related

More efficient way to add Panes to an HBox?

I am working on a project that is going to involve adding panes (or hboxes if that works better) to a very wide HBox. I set it up in an fxml pretty easily but I'm already going to have to make a few of these and I would love to avoid all the copying pasting. So I figured I should try and use a for loop to populate the HBox but for whatever reason it is not working. I'm in the very beginning steps so my code is very simple and straight forward.
This is the basic sample I'm trying to recreate
<HBox>
<children>
<Pane fx:id="pane0" prefHeight="200.0" prefWidth="200.0">
<children>
<Button layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
</children></Pane>
<Pane fx:id="pane1" prefHeight="200.0" prefWidth="200.0">
<children>
<Button layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
</children></Pane>
<Pane fx:id="pane2" prefHeight="200.0" prefWidth="200.0">
<children>
<Button layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
</children></Pane>
</children>
</HBox>
So to create it dynamically (and for clarity) I created a class for the HBox and the panes. For now each pane just has a button but they will undergo further customization once this code works. Same with the HBox.
Here's the HBox
public class HBoxTestClass {
#FXML
HBox hBox = new HBox();
public HBoxTestClass(){
}
#FXML
public void initialize(){
populateHBox();
}
private void populateHBox(){
for (int i = 0; i < 3; i++){
hBox.getChildren().add(new TestPane());
hBox.setSpacing(10);
}
}
}
its fxml
<HBox fx:id="hBox" xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="sample.HBoxTestClass"
prefHeight="400.0" prefWidth="600.0">
</HBox>
The pane class and its fxml
public class TestPane extends Pane{
#FXML Pane testPane = new Pane();
#FXML Button button = new Button();
public TestPane(){
}
#FXML
private void initialize(){
button.setText("Click Me!");
}
}
<Pane fx:id="testPane" xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="sample.TestPane"
prefHeight="200.0" prefWidth="200.0">
<children>
<Button fx:id="button" layoutX="71.0" layoutY="95.0" mnemonicParsing="false" text="Button" />
</children>
</Pane>
So my very simple code above just produces a blank, wide screen with no panels in it. I put a console print in the constructor of my TestPane class so I know it's getting called but still nothing appears. Any advice? Thanks
I am not sure what you are doing in your code incorrectly. Code like #FXML Pane testPane = new Pane(); should look like #FXML Pane testPane;. I am not sure how code like #FXML
public void initialize(){ is being called in your program. Try to follow this MCVE.
Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication363 extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Controller
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
/**
*
* #author blj0011
*/
public class FXMLDocumentController implements Initializable
{
#FXML
private HBox hBox;
#Override
public void initialize(URL url, ResourceBundle rb)
{
// TODO
for (int i = 0; i < 5; i++) {
StackPane stackPane = new StackPane(new Label("Label: " + i));
hBox.getChildren().add(stackPane);
}
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<HBox fx:id="hBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication363.FXMLDocumentController" />

How to get a reference to the nodes described via fx:id in javafx?

I have a reduced example of my problem where the code references from fx:id are not null when the initialize is called but then go to null right after the function call. What is the correct way to get such references? This is sample.fxml
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="sample.Main">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<Text fx:id="textRef" strokeType="OUTSIDE" strokeWidth="0.0" text="Hello world" />
</children>
</GridPane>
And this is the Main.java which is declared as its controller.
public class Main extends Application implements Initializable{
#FXML
public Text textRef;
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
this.someNewFunction();
}
private void someNewFunction() {
this.textRef.setText("Changed in somNewFunction");
}
public static void main(String[] args) {
launch(args);
}
#Override
public void initialize(URL location, ResourceBundle resources) {
this.textRef.setText("Changed at initialize");
}
}
The text ref is valid inside the initialize call but throws a nullpointerexception when inside the someNewFunction.
The Main instance that is launched is a different object than the Main instance created by the FXMLLoader to be used as controller.
IMHO it would be better to get the controller from the FXMLLoader after loading the fxml and also use a class different to the Application as controller:
public class MainController implements Initializable {
...
}
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="sample.MainController">
...
</GridPane>
#Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
MainController controller = loader.getController();
controller.someNewFunction();
}
However you could also specify the controller that should be used with the fxml:
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
...
</GridPane>
#Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
loader.setController(this);
Parent root = loader.load();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
this.someNewFunction();
}
You are executing the someNewFunction outside the javafx loop.
I mean, after the show method ends, the main thread takes control and the javafx components no longer exists (they are destroyed as is supposed that they are no longer needed).
You have to bind the someNewFunction to some event attached to some FXML element reference (i.e. a Button via the setOnAction on SceneBuilder, or via code), place the method call inside the initialize method, or simply move the call before the primaryStage.show() line.
Hopes this helps

JavaFX #FXML binding from within Java code

I have a custom JavaFX component extending the Tab class:
public class MyTab extends Tab implements Initializable {
#FXML private TextField myInput;
private final MyDTO dto;
public MyTab(MyDTO dto) {
super();
this.dto = dto;
final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/my-tab.xml"));
fxmlLoader.setResources(MSG.getResourceBundle());
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
}
catch (IOException exception) {
throw new RuntimeException(exception);
}
}
#Override
public void initialize(URL url, ResourceBundle res) {
setText("My Tab");
myInput.setText(dto.getValue()); // !!!
}
}
With the FXML:
<fx:root type="javafx.scene.control.Tab" xmlns:fx="http://javafx.com/fxml">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Label id="myLabel" layoutX="14.0" layoutY="14.0" text="Text:" />
<TextField id="myInput" layoutX="162.0" layoutY="10.0" prefHeight="25.0" prefWidth="300.0" />
</children>
</AnchorPane>
</content>
</fx:root>
I need to create this objects (custom tabs) dynamically from the java code:
final MyTab myTab = new MyTab(new MyDTO(...));
tabPane.getTabs().add(myTab);
When I use it like this, the #FXML binding doesn't work and the line
myInput.setText(dto.getValue());
throws NullPointerException. When the line with the setting of the text from the code is commented, the input is showned, so the problem is only in the binding.
I am using JavaFX 2 for Java 1.7
Thank you for any idea!
Solution is very easy, I just overlooked the mistake in the FXML code:
Should be
<TextField fx:id="myInput" ...
instead of
<TextField id="myInput" ...

How to change the image of Imageview on MouseClick

i am creating an application in javafx using scene builder. i have an imageview and i want to change the image when someone clicks on it. i have added a mouse event to the image view but it's not changing the image when i click on it. please find below the code for the same.
public class HomeScreenController implements Initializable {
#FXML
public ImageView updateproject;
public String updateprojectstyle="-fx-image:url(\"../../../../Images/update-enable.png\");";
}
#FXML
private void updateProject(MouseEvent event) throws IOException
{
System.out.println("hello");
updateproject.setStyle(updateprojectstyle);
}
#Override
public void initialize(URL location, ResourceBundle resources) {
//To change body of generated methods, choose Tools | Templates.
}
}
Updated Question (MCVE)
My FXML looks like:
<VBox prefHeight="658.0" prefWidth="162.0" styleClass="sidepanel" BorderPane.alignment="CENTER">
<children>
<Pane maxHeight="138.0" maxWidth="162.0" prefHeight="200.0" prefWidth="200.0">
<children>
<ImageView fx:id="addproject" fitHeight="60.0" fitWidth="66.0" layoutX="48.0" layoutY="39.0" pickOnBounds="true" preserveRatio="true" styleClass="addproject-img" />
</children>
</Pane>
<Pane maxHeight="138.0" maxWidth="162.0" prefHeight="200.0" prefWidth="200.0">
<children>
<ImageView fx:id="updateproject" fitHeight="57.0" fitWidth="84.0" layoutX="41.0" layoutY="32.0" onMouseClicked="#updateProject" pickOnBounds="true" preserveRatio="true" styleClass="updateproject-img" />
</children>
</Pane>
<Pane layoutX="10.0" layoutY="148.0" maxHeight="138.0" maxWidth="162.0" prefHeight="200.0" prefWidth="200.0">
<children>
<ImageView fx:id="deleteproject" fitHeight="59.0" fitWidth="80.0" layoutX="41.0" layoutY="32.0" pickOnBounds="true" preserveRatio="true" styleClass="deleteproject-img" />
</children>
</Pane>
</children>
</VBox>
My Controller looks like:
public class HomeScreenController implements Initializable {
#FXML
public ImageView updateproject;
#FXML
public ImageView addproject;
#FXML
public ImageView deleteproject;
public String updateprojectstyle="-fx-image:url(\"../../../../Images/update-enable.png\");";
public String addprojectstyle="-fx-image:url(\"../../../../Images/add-disable.png\");";
#FXML
private void updateProject(MouseEvent event) throws IOException
{
System.out.println("hello");
updateproject.setStyle(updateprojectstyle);
addproject.setStyle(addprojectstyle);
}
#Override
public void initialize(URL location, ResourceBundle resources) {
//To change body of generated methods, choose Tools | Templates.
}
}
My css looks like:
.updateproject-img{
-fx-image:url("../../../../Images/update-disable1.png");
}
.addproject-img{
-fx-image:url("../../../../Images/add-active.png");
}
.deleteproject-img{
-fx-image:url("../../../../Images/delete-disable1.png");
}
i want to disable the add project icon and enable the update project icon when the user clicks on update project image view. click event is firing but it's not loading the image.
Here is a code with the css style on the ImageView.
Images on ImageView are set via -fx-image:url(...) and not via -fx-background-image:url(...), as you have used in your code.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class ChangeImageViewImage extends Application {
private static final String IMAGE1 = "http://img1.wikia.nocookie.net/__cb20130120032553/dragonball/images/3/32/Chibi-Ichigo-bleach-anime-33253004-332-400.png";
private static final String IMAGE2 = "https://40.media.tumblr.com/68f246eef26984be8a44e4367cfedd47/tumblr_n3lsszuyj41txcp2ko1_500.jpg";
#Override
public void start(Stage primaryStage) throws Exception {
ImageView imageView = new ImageView();
imageView.setFitHeight(400);
imageView.setFitWidth(300);
imageView.setStyle("-fx-image: url(\""+ IMAGE1 + "\");");
imageView.setOnMouseClicked(event -> {
imageView.setStyle("-fx-image: url(\""+ IMAGE2 + "\");");
});
Pane pane = new Pane();
pane.getChildren().add(imageView);
Scene scene = new Scene(pane, 300, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

javafx : pass parameter to the eventhandler function in fxml file

Is there a way to call the event handler method from the fxml file which holds parameter? please find the files:
My Fxml Looks like:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cognizant.iotworkbench.View.AddImageController">
<children>
<Label layoutX="270.0" layoutY="14.0" text="Add Sensor" />
<BorderPane layoutY="-1.0" prefHeight="400.0" prefWidth="602.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<left>
<HBox fx:id="source" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<ImageView fx:id="sourceimg" fitHeight="114.0" fitWidth="142.0" onDragDetected="#setUpGestureSource" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="file:Images/Project.png" />
</image>
</ImageView>
</children>
</HBox>
</left>
<right>
<HBox fx:id="target" onDragDropped="#setUpGestureTarget" onDragOver="#setUpGestureTarget" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</right>
</BorderPane>
</children>
</AnchorPane>
My controller class looks like
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
/**
* FXML Controller class
*
*/
public class AddImage implements Initializable,ControlledScreen {
ScreensController myController;
/**
* Initializes the controller class.
*/
#FXML
final HBox target=new HBox();
#FXML
final HBox source=new HBox();
#FXML
final ImageView sourceimg=new ImageView();
#FXML
public void setUpGestureSource()
{
System.out.println("source");
source.setOnDragDetected(new EventHandler <MouseEvent>() {
#Override
public void handle(MouseEvent event) {
/* drag was detected, start drag-and-drop gesture*/
System.out.println("onDragDetected");
/* allow MOVE transfer mode */
Dragboard db = source.startDragAndDrop(TransferMode.COPY);
/* put a string on dragboard */
ClipboardContent content = new ClipboardContent();
Image sourceImage = sourceimg.getImage();
content.putImage(sourceImage);
db.setContent(content);
event.consume();
}
});
}
#FXML
public void setUpGestureTarget(){
System.out.println("target");
target.setOnDragOver(new EventHandler <DragEvent>() {
#Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if(db.hasImage()){
event.acceptTransferModes(TransferMode.COPY);
}
event.consume();
}
});
target.setOnDragDropped(new EventHandler <DragEvent>() {
#Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if(db.hasImage()){
insertImage(db.getImage(), target);
event.setDropCompleted(true);
}else{
event.setDropCompleted(false);
}
event.consume();
}
});
}
void insertImage(Image i, HBox hb){
ImageView iv = new ImageView();
iv.setImage(i);
setUpGestureSource();
hb.getChildren().add(iv);
}
#Override
public void setScreenParent(ScreensController screenParent) {
myController=screenParent;
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Drag and Drop event is not getting fired. I dont know what's wrong with the code. Please help
All methods that are annotated with #FXML may or may not have a parameter. If you need a parameter, you can directly pass the specific Event as a parameter to the method.
Let us take an example and say you have an onDragOver event added for a HBox in the fxml.
<HBox fx:id="targetBox" onDragOver="#setupGestureTarget">
...
</HBox>
Then you may have a method which does the needful inside the controller class.
#FXML
public void setupGestureTarget(DragEvent event) {
Dragboard db = event.getDragboard();
if(db.hasImage()){
event.acceptTransferModes(TransferMode.COPY);
}
event.consume();
}
If you need the source node for which the method was called, you can get an access to it by getSource() of the event, then type-caste it to Node.
public void setupGestureTarget(DragEvent event) {
...
((Node)event.getSource()).setId("myHbox");
System.out.println(targetBox.getId()); // Prints - myHbox
}
Note : For using methods specific to HBox, you can type-caste it to a HBox instead of a Node.
EDIT - As per user comments and updates
There are multiple issues with your approach. Let me address them all.
1.Values to the fields annotated with #FXML are injected while the FXML is getting loaded and should not carry new keyword.
#FXML
final HBox target=new HBox();
should be replaced by
#FXML
final HBox target;
2.As I have already stated your method can have a parameter which defines the Event. So the methods setUpGestureSource() and setUpGestureTarget() can be defined as :
#FXML
public void setUpGestureSource(DragEvent event) {
...
}
#FXML
public void setUpGestureTarget(DragEvent event) {
...
}
3.The method setUpGestureSource is called when a drag-event occurs on the ImageView, so you don't need to add another EventHandler for it.
#FXML
public void setUpGestureSource(MouseEvent event) {
/* drag was detected, start drag-and-drop gesture*/
System.out.println("onDragDetected");
* allow MOVE transfer mode */
Dragboard db = source.startDragAndDrop(TransferMode.COPY);
/* put a string on dragboard */
ClipboardContent content = new ClipboardContent();
Image sourceImage = sourceimg.getImage();
content.putImage(sourceImage);
db.setContent(content);
event.consume();
}
Similarly, change the other method as well.

Resources