Question

I've initialized a GridBagLayout globally, then in my class constructor have instantiated it and added some buttons etc. to it.

How can I add things to it after the fact? Simple class extends JFrame. Whenever I try class.add(stuff, gridbagconstraints) after the fact (used just add(stuff, gribagconstraints) in constructor) Nothing happens and nothing is added to my layout.

Do I need to "refresh" the layout manager or something? It is declared globally.

Update: I've tried revalidate() but it does not seem to be working, here is a simplified version of my code with a test button in place for proof of concept:

public class MainGUI extends JPanel{
    static GridBagConstraints c;
    static MainGUI mainGUIclass;
    static JFrame mainGUIframe;

    public MainGUI() {
    this.setLayout(new GridBagLayout());
    c = new GridBagConstraints();

    saveButton = new JButton("Save and Exit");
    saveButton.setPreferredSize(new Dimension(200, 30));

    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 4;
    add(saveButton, c);
    }

public static void main(String[] args) {
    mainGUIframe = new JFrame("Message");

    mainGUIframe.setSize(800,800);

    mainGUIclass = new MainGUI();

    mainGUIframe.add(mainGUIclass);
    mainGUIframe.setVisible(true);


   //now the addition
    JButton newButton = new JButton("New Button");  

    newButton.setPreferredSize(new Dimension(200, 30));


    c.gridx = 5;
    c.gridy = 0;
    c.gridwidth = 4;
    mainGUIclass.add(newButton,c);

    //none of this seems to work
    mainGUIclass.revalidate();//?
    mainGUIclass.repaint();//?

  }
}

Update2: This appears to be an issue with the passbyvalue nature of java and another class(canvas) I am trying to add to my layout. Will update if I find a solution.

Update3: This is a thread problem, the class I'm calling is hanging the main window.

Edit: I provided the code as a reference, and tried to be complete to provide a complete picture, not to be easily compiled on its own. Thank you to all who offered your assistance.

Update4: Success! They key was that the mediaplayer class did an "isDisplayable()" check, which caused it to hang the program if the frame it was being added to was not get added to the gridbaglayout. A series of unfortunate looking pass by values (of JInternalFrames), preadding the internalframe to the gridbaglayout and a remote start of the media from another method allows what I am seeking to work.

Was it helpful?

Solution

In your example you are adding a button with exactly the same GridBagConstraints as the previous button. When I try running that code your buttons are laid on top of each other and so you will only see one of them. Try changing your GridBagConstraints so that the second button you add is placed in another location. It is recommended practice to instantiate a new GridBagConstraints for each constrained component to remove the chance of such programming errors occurring.

Also, with regards to your update, JFrame does not have a revalidate() function.

If you haven't already done so, it would be worth you reading thoroughly through this.

OTHER TIPS

You would call revalidate() on the container (if it derives from JComponent such as JPanel) that uses the layout to have the layouts re-set the components they contain. This should recurse through all the containers held by this layout, and they too should have their component layouts updated. The main exception to this that I know of would be components held in JScrollPanes, and for that, you'd need to call revalidate on the scrollpane's JViewport.

Also, sometimes you will need to call repaint() after revalidate(), especially if you have removed any components held by the container.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top