0

. i am creating simple question asking system in java. so wrote Scanner class How to Call inside the TextArea using Java. what tried so far i attached below. when the form is loaded question should display on the textarea.

public piz() {
    initComponents();
    ques();

    txtarea.setText(ques());
}

public void ques()
{
    Scanner in = new Scanner(System.in);
    System.out.println("Enter customer name: ");
    String name = in.nextLine();


}
mayu kobi
  • 103
  • 9
  • 3
    So you want to turn the text area into a console? That is not a trivial thing to do. When writing a GUI, you should get inputs in a GUI way. Don't try to use the mindset you used to use in writing command line apps. – Sweeper Mar 06 '20 at 07:28
  • Please read this. There is no point in using Scanner with GUI. https://docs.oracle.com/javase/6/docs/api/javax/swing/JTextField.html read this, and check out this question https://stackoverflow.com/questions/5752307/how-to-retrieve-value-from-jtextfield-in-java-swing – bakero98 Mar 06 '20 at 08:53

1 Answers1

0

You can use parameters to pass in a reference to the TextArea

    ques(txtarea);
}

public void ques(JTextArea txtarea)
{
    Scanner in = new Scanner(System.in);
    System.out.println("Enter customer name: ");
    String name = in.nextLine();
    txtarea.setText(name);
}

However, you ideally wouldn't use the console to enter data into a GUI form

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245