Search code examples
javaswingjtextarea

Text area not showing up


I am trying to add a simple empty field to my program, in
which a user can put in his name. Trying this, no text area is visible at all.

JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setSize(800, 600);
JTextArea enterName1 = new JTextArea(50, 50);
enterName1.setRows(20);
enterName1.setColumns(1);
panel.add(enterName1, BorderLayout.CENTER);
enterName1.setVisible(true);
panel.setVisible(true);

What am I doing wrong? Thanks in advance for any help.


Solution

  • You just need to add your panel to a jframe and make sure it's visible!

    Main

    public static void main(String[] args) throws ParseException {
    
        MyView view = new MyView();
    
    }
    

    View

    public class MyView extends JFrame {
    
        public MyView() {
           JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
            panel.setSize(800, 600);
            JTextArea enterName1 = new JTextArea(50, 50);
            enterName1.setRows(20);
            enterName1.setColumns(1);
            panel.add(enterName1, BorderLayout.CENTER);
            enterName1.setVisible(true);
            panel.setVisible(true);
            this.add(panel);
            this.setVisible(true);
            this.setSize(800, 600);
        }
    }
    

    Output (N.b. I wrote "Hello world!!")

    enter image description here