Reputation: 1532
I have an abstract entity:
public abstract class Entity extends JPanel implements FocusListener
And I have a TextEntity:
public class TextEntity extends Entity
Inside TextEntity's constructor I want to put a JTextArea that will cover the panel:
textArea = new JTextArea();
textArea.setSize(getWidth(),getHeight());
add(textArea);
But getWidth()
and getHeight()
returns 0. Is it a problem with the inheritance or the constructor?
Upvotes: 0
Views: 2037
Reputation: 1050
Try using some LayoutManager that takes care of resizing the components inside the panel. For example the BorderLayout, and add the textarea to the center.
Something like this (it's been a few years since I coded Swing):
textArea = new JTextArea();
textArea.setSize(getWidth(),getHeight());
setLayout(new BorderLayout());
add(textArea, BorderLayout.CENTER);
Now, when you make the panel visible, the layout manager should take care of keeping the textarea the same size as the panel. Also make sure you don't have any borders in the panel.
Upvotes: 1
Reputation: 3343
Depending on the layout, you would need to set the preferred/min/max size of at the embedded components in order for pack to calculate the actual sizes.
Upvotes: 0
Reputation: 1010
Shuldn't be an inheritance problem. Probably in the constructor the JPanel doesn't still have a size.
Upvotes: 3