Wojciech Kazior Wojciech Kazior - 1 month ago 21
Java Question

JTextArea added to BorderLayout is not visible

I have four components and I want to add them to the frame which has BorderLayout set. MenuBar goes to the North, JScrollPane goes to the Center, JTextField goes to the South and JTextArea goes to the East. The problem is that the last component is not visible.

setLayout(new BorderLayout());

add(menu, BorderLayout.NORTH);
add(scrollChatPane, BorderLayout.CENTER);
add(myMessage, BorderLayout.SOUTH);
add(users, BorderLayout.EAST);


The code above works as follows:

enter image description here

But when I replace my components with simple buttons, everything works fine:

setLayout(new BorderLayout());

add(new Button("North"), BorderLayout.NORTH);
add(new Button("Center"), BorderLayout.CENTER);
add(new Button("South"), BorderLayout.SOUTH);
add(new Button("West"), BorderLayout.WEST);
add(new Button("East"), BorderLayout.EAST);


The code above works as follows:

enter image description here

Can anybody help? Thanks.

Answer

Your JTextArea has no initial size so its not visible. I assume you want a fixed width and a variable height so I think this is what you want

JTextArea users = new JTextArea();
users.setPreferredSize(new Dimension(100, users.getHeight()));
add(users, BorderLayout.EAST);