Search code examples
javaswingjtextareajoptionpane

Input from JOptionPane, output in JTextArea in JFrame?


How do you make a JTextArea in the JFrame that accepts multiple input from JOptionPane? Is that even possible? Thanks to whoever helps!


Solution

    1. Create a new class and extend JFrame
    2. add a JTextArea to it. Make it a member variable
    3. add a button to the frame. In the action method call open the input dialog
    4. when the dialog returns, append the text to the JTextArea using its append method (don't forget to check for empty/null string)

    Here is a sample program:

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    
    public class InputTest extends JFrame {
    
        private final JTextArea textarea;
        private final JButton button;
    
        public InputTest(String title) {
            super(title);
    
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            textarea = new JTextArea(5,30);
    
            button = new JButton("new input");
            button.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    String input = JOptionPane.showInputDialog(InputTest.this, "Please enter some text");
    
                    if((input != null) && !input.isEmpty()) {
                        textarea.append(input);
                        textarea.append(System.getProperty("line.separator"));
                    }                
                }
            });
    
            JPanel p = new JPanel(new BorderLayout());
            p.add(textarea, BorderLayout.NORTH);
            p.add(button, BorderLayout.SOUTH);
    
    
            this.getContentPane().add(p);
    
        }
    
        public static void main(String[] args) {
            InputTest it = new InputTest("Input Test");
            it.setSize(200, 200);
            it.setVisible(true);
        }
    
    }