I' ve this simple JPanel subclass:
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SearchTextField extends JPanel
{
private ImageIcon image;
private JTextField textField;
SearchTextField ()
{
this.setLayout(new FlowLayout());
image = new ImageIcon ("img/search.png");
textField = new JTextField ("test");
JLabel label = new JLabel(image);
this.add(label,FlowLayout.LEFT);
this.add(textField, FlowLayout.CENTER);
this.setPreferredSize(new Dimension (250,50));
textField.setPreferredSize(new Dimension (this.getWidth() - 50, this.getHeight()));
}
}
The problem is that the this.getWidth()
method both on the JPanel and JTextArea returns 0. It seems that the component are not already initialized. Have you got any idea about fixing it? thanks!