How to disable a container and its children in Swing

后端 未结 6 744
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 16:44

I cannot figure out a way to disable a container AND its children in Swing. Is Swing really missing this basic feature?

If I do setEnabled(false) on a container, its

6条回答
  •  盖世英雄少女心
    2020-12-08 17:02

    As VonC's answer, there's no simple solution existed. So i recommend you to program with a supporting infrastructure from the start.

    A simple infrastructure is likely to be, for example, using delegated listeners that do a "event enabled" check from a super container's flag before actual event-respond:

    class ControlledActionListener extends ActionListener {
        ...
        public void actionPerformed( ActionEvent e ) {
            if( !container.isEnabled() ) return;
    
            doYourBusinessHere();
        }
    }
    

    Or even better, you can use the APT to automatically inject the boilerplate code for you.

    This works well all the time. It's the clean way to block both user interaction and programming calls with a single effort. Even though it costs you some codes to support the underlying functionality, you get simplicity, usablity and stability in return.

    PS. i'd like to see better solution to this problem.

提交回复
热议问题