Jacob
Jacob

Reputation: 268

JTextArea not appearing in JFrame - Java

I'm creating a simple GUI consisting of a full-window (J)TextArea. I've created a JFrame window and a JTextArea text area and set up both. Additionally I created some colors and the font I want to use with the text area.

Upon running the class, the window pops up as expected, however the text area is not there.

I have set the text area to visible, so that can't be a problem.

Code:

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class GUI {

    public static void main(String[] args) {

        //create and set up window
        JFrame window = new JFrame("Console");

        window.setSize(800, 500);
        window.setVisible(true);
        window.setLocationRelativeTo(null);

        //create fonts and colors
        Color gray = new Color(34, 34, 34);
        Color lightGray = new Color(207, 191, 173);
        Font consolas = new Font("Consolas", Font.PLAIN, 15);

        //create and set up text area
        JTextArea text = new JTextArea();

        text.setSize(800, 500);
        text.setVisible(true);

        //text area font and colors
        text.setBackground(gray);
        text.setForeground(lightGray);
        text.setFont(consolas);

        text.append("Text");

    }   
}

And what results is a blank window named 'Console'.

How do I fix the JTextArea so that it shows?

Upvotes: 1

Views: 848

Answers (1)

pL4Gu33
pL4Gu33

Reputation: 2085

You should add your JTextArea to the JFrame with the add- Method window.add(text);

Another point make the frame visible after adding. window.setVisible(true); in the last line. Because sometimes there are strange layouterrors.

Upvotes: 3

Related Questions