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

David
  • 184
  • 2
  • 4
  • 20
  • if you want your `TextField` to be also _collapsed_ while hidden, use: `myTextField.setManaged(false);` – Spotted Sep 08 '15 at 11:39
  • I actually don't know how to hide it so... – David Sep 08 '15 at 12:08
  • What's your Java/JavaFX version and on which platform are you ? – Spotted Sep 08 '15 at 12:10
  • I'm working with Eclipse and Jfx8 – David Sep 08 '15 at 12:12
  • 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. – Spotted Sep 08 '15 at 12:16
  • Actually nothing happened when I launch it my Textfield still visible. – David Sep 08 '15 at 12:21
  • check this post.. it maybe of some help http://stackoverflow.com/questions/38069355/how-to-deactivate-a-textfield-and-a-label-by-combobox-selection-in-javafx –  Jun 28 '16 at 08:33

6 Answers6

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.

Chetan Hallan
  • 279
  • 4
  • 15
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.

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);
}
}
Seiran
  • 126
  • 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 : http://stackoverflow.com/questions/32369702/hide-textfields-and-label-on-a-combobox-event-with-javafx – David Sep 08 '15 at 08:36
  • I believe the problem is that your `TextField` not created dynamically. Try creating it in the runtime. – Seiran Sep 08 '15 at 08:52
  • How should I do that ? Never use dinamic Textfield it's my first javafx application. – David Sep 08 '15 at 09:19
  • 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 ? – David Sep 08 '15 at 11:00
  • Sure, `HBox` is like `JPanel` only it's aligning the objects horizontally – Seiran Sep 08 '15 at 11:07
  • When does that new window is going to appear ? Because I don't understand when is it called. – David Sep 08 '15 at 11:38
  • Can you be more specific? What window? – Seiran Sep 08 '15 at 11:41
  • 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. – David Sep 08 '15 at 11:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89061/discussion-between-seiran-and-david). – Seiran Sep 08 '15 at 11:55
3

You can use:

myTextField.setDisable(true);

It will disable field for a particular Action.

Niloy Quazi
  • 107
  • 2
  • 12
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);
Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
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());