arcgissamples\scenario\toc\CheckBoxPanel.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Engine Viewer
arcgissamples\scenario\toc\CheckBoxPanel.java
/* Copyright 2015 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
/*
 * ArcGIS Engine Developer Sample
 * Application Name: CheckBoxPanel.java
 */

package arcgissamples.scenario.toc;

/** Displays check box to toggle the visibility of layer.
 *  A Check box is created and added to JPanel. checkbox height and fontheight is
 *  checked to make sure the text does not get cuttoff
 */

import java.awt.Dimension;

import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.UIManager;


public class CheckBoxPanel extends JPanel {
  private static final long serialVersionUID = 1L;
  JCheckBox check = null;

  public CheckBoxPanel() {
    //Create a checkbox object
    setLayout(null);
    check = new JCheckBox();
    //Add check box.
    add(check);
    //sets background color to that of tree control.
    check.setBackground(UIManager.getColor("Tree.textBackground"));
    this.setBackground(UIManager.getColor("Tree.textBackground"));
  }

  /**Sets the preffered size of the panel.
   * @see javax.swing.JComponent#getPreferredSize()
   * @return java.awt.Dimension
   */

  public Dimension getPreferredSize() {
    Dimension checkDimension = check.getPreferredSize();
    return checkDimension;
  }

  /**Toggles check box.
   * Used to set layer visibility
   * @param value
   */

  public void setSelected(boolean value) {
    check.setSelected(value);
  }

  /**Sets the label text for the checkbox
   * @param stringValue
   */

  public void setText(String stringValue) {
    check.setText(stringValue);
  }

  /** Lays out checkbox based on the prefferred width and height
   *  @see javax.swing.JComponent#doLayout()
   */

  public void doLayout() {
    java.awt.Font font = check.getFont();
    java.awt.FontMetrics fontMetrics = check.getFontMetrics(font);
    check.setBounds(0, 0, check.getPreferredSize().width,
        fontMetrics.getHeight());
  }

}