Reset JComponent to default value

后端 未结 5 560
广开言路
广开言路 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:12

    One possible workaround would be to create a custom reset function. Reinitialize the panel ( your form).

    For e.g.

    void reset(){
        //recreate the form panel.
        formPanel = new FormPanel(); 
    }
    

    Create a custom class FormPanel to store the form fields and their listeners.

    Re initializing the panel components would result in an overhead of reassigning the listeners as @Robin suggested.

    0 讨论(0)
  • 2021-01-21 17:16

    As a good developer, you probably have a nice separation between view and model. When the model gets updated, those changes are then reflected in the view.

    If you have such structure, you could simply reset your model to a default state, and that reset-ed state then become visible in the UI (which means the UI is reset to its default state)

    0 讨论(0)
  • 2021-01-21 17:28

    there is no reset sort of method for swing component, your code should handle it.

    0 讨论(0)
  • 2021-01-21 17:29

    Write a method which sets the initial values of all elements on start. Then you can reuse this initialization method to "reset" the values.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题