razshan
razshan

Reputation: 1138

How to remove JLabels?

Just how you add JLabels, JTextFields, JButtons by doing this add(label1); add(button1);

how to remove?

I have a button that will remove a particular JTextField.

The button:

thehandler3 handler3 = new thehandler3();
button3.addActionListener(handler3); // first x button

private class thehandler3 implements ActionListener{
    public void actionPerformed(ActionEvent event){

        remove(field1);

    }}

It is not working. I get no compliation or execution error.

Upvotes: 1

Views: 6139

Answers (2)

Isaac Truett
Isaac Truett

Reputation: 8884

You need to call remove() on the container you want to remove the component from, like this:

panel.remove(label1);

You will also want to consider threading issues when updating the UI:

Event Dispathcing Thread

Upvotes: 4

camickr
camickr

Reputation: 324118

The code would be:

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

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

Upvotes: 4

Related Questions