I would like to hide or deactivate a TextField
and its Label
in my JavaFX application.
This is what I tried
myTextField.setVisible(false);
, but it's not working
I use Eclipse V4.5.0 on windows 7 with jfx8 V2.0.0
I would like to hide or deactivate a TextField
and its Label
in my JavaFX application.
This is what I tried
myTextField.setVisible(false);
, but it's not working
I use Eclipse V4.5.0 on windows 7 with jfx8 V2.0.0
There is difference between hiding and deactivating a TextField in JavaFX.
To Hide :- You need to set the visible property to false.
The possible reason's why its not working in your case is that if you have skipped mentioning the fx:id for your TextField or Label.
To do so just go through the fxml if using and set the fx:id="myTextField" , and then the same code that you have write will start to work.
The same is used to hide any Label.
To Deactivate :- There is a field named as disable just set that disable property to true to disable or deactivate any field.
I understand that you want to hide/show a text field (javaFX), i usually use the same method you mentioned assume that the text field variable name is field then:
to hide it use
field.setVisible(false);
to show it use
field.setVisible(true);
and it works always for me.
I am not sure that I understand correctly your question, but I will try to answer to what I understand.
if you only want to deactivate TextField
you can use:
myTextField.setEditable(false);
This will not "hide" the TextField
it will be only not editable.
Based on your provided code, the problem might be in static created (in the FXML) TextField
. What I suggest is to try and create the Pane
and the TextField
dynamically in the runtime.
Here is simple example how to create and use JavaFX components in the runtime :
public class ButtonInPane extends Application{
// Override the start method in the Application class
Button btn=new Button("OK");
HBox cont;
TextField myTextField;
public void start(Stage stage1){
Label myLable=new Label("Some Lable");
myTextField=new TextField("Some text");
cont=new HBox();
cont.getChildren().addAll(myLable,myTextField);
Stage stage = new Stage(); //this instead of JFrame
FlowPane pane2 = new FlowPane(); //this instead of JPanel
pane2.getChildren().addAll(btn,cont);
Scene scene2 = new Scene(pane2, 250, 50);
stage.setTitle("Button in a FlowPane"); // Set the stage title
stage.setScene(scene2); // Place the scene in the stage
stage.show(); // Display the stage
stage.setAlwaysOnTop(true);
//set event
setEventActions();
}
private void handlePlayAction() {
cont.setVisible(false);
//OR myTextField.setVisible(false);
}
private void setEventActions() {
this.btn.setOnAction(event -> this.handlePlayAction());
}
public static void main(String[] args)
{ launch(args);
}
}
TextField
not created dynamically. Try creating it in the runtime. –
Wolff HBox
is like JPanel
only it's aligning the objects horizontally –
Wolff You can use:
myTextField.setDisable(true);
It will disable field for a particular Action.
You can also do this for a Label in your .fxml
file like this:
<Label layoutX="349.0" layoutY="85.0"
text="Label" visible="false" fx:id="actionSuccessLabel"/>
and then display it later in your Controller class like this:
actionSuccessLabel.setVisible(true);
Don't forget to do the setVisible or the visible property binding after the new function.
TextField textField = new TextField(field.getValue());
textField.visibleProperty().bind(field.getVisibleProperty());
© 2022 - 2024 — McMap. All rights reserved.
TextField
to be also collapsed while hidden, use:myTextField.setManaged(false);
– Repp