Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using GUI's JFrame cancels the Scanner class?

I created a JFrame window with some buttons and I want to make it when a user click a specific button a method will be launched and the method will use a scanner object to pick up information from the user.

For some reason after I click that button from the frame, the method launches but I am not able to type any info.

Is there a way to use JFrame and Scanner class at the same application?

like image 837
Davis8988 Avatar asked Oct 01 '22 00:10

Davis8988


1 Answers

Yes, there is definitely a way to use both of them at the same time. You just cannot use the Scanner to take input from the command line.

A program in Java should either be a GUI application or a Console application, but not both. Once you bring up a JFrame, your code should not be reading from console. What you should do is adding input fields to your GUI (e.g. a JTextField) and read a String from it. You can pass the String that you read to a Scanner to subdivide it into tokens. Alternatively, you could make multiple text input fields, and take your input that way.

like image 98
Sergey Kalinichenko Avatar answered Oct 03 '22 12:10

Sergey Kalinichenko