How to hide or deactivate a TextField and a Label with javafx
Asked Answered
C

6

7

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

Cormier answered 8/9, 2015 at 7:43 Comment(7)
if you want your TextField to be also collapsed while hidden, use: myTextField.setManaged(false);Repp
I actually don't know how to hide it so...Cormier
What's your Java/JavaFX version and on which platform are you ?Repp
I'm working with Eclipse and Jfx8Cormier
Could you edit your post with the exact version of Java, IDE and OS you are on ? Also, edit your post with what exactly happens instead of it's not working. A screenshot of the situation may also be useful.Repp
Actually nothing happened when I launch it my Textfield still visible.Cormier
check this post.. it maybe of some help #38069855Fluoresce
R
7

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.

Reynaud answered 24/5, 2016 at 13:3 Comment(0)
S
6

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.

Saline answered 16/5, 2016 at 13:11 Comment(0)
W
4

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);
}
}
Wolff answered 8/9, 2015 at 8:21 Comment(9)
I've post something 4 days ago that were very clear and no answer so ... For your answer the first part is a good thing I should try it. And I don't understand the second part about Hbox ? If you want to understand better what i want I invite you to read that post : #32370202Cormier
I believe the problem is that your TextField not created dynamically. Try creating it in the runtime.Wolff
How should I do that ? Never use dinamic Textfield it's my first javafx application.Cormier
Here is what I understand about this code tell me if I'm wrong : It open a new window with TextFields and label instead of hiding the others ? (It's a good idea I thought it was harder than create and hide the Label and TextField). Could you explain me what is the Hbox ?Cormier
Sure, HBox is like JPanel only it's aligning the objects horizontallyWolff
When does that new window is going to appear ? Because I don't understand when is it called.Cormier
Can you be more specific? What window?Wolff
The code you gave me open a new window with a Label and a TextField or I'm wrong ? Because I don't really understand it to be honnest.Cormier
Let us continue this discussion in chat.Wolff
S
3

You can use:

myTextField.setDisable(true);

It will disable field for a particular Action.

Spatula answered 12/4, 2018 at 12:25 Comment(0)
C
2

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);
Clive answered 18/8, 2016 at 19:55 Comment(0)
H
1

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());
Huntingdonshire answered 25/8, 2017 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.