2

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!

lbalazscs
  • 17,474
  • 7
  • 42
  • 50
Luca
  • 1,704
  • 3
  • 29
  • 42
  • 2
    [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing](http://stackoverflow.com/q/7229226/230513)? – trashgod Sep 20 '12 at 19:47
  • [JTextField(String text, int columns)](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html#JTextField%28java.lang.String,%20int%29) and `JPanel` has implented `FlowLayout` as default `LayotManager` in `API` – mKorbel Sep 20 '12 at 20:21

2 Answers2

3

Don't set the preferred size of the textfield, and especially not with a code that references the panels size. Also it is not necessary to set the preferred size of the panel. If your image is 50x50 pixels, FlowLayout will take care of the rest.

You probably should use a JTextField constructor where you can specify the size in characters, this will ensure that horizontally your JTextField has the right size.

lbalazscs
  • 17,474
  • 7
  • 42
  • 50
1

The size of a Swing component is not set until all of the components have been validated. Part of what frame.pack() does is validate the components.

You can fix your immediate problem by changing this line:

textField.setPreferredSize(new Dimension
    (this.getWidth() - 50, this.getHeight()));

to

textField.setPreferredSize(new Dimension 
    (this.getPreferredWidth() - 50, this.getPreferredHeight()));
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Really intresting workaround! maybe you have also the solution for my original problem: i wan' t to resize the JTextField when the JPanel i resized to fit his size and subtract a fixed value. Thanks! – Luca Sep 20 '12 at 19:20
  • @Luca: You'll have to use a layout manager, rather than calculating the sizes yourself. Since you only have one component inside your JPanel, you can use the BorderLayout. Put the text field in the center of the BorderLayout. http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – Gilbert Le Blanc Sep 20 '12 at 19:26
  • I have two component in my layout, a JLabel and JTextField. I tried with BorderLayout with label in the `LINE_START` and JextField in `CENTER` but this is not working – Luca Sep 21 '12 at 10:46
  • @Luca: You'll have to go to the GridBag layout, then. See this answer of mine for an example of the GridBagLayout. http://stackoverflow.com/questions/12516472/gridbag-layout-in-java/12516849#12516849 – Gilbert Le Blanc Sep 21 '12 at 13:15