Reset JComponent to default value

后端 未结 5 559
广开言路
广开言路 2021-01-21 16:58

For example, if component is a checkbox it must set to false, or it is a textfield it must be clear the text. I am trying to write a method for reset all components in a J

5条回答
  •  隐瞒了意图╮
    2021-01-21 17:37

    There is no reset function in Swing. The best way to do this is to create a method with the values you want to reset and set everything here e.g. :

    public void resetWindow(){
        checkBox.setSelected(false);
        textField.setText("");
    }
    

    The advantage of using this way is that you can just reuse this method whenever you want to reset and also when the class loads.

    The other way you could do it is by creating another instance of your Panel and throwing away the original. That way everything would be in the start state.

提交回复
热议问题