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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With