开发者

How to remove JLabels?

开发者 https://www.devze.com 2023-02-13 21:01 出处:网络
Just how you add JLabels, JTextFields, JButtons by doing this add(label1); add(button1); how to remove?

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 comp开发者_运维知识库liation or execution error.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号