0

I have this piece of code, which should remove a label, when a button is pressed:

final JLabel label = new JLabel("Label text");
rightPanel.add(label);

final JButton remove = new JButton("Remove label");
leftPanel.add(remove);
add.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e)
    {
        rightPanel.remove(label);
    }
});

But when I click on the button, it doesn't remove the label text. Only when I resize the window (for example set it to full screen), the label text dissapears.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
danrodi
  • 296
  • 1
  • 4
  • 24
  • you need to dispose the JFrame o JDialog – RamonBoza Nov 08 '13 at 15:14
  • You are taking back in the WAY-back machine with my pet monkey, but if memory serves me, in AWT and Swing you have to mark the UI dirty somehow after you made changes to it, or at least somehow signal that the window should repaint. Resizing the window would do that for you. I also seem to remember that repainting was a suggestion, and that it didn't happen right when you called the method. Hopefully you can google a bit and find what you need. – CodeChimp Nov 08 '13 at 15:15
  • @RamonBoza Do you mean the extends JFrame in public class? – danrodi Nov 08 '13 at 15:16
  • Take a look at [this question](http://stackoverflow.com/questions/2381169/updating-a-panel) - one answer suggests telling the frame to `revalidate()`. – thegrinner Nov 08 '13 at 15:16
  • repaint a component)) – Andrew Evt Nov 08 '13 at 15:16
  • Thank you for these answers, using frame.repaint(); after removing the label seemed to do it. – danrodi Nov 08 '13 at 15:20
  • @danrodi accept the answer to close off the question - also you should upvote camickr's answer from the question when it was asked previously :) – Katana24 Nov 08 '13 at 15:22
  • @Katana24 Glad you said it. – danrodi Nov 08 '13 at 15:26

2 Answers2

6

From this previous answer located here, put forth by camickr, you need to do the following:

The code would be (assuming the use of a JPanel):

panel.remove(...);
panel.revalidate();
panel.repaint(); // sometimes needed

You need to remove the component and then tell the panel to layout the remaining components.

Community
  • 1
  • 1
Katana24
  • 8,706
  • 19
  • 76
  • 118
  • ^This should do it. Basically the Model was updated (removed button) hopefully by a Controller. But the view has to be signalled that the model changed, so that the viewport can be updated. This is a very short resume of how the Model-View-Controller should work. Swing was build with the MVC paradigm in mind BTW. – Ric Jafe Nov 08 '13 at 15:20
1

Perhaps not an answer to your question but what I consider helpful advice: only add/remove components when it is absolutely necessary. If you get creative, you'll find that there are often better solutions than adding/removing components. For example, instead of removing a JButton, consider disabling it instead.

In your situation, you could always just do label.setText(""). This way you don't need to revalidate() and repaint().

I very rarely call revalidate() and repaint() in my code. I think it's better to update the existing components than to remove/add them.

ryvantage
  • 13,064
  • 15
  • 63
  • 112