Nitin Kumar Singh
Nitin Kumar Singh

Reputation: 57

Java8 - Text doesn't display in a TextArea

This is a small code which should display a textarea and should show what I type. But I don't know why its not displaying the text. When I press and hold any key the horizontal scroll bar moves that means somthing is going in there but nothing is getting displayed. I AM USING JDK 1.8 and edited using notepad on windows 7 64-bit.

public class tarea
{

    public static void main (String[] args)
    {
        Frame f = new Frame("My Frame");
        f.setLocation(100, 100);
        f.setSize(500, 500);    
        f.setLayout(null);
        f.setBackground(Color.BLACK);
        f.setTitle("My area");

        TextArea ta = new TextArea();
        ta.setBounds(10, 10, 400, 400);
        f.add(ta);

        f.setVisible(true);

    }
}

Upvotes: 0

Views: 369

Answers (5)

Priyank
Priyank

Reputation: 37

TextArea class alternatively can be initialized with 2 parameters i.e. width and height

 TextArea ta=new TextArea(50,50);

You should remove setBounds() method and should use

setLayout(new FlowLayout());

INSTEAD OF

setLayout(null);

If you face any more problem related to this let me know.....

Upvotes: 0

Muhammad
Muhammad

Reputation: 7344

By setting the f.setLayout(null) the Frame will use an absolute layout which will start drawing from the top left corner of the Frame means drawing or adding components will be drawn under the title bar. The problem is that when you add your textarea to the Frame it starts from the top left corner of the Frame and the text is hidden under the title bar you should increase the value of y in ta.setBounds(10, 30, 400, 400); from 10 to 30

Upvotes: 0

user1803551
user1803551

Reputation: 13427

If you insist on using AWT for some reason, this is what you probably want to do:

public class tarea {

    public static void main(String[] args) {

        Frame f = new Frame("My Frame");
        f.setLocation(100, 100);
        f.setBackground(Color.BLACK);
        f.setTitle("My area");
        TextArea ta = new TextArea(20, 50);
        f.add(ta);
        f.pack();
        f.setVisible(true);
    }
}

Unless there is a good reason you need null layout for, don't do it. You saw what it leads to.

Upvotes: 0

Mistsz1
Mistsz1

Reputation: 181

It is displaying the text, you just don't see it. The top of the TextArea is just too high to be displayed.

You have to change this line:

ta.setBounds(10, 10, 400, 400);

to this:

ta.setBounds(10, 30, 400, 400);

Changing the Y position will solve the problem.

Upvotes: 5

Chris
Chris

Reputation: 23179

It is being displayed. Those scroll bars are the scroll bars on the textarea, not the frame. Put your cursor in there and try to type... it should accept text.

Consider setting a layout manager if you want better control over how the components are organized within the panel.

Something like:

f.setLayout(new GridLayout(2, 1));
f.add(new Label("This is a text area:"));
f.add(ta);

Upvotes: 0

Related Questions