ReactJS: Checkbox

This entry is part 26 of 35 in the series React JS

See the Pen Stateful React checkbox component by daryl (@darylw) on CodePen.0

/**
* www.devhamsters.com
*/

class Checkbox extends React.Component {
  static propTypes = {
    checked: React.PropTypes.bool,
    disabled: React.PropTypes.bool,
  };
  static defaultProps = {
    checked: false,
    disabled: false,
  };
  constructor(props) {
    super(props);
    this.state = {
      checked: props.checked,
    };
  };

  _handleChange = () => {
    this.setState({
      checked: !this.state.checked,
    });
  };

  render() {
    const { disabled } = this.props;
    const { checked } = this.state;
    return (
      <div className="React__checkbox">
        <label>
          <input 
            type="checkbox"
            className="React__checkbox--input"
            checked={checked}
            disabled={disabled}
            onChange={this._handleChange}
          />
          <span 
            className="React__checkbox--span"
          />
        </label>
      </div>
    );
  }
}

const App = (
  <div className="App">
    <Checkbox />
    <Checkbox checked />
    <Checkbox disabled />
    <Checkbox checked disabled />
  </div>
);
      
ReactDOM.render(
  App, 
  document.getElementById('app')
);

ReactJS: Flickering checkbox

This entry is part 13 of 35 in the series React JS

https://codepen.io/gaearon/pen/PJKEGp

class FlickeringCheckbox extends React.Component {
  constructor(){
    super();
    this.state = { isIndeterminate: true };
  }
  componentDidMount() {
    setInterval(() => this.setState({isIndeterminate: !this.state.isIndeterminate}), 500);
  }
  render() {
    return <input 
      type="checkbox"
      ref={elem => elem && (elem.indeterminate = this.state.isIndeterminate)}
    />

  }
}

ReactDOM.render(
  <FlickeringCheckbox />,
  document.getElementById('root')
);

JavaFX: Which checkbox is checked

This entry is part 30 of 55 in the series JavaFX

In an event listener, you can test the state of a check box by using the isSelected method, and you can set the state of a check box by calling its setSelected method.

package javafxapplication24;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;

public class JavaFXApplication24 extends Application {

    CheckBox cb1 = new CheckBox("Rock");
    CheckBox cb2 = new CheckBox("Country");

    @Override
    public void start(Stage stage) {

        stage.setTitle("JavaFX Program!");

        VBox vbox_pane = new VBox();
        vbox_pane.setAlignment(Pos.CENTER);

        HBox hbox1 = new HBox();
        HBox hbox2 = new HBox();
        HBox hbox3 = new HBox();

        hbox1.setPadding(new Insets(15, 12, 15, 12));
        hbox1.setAlignment(Pos.CENTER_LEFT);

        hbox2.setPadding(new Insets(15, 12, 15, 12));
        hbox2.setAlignment(Pos.CENTER_LEFT);

        Button btn = new Button("Submit");
        
        cb2.setSelected(true);

        btn.setOnAction(e -> buttonClick());

        hbox1.getChildren().add(cb1);
        hbox2.getChildren().add(cb2);
        hbox3.getChildren().add(btn);

        vbox_pane.getChildren().addAll(hbox1, hbox2, btn);

        Scene scene = new Scene(vbox_pane, 300, 250);
        stage.setScene(scene);

        stage.show();
    }

    private void buttonClick() {

        if (cb1.isSelected()) {
            System.out.println("You like Rock music");
        }
        if (cb2.isSelected()) {
            System.out.println("You like Country music");
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

checkbox2

checkbox3

JavaFX: Creating checkboxes

This entry is part 29 of 55 in the series JavaFX

Checkboxes allow multiple selections.  Here in this post, we also look at how to put 2 Hboxes into a Vbox where each Hbox is for one Checkbox.

Creating a checkbox is relatively easy.

CheckBox cb1 = new CheckBox("Rock");

Consider the the example below.

package javafxapplication24;
 
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
 
public class JavaFXApplication24 extends Application {
 
    @Override
    public void start(Stage stage) {
 
        stage.setTitle("JavaFX Program!");
        
        VBox vbox_pane = new VBox();
        vbox_pane.setAlignment(Pos.CENTER);
        
        HBox hbox1 = new HBox();
        HBox hbox2 = new HBox();
        
        hbox1.setPadding(new Insets(15, 12, 15, 12));
        hbox1.setAlignment(Pos.CENTER_LEFT);
        
        hbox2.setPadding(new Insets(15, 12, 15, 12));
        hbox2.setAlignment(Pos.CENTER_LEFT);
 
        CheckBox cb1 = new CheckBox("Rock");
        CheckBox cb2 = new CheckBox("Country");
 
        hbox1.getChildren().add(cb1);   
        hbox2.getChildren().add(cb2);  
        
        vbox_pane.getChildren().addAll(hbox1,hbox2);
 
        Scene scene = new Scene(vbox_pane, 300, 250);
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

checkbox

WPF: Checkbox isChecked

This entry is part 17 of 54 in the series Learn WPF

How do we know if a checkbox is checked, you may ask.

The WPF checkbox comes with a IsChecked boolean where we can use it to determine if a checkbox is checked or unchecked.

In the example below, we set the checkbox using IsChecked=’True”.

MainWindow.xaml
<Window x:Class="CheckBox1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <StackPanel>
        <Grid Name="grid1">
            <CheckBox Name="cb" IsChecked="True" Margin="50" HorizontalAlignment="left" FontSize="24" >Check Box</CheckBox>
        </Grid>
        <TextBlock Name="tB" Text="Check Box Status" HorizontalAlignment="right" Margin="50"></TextBlock>
    </StackPanel>
</Window>

MainWindow.xaml
using System.Windows;
using System.Windows.Controls;

namespace CheckBox1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            if (cb.IsChecked==true)
            {
                tB.Text = "Checkbox is checked";
            }
        }       
    }
}

The output from running the above code is:

isChecked