Hiding UI Elements on Button press using Swing

For the last couple of days I’ve been trying to use a button to show/hide some of the components on the screen of my program node UI, and i cant figure out how to do it. a simplified version of my ProgramNodeView code is as follows:

private void buildUI(JPanel panel, provider)
{

panel.add(Component1())
panel.add(Component2())
panel.add(Button())
}    

private JButton Button()
{
    JButton button = new JButton("hide things");
    button.addActionListener(new ActionListener(){
         @Override
         public void actionPerformed(ActionEvent e)
         {
             some code;
         }
    });
    return button;
}

my question is what can i add to the button’s code to hide the components that i add in the buildUI() method. I tried adding a bit of logic that set a boolean variable to be true when the hide button was pressed, then a couple of if statements in buildUI() to decide if the components needed to be added.
The problem is buildUI() is only called once, so I tried calling buildUI again in the buttons action listener, but I cant get the arguments correct.
Can anyone offer any advice?

1 Like

I’ve tried to make a URcap to test that, where i simply have a button to remove a textfield.
My program looks like this in polyscope:
image

My main code in my class that implements programNodeView have a private methods to create the textfield and the button - the code for creating button looks like this:

In this class I have a public method for setting the visiblity of the button which can be called in the contribution class ( the logic class):

image

In the contribution class that implements programNodeContribution i have used the “undoRedoManager” (check API reference for more info) --> Here is the relevant code snippet from the class:

Hope it helped!

2 Likes

That’s very helpful, thank you very much!