sumu00
sumu00

Reputation: 49

JTextArea.setText not visible

I am trying to use JTextArea.setText in java to put me something up to the window. I wanted to get my screensize into textarea, however, one of two .setText() is not showing anything to the screen.

My code:

public class SimpleFrame {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Demo");
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        double hi = d.getHeight();
        double wi = d.getWidth();
        JTextArea area = new JTextArea(10, 10);
        area.setEditable(false);
        area.setText("height: " + hi);
        area.setText("width: " + wi);
        frame.setSize(400, 400);
        frame.add(area); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }
}

Output:

width: 1920.0

Upvotes: 0

Views: 545

Answers (1)

pzaenger
pzaenger

Reputation: 11973

You need to append your text with area.append("..."); since area.setText("..."); overrides the content.

public void append(String str): Appends the given text to the end of the document.

public void setText(String t): Sets the text of this TextComponent to the specified text.

Upvotes: 4

Related Questions