Posted By

s1w on 11/05/14


Tagged

checkbox three jcheckbox tristate tri-state


Versions (?)

Tri-state JCheckBox (using ClientProperty and native icon)


 / Published in: Java
 

URL: http://siwego.net/dev/tri-state.png

The best lightweight solution to implement tri-state CheckBox in java.

usage: TCheckBox tcb = new TCheckBox("My CheckBox");

TCheckBox tcb = new TCheckBox("My CheckBox", 1);

  1. /*
  2.  * Tri-state checkbox example
  3.  * @s1w_
  4. */
  5. import javax.swing.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8.  
  9.  
  10. class TCheckBox extends JCheckBox implements Icon, ActionListener {
  11.  
  12. final static boolean MIDasSELECTED = true; //consider mid-state as selected ?
  13.  
  14.  
  15. public TCheckBox() { this(""); }
  16.  
  17. public TCheckBox(String text) {
  18. super(text);
  19. putClientProperty("SelectionState", 0);
  20. setIcon(this);
  21. addActionListener(this);
  22. }
  23.  
  24. public TCheckBox(String text, int sel) {
  25. /* tri-state checkbox has 3 selection states:
  26.   * 0 unselected
  27.   * 1 mid-state selection
  28.   * 2 fully selected
  29.   */
  30. super(text, sel > 1 ? true : false);
  31.  
  32. switch (sel) {
  33. case 2: setSelected(true);
  34. case 1:
  35. case 0:
  36. putClientProperty("SelectionState", sel);
  37. break;
  38. default:
  39. }
  40. addActionListener(this);
  41. setIcon(this);
  42. }
  43.  
  44. @Override
  45. public boolean isSelected() {
  46. if (MIDasSELECTED && (getSelectionState() > 0)) return true;
  47. else return super.isSelected();
  48. }
  49.  
  50. public int getSelectionState() {
  51. return (getClientProperty("SelectionState") != null ? (int)getClientProperty("SelectionState") :
  52. super.isSelected() ? 2 :
  53. 0);
  54. }
  55.  
  56. public void setSelectionState(int sel) {
  57. switch (sel) {
  58. case 2: setSelected(true);
  59. break;
  60. case 1:
  61. case 0: setSelected(false);
  62. break;
  63. default: throw new IllegalArgumentException();
  64. }
  65. putClientProperty("SelectionState", sel);
  66. }
  67.  
  68.  
  69. final static Icon icon = UIManager.getIcon("CheckBox.icon");
  70.  
  71. @Override
  72. public void paintIcon(Component c, Graphics g, int x, int y) {
  73. icon.paintIcon(c, g, x, y);
  74. if (getSelectionState() != 1) return;
  75.  
  76. int w = getIconWidth();
  77. int h = getIconHeight();
  78. g.setColor(c.isEnabled() ? new Color(51, 51, 51) : new Color(122, 138, 153));
  79. g.fillRect(x+4, y+4, w-8, h-8);
  80.  
  81. if (!c.isEnabled()) return;
  82. g.setColor(new Color(81, 81, 81));
  83. g.drawRect(x+4, y+4, w-9, h-9);
  84. }
  85.  
  86. @Override
  87. public int getIconWidth() {
  88. return icon.getIconWidth();
  89. }
  90.  
  91. @Override
  92. public int getIconHeight() {
  93. return icon.getIconHeight();
  94. }
  95.  
  96. public void actionPerformed(ActionEvent e) {
  97. TCheckBox tcb = (TCheckBox)e.getSource();
  98. if (tcb.getSelectionState() == 0)
  99. tcb.setSelected(false);
  100.  
  101. tcb.putClientProperty("SelectionState", tcb.getSelectionState() == 2 ? 0 :
  102. tcb.getSelectionState() + 1);
  103.  
  104. // test
  105. System.out.println(">>>>IS SELECTED: "+tcb.isSelected());
  106. System.out.println(">>>>IN MID STATE: "+(tcb.getSelectionState()==1));
  107. }
  108. }

Report this snippet  

You need to login to post a comment.