IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 3 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Three-state checkbox in Java
I was very proud of my solution :) Hack, I know, but it should work for the time being

(since this is written on company time, I don't publish the whole thing, but the model for it is trivial)

\npublic class ThreeStateCheckBox extends JCheckBox\n{\n\n  static ButtonModel thirdStateModel = new AlwaysPressedSelectedArmedModel();\n\n  ThreeStateCheckBox(String text) {\n    super(text);    \n    setModel(new ThreeStateButtonModel());\n  }\n\n  protected void paintComponent(Graphics g) {\n    ThreeStateButtonModel model = (ThreeStateButtonModel)getModel();\n      \n    if (model.getThirdState() && model.isEnabled()) {\n      // in this state, we want the checkbox to be painted selected,\n      // pressed an armed. So, we substitute our real model with a\n      // fake one that returns selected, pressed and armed no matter\n      // what. Then, after the painting is done, we restore the original model.\n      \n      // this relies on the single-threaded nature of Swing - no other\n      // events will be processed while we're painting. We will have\n      // to change the code if mouse clicks will start happening in\n      // parallel with painting\n\n      // we have to access "model" directly, because setModel() causes\n      // a reppint, among other things\n\n      this.model = thirdStateModel;\n      super.paintComponent(g);\n      this.model = model;\n\n      // back to the originally scheduled programming\n\n    } else {\n      super.paintComponent(g);      \n    }\n  }\n\n  void setThirdState() {\n    ((ThreeStateButtonModel)getModel()).setThirdState();\n  }\n\n  boolean getThirdState() {\n    return ((ThreeStateButtonModel)getModel()).getThirdState();\n  }\n\n  /// The model that cause the checkbox to paint itself in the right\n  /// way in the third state\n  static public class AlwaysPressedSelectedArmedModel extends DefaultButtonModel {\n    public boolean isPressed() {\n      return true;\n    }\n\n    public boolean isArmed() {\n      return true;\n    }\n    \n    public boolean isSelected() {\n      return true;\n    }\n\n  }\n\n<model code here>\n\n}\n\n
--

Select [link|http://www.glumbert.com/pictures/Default.asp?index=30|here].
New tsk tsk tsk
Don't you have any shame? You're supposed to file an environmental impact statement with Sun and the State of Schwarzifornia before hacking up the woods :)
-drl
New Yes/No/Maybe so?
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Indeed
--

Select [link|http://www.glumbert.com/pictures/Default.asp?index=30|here].
New Hm.
The main place where I see tristate checkboxes/buttons is in Describe (OS/2 wp). It's used in the styles dialogue, and indicates whether an option is enable, disabled, or inherited from the parent style. For that use, it makes perfect sense.
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New Tree
All children selected, none selected, some selected.
--

Select [link|http://www.glumbert.com/pictures/Default.asp?index=30|here].
New Ahhh....
--\n-------------------------------------------------------------------\n* Jack Troughton                            jake at consultron.ca *\n* [link|http://consultron.ca|http://consultron.ca]                   [link|irc://irc.ecomstation.ca|irc://irc.ecomstation.ca] *\n* Kingston Ontario Canada               [link|news://news.consultron.ca|news://news.consultron.ca] *\n-------------------------------------------------------------------
New Why not use a drop-down list with 3 values?
Although I realize that sometimes the customer wants stupid things and we have to deliver.
________________
oop.ismad.com
New It's in a tree
combobox just would not look natural

Also, those checkboxes are used in property boxes that reflect multiple selection: what if some files are read-only, and some are not?
--

Select [link|http://www.glumbert.com/pictures/Default.asp?index=30|here].
     Three-state checkbox in Java - (Arkadiy) - (8)
         tsk tsk tsk - (deSitter)
         Yes/No/Maybe so? -NT - (admin) - (4)
             Indeed -NT - (Arkadiy) - (3)
                 Hm. - (jake123) - (2)
                     Tree - (Arkadiy) - (1)
                         Ahhh.... -NT - (jake123)
         Why not use a drop-down list with 3 values? - (tablizer) - (1)
             It's in a tree - (Arkadiy)

They're going to sue you.
165 ms