Raggaer
Raggaer

Reputation: 3318

Java JTextField stopping GUI

Currently my code looks like this

public Frame(String title, Integer x, Integer y)
{
    gui = new JFrame(title);

    // Give style to the GUI

    this.Style(x, y);

    // Build our GUI

    new Builder(gui);
}

public void Style(Integer width, Integer height)
{
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setVisible(true);
    gui.setResizable(false);
    gui.setSize(new Dimension(width, height));
    gui.setLayout(new GridLayout(5, 4));
}

Builder.java

public class Builder 
{
    public Builder(JFrame frame)
    {
        Build gui = new Build(frame);
        JTextField input = gui.buildInput("aaaaa");
        JButton button1 = gui.buildButton("aaa");
        JButton button21 = gui.buildButton("aaa");
    }
}

Build.java

public class Build 
{
    private JFrame frame;

    public Build(JFrame frame)
    {
        this.frame = frame;
    }

    public JButton buildButton(String text)
    {
        JButton button = new JButton(text);
        frame.add(button);

        return button;
    }

    public JTextField buildInput(String text)
    {
        JTextField textArea = new JTextField(text);
        frame.add(textArea);

        return textArea;
    }
}

Everything works fine except when I try to add a JTextField, if I do it the JTextField doesnt appear and the rest of the GUI elements dissapear, for example I try the following on my Builder class

   Build gui = new Build(frame);
   //JTextField input = gui.buildInput("aaaaa");
   JButton button1 = gui.buildButton("aaa");
   JButton button21 = gui.buildButton("aaa");

Works and displays 2 buttons on my frame but I un-comment the JTextField input the GUI buttons wont show same as the TextField.

Upvotes: 0

Views: 70

Answers (1)

Sam
Sam

Reputation: 1206

I haven't tracked down any official documentation regarding your issue, but the problem appears to be that your frame is not invalidating and re-validating itself when you add the components to it. I was able to resolve this two different ways.

  1. Move your call for frame.setVisible(true) to after you have built the GUI. This will force the frame to re-validate its layout before displaying.
  2. Make a call to frame.validate() after building the GUI, which also solves the problem.

Placing either of these in the final line of your Builder constructor worked fine for me in both cases.

Ordinarily, adding components to another component should force a re-validation, but for some reason this is not happening. However, I generally try to add all my components to my frames before making them visible anyways, and might be a good practice to get into.

Edit: I found this in the API documentation for the add(Component comp) method in java.awt.Containter (emphasis mine):

This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component.

Thus, if the component is already visible, it must be validated. Calling setVisible(true) will cause a validation, and is why if it is called after adding everything fixes the problem.

Upvotes: 1

Related Questions