What code would I use to ask a user to enter their grade into a pop-up window?
When a JButton is pressed, I want a little box to pop-up and prompt the user to enter their grade. Furthermore, would it be possible to get the value of the entered double value?
Thanks for all your time. I appreciate it!
You can get user input like this using a BufferedReader: InputStreamReader inp = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(inp); // you will need to import these things. String name = br. readline();
Use JOptionPane.showInputDialog().
You can find a nice tutorial at: http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
You want a JOptionPane. Use something like the following code snippet inside the JButton's ActionListener:
JTextArea textArea = new JTextArea();
textArea.setEditable(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.requestFocus();
textArea.requestFocusInWindow();
scrollPane.setPreferredSize(new Dimension(800, 600));
JOptionPane.showMessageDialog(
(ControlWindow) App.controller.control, scrollPane,
"Paste Info", JOptionPane.PLAIN_MESSAGE);
String info = textArea.getText();
You could parse/validate the double value from the output string. You could also use different swing components - this example is a scrollable text area.
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