Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Pop-Up Window to Ask for Data

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!

like image 633
Philip McQuitty Avatar asked Jun 22 '11 21:06

Philip McQuitty


People also ask

How do you ask user input data in Java?

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();


2 Answers

Use JOptionPane.showInputDialog().

You can find a nice tutorial at: http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html#input

like image 122
Marcelo Avatar answered Sep 19 '22 12:09

Marcelo


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.

like image 26
G__ Avatar answered Sep 18 '22 12:09

G__