Java popup for requesting data

What code would I use to ask the user to enter their class in the popup?

When JButton is clicked, I want the box to pop up, and prompt the user to enter their rating. Also, is it possible to get the value of the entered double value?

Thanks for all your time. I appreciate it!

+6
source share
3 answers

You need a JOptionPane. Use something like the following code snippet inside the JButton 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 can parse / confirm the double value from the output string. You can also use various swing components - this example is a scrollable text area.

+3
source

The easiest way is to use JoptionPane.showInputDialog(...) . However, keep in mind that it will work if someone tries to enter anything other than double.

 @Override public void actionPerformed(ActionEvent actionEvent) { double someNumber = Double.parseDouble( JOptionPane.showInputDialog(this, "Type in grade:")); } 
+3
source

Source: https://habr.com/ru/post/891164/


All Articles