New Line in Textfield

I am trying to have some pre-defined text in my TextField, but I don't know how to add new lines in between the words. I tried \n, but it isn't working.

Code:

    package view;

import javafx.scene.text.Text;

public class Overview extends VBox{

    private TextField info;

    public Overview() {

        //Adds a little border around the app so the text field doesn't go to the edges
        this.setPadding(new Insets(30, 30, 30, 30));

        //----- Initialising text field --------
        info = new TextField("Name:\n" + "PNumber:\n"+ "Email:\n"+ "Date:\n"+ "Course:\n"+ ""+ "Selected Modules:\n"+ "==============");
        info.setAlignment(Pos.TOP_LEFT);
        info.setMinHeight(850);
        info.setEditable(false);


        //------ Adding the text to the Text Field --------


        //-------- Putting it into VBox so that it stretches with the screen ----------
        VBox fit = new VBox(info);




        this.getChildren().add(fit);

    }

}

Does anyone know how to add a newline between each word? I want there to be a separation between words and then it will automatically fill in the information later.

3

2 Answers

TextField is only for single lines, it will not render multiple lines as you want your code to achieve. so instead of using TextField use TextArea.

try to use the textarea instead of textfield. You can use the mulitple lines in textarea but textfield is a single line control

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest