-1

adding new category within the same window

I am making a new form using javafx to add new item to an inventory, if I need to add new category into the Item form, I must be able to do within the frame itself by popping up a new text field as shown in the red box. Is there any way to do the same in JavaFX?

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
Nas
  • 3
  • 3
  • 4
    Welcome to SO. Please read [How to ask a good question](https://stackoverflow.com/help/how-to-ask) – bated Aug 18 '17 at 15:35
  • You can take a look at [this](https://stackoverflow.com/questions/44173811/how-to-make-a-javafx-label-selectable/44182371#44182371) for ideas. – SedJ601 Aug 18 '17 at 19:49

1 Answers1

0

Two different approaches. You could use a TextField and setEditable(false). Then make the TextField's setEditable(true) after a double-click on the TextFeild. The first half of the code shows this approach. Another option is to use a Label. When the Label is double-clicked, hide the Label and show a TextField or TextArea. Type your data into one of these nodes and when enter is pressed, remove the TextField or TextArea and show the text in your Label. The second half of the code show this approach.

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication7 extends Application {

    @Override
    public void start(Stage primaryStage) {        
        VBox root = new VBox();
        StackPane stackPane = new StackPane();

        TextField textField = new TextField("Text");
        textField.setEditable(false);//Set Editiable to false

        textField.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
                    if(mouseEvent.getClickCount() == 2){
                        textField.setEditable(true);//On double click set editable to true

                        textField.setOnKeyPressed(event ->{
                            if(event.getCode().toString().equals("ENTER"))
                            {
                                textField.setEditable(false);//On enter set editable to false
                            }
                        });
                    }
                }
            }
        });

        Label label = new Label("Test");
        VBox.setVgrow(label, Priority.ALWAYS);
        label.wrapTextProperty().set(true);

        label.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
                    if(mouseEvent.getClickCount() == 2){
                        label.setVisible(false);
                        TextArea textarea = new TextArea(label.getText());
                        textarea.setPrefHeight(label.getHeight() + 10);
                        stackPane.getChildren().add(textarea);

                        textarea.setOnKeyPressed(event ->{
                            System.out.println(event.getCode());
                            if(event.getCode().toString().equals("ENTER"))
                            {
                                label.setText(textarea.getText());
                                stackPane.getChildren().remove(textarea);
                                label.setVisible(true);                               
                            }
                        });
                    }
                }
            }
        });

        stackPane.getChildren().add(label);   

        root.getChildren().add(textField);
        root.getChildren().add(stackPane);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }       

}
SedJ601
  • 12,173
  • 3
  • 41
  • 59