How to hide or deactivate a TextField and a Label with javafx

37,987

Solution 1

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.

Solution 2

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.

Solution 3

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);
}
}

Solution 4

You can use:

myTextField.setDisable(true);

It will disable field for a particular Action.

Solution 5

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);
Share:
37,987
David
Author by

David

Student in engineering in embedded electronic in France. Not very good at programming but still learning.

Updated on April 13, 2020

Comments

  • David
    David about 4 years

    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

  • David
    David over 8 years
    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 : stackoverflow.com/questions/32369702/…
  • Seiran
    Seiran over 8 years
    I believe the problem is that your TextField not created dynamically. Try creating it in the runtime.
  • David
    David over 8 years
    How should I do that ? Never use dinamic Textfield it's my first javafx application.
  • David
    David over 8 years
    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 ?
  • Seiran
    Seiran over 8 years
    Sure, HBox is like JPanel only it's aligning the objects horizontally
  • David
    David over 8 years
    When does that new window is going to appear ? Because I don't understand when is it called.
  • Seiran
    Seiran over 8 years
    Can you be more specific? What window?
  • David
    David over 8 years
    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.
  • Seiran
    Seiran over 8 years