Saturday, 2 June 2018

JavaFX: CheckBox: Event handling

By adding a change listener to checkbox, you can keep track of checkbox state changes.

Ex
checkBox.selectedProperty().addListener((ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) -> {
         System.out.println(checkBox.getText() + " changed from " + old_val + " to " + new_val);
});

Find the below working application.

CheckBoxApp.java
package com.sample.demos;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
  CheckBox checkBox1 = new CheckBox("Chess");
  CheckBox checkBox2 = new CheckBox("Cricket");

  CheckBox[] checkBoxes = { checkBox1, checkBox2 };

  for (CheckBox checkBox : checkBoxes) {
   checkBox.selectedProperty()
     .addListener((ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) -> {
      System.out.println(checkBox.getText() + " changed from " + old_val + " to " + new_val);
     });
  }

  VBox vbox = new VBox(10, checkBox1, checkBox2);

  Scene scene = new Scene(vbox, 400, 300);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX CheckBox Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(CheckBoxApp.class, args);
 }
}




Try to select and unselect the checkboxes, you can able to see below kind of messages in console.

Cricket changed from false to true
Cricket changed from true to false
Chess changed from false to true
Chess changed from true to false





Previous                                                 Next                                                 Home

JavaFX: CheckBox: States of checkbox

CheckBox can be either in defined (Selected/notSelected) (or) undefined states. You can able to select/unselect the checkbox, when the checkbox is in defined state. You can't perform select/unselect the checkbox when the checkbox is in undefined state.

How can I put the checkbox in defined/undefined states?
By using setSelected (), setIndeterminate () methods, you can put the checkbox in defined/undefined states.

setSelected 
setIndeterminate
StateOfCheckBox

true
false
defined
false
false
defined
true
true
undefined
false
true
undefined

Find the below working application.

CheckBoxApp.java
package com.sample.demos;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {

  CheckBox definedCheckBox1 = new CheckBox("definedCheckBox1");
  definedCheckBox1.setSelected(true);
  definedCheckBox1.setIndeterminate(false);

  CheckBox definedCheckBox2 = new CheckBox("definedCheckBox2");
  definedCheckBox2.setSelected(false);
  definedCheckBox2.setIndeterminate(false);

  CheckBox unDefinedCheckBox1 = new CheckBox("unDefinedCheckBox1");
  unDefinedCheckBox1.setSelected(true);
  unDefinedCheckBox1.setIndeterminate(true);

  CheckBox unDefinedCheckBox2 = new CheckBox("unDefinedCheckBox2");
  unDefinedCheckBox2.setSelected(false);
  unDefinedCheckBox2.setIndeterminate(true);

  VBox vbox = new VBox(10, definedCheckBox1, definedCheckBox2, unDefinedCheckBox1, unDefinedCheckBox2);

  Scene scene = new Scene(vbox, 400, 300);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX CheckBox Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}


TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(CheckBoxApp.class, args);
 }
}





Previous                                                 Next                                                 Home

Friday, 1 June 2018

JavaFX: Select the checkbox by default

By using 'setSelected' method of CheckBox class, you can select the check box by default.

Ex
CheckBox checkBox1 = new CheckBox();
checkBox1.setText("Chess");
checkBox1.setSelected(true);

Find the below working application.

CheckBoxApp.java
package com.sample.demos;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
  CheckBox checkBox1 = new CheckBox();
  checkBox1.setText("Chess");
  checkBox1.setSelected(true);

  CheckBox checkBox2 = new CheckBox("Cricket");

  VBox vbox = new VBox(10, checkBox1, checkBox2);

  Scene scene = new Scene(vbox, 400, 300);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX CheckBox Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}


TestFX.java

package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(CheckBoxApp.class, args);
 }
}





Previous                                                 Next                                                 Home

JavaFX: Create check box

CheckBox class provides below constructors to create check box instances.

public CheckBox()
public CheckBox(String text)

Ex
CheckBox checkBox1 = new CheckBox();
checkBox1.setText("Chess");

CheckBox checkBox2 = new CheckBox("Cricket");

Find the below working example.

CheckBoxApp.java
package com.sample.demos;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CheckBoxApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
  CheckBox checkBox1 = new CheckBox();
  checkBox1.setText("Chess");

  CheckBox checkBox2 = new CheckBox("Cricket");

  VBox vbox = new VBox(10, checkBox1, checkBox2);

  Scene scene = new Scene(vbox, 400, 300);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX CheckBox Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(CheckBoxApp.class, args);
 }
}





Previous                                                 Next                                                 Home

JavaFX: Working with check boxes


By using check box widget, we can allow users to select multiple options.

      Create check box
      Select the checkbox by default
      States of checkbox
      Event handling

Previous                                                 Next                                                 Home

JavaFX: Apply css styles to toggle button

We can style toggle button widgets using style sheets.

Below step-by-step procedure explains, how to set the styles to ToggleButton using style sheets.

Step 1: Put all the css styles in a file and add the style sheet to the Scene object.
private static String cssFilePath = "C:\\Users\\krishna\\Documents\\Study\\javaFX\\demo.css";
Scene scene = new Scene(vbox, 800, 400);
scene.getStylesheets().add(new File(cssFilePath).toURI().toURL().toExternalForm());

Step 2: Add the styles to the toggle buttons.
Ex
ToggleButton toggleButton1 = new ToggleButton("Red");
toggleButton1.setUserData(Color.RED);
toggleButton1.getStyleClass().add("toggle-button1");

Find the below working application.

demo.css
.toggle-button1{
  -fx-font: 40 arial;
    -fx-base: red;   
}
 
.toggle-button2{
  -fx-font: 40 arial;
    -fx-base: lightgreen;  
}
 
.toggle-button3{
  -fx-font: 40 arial;
    -fx-base: lightblue;   
}

ToggleButtonApp.java
package com.sample.demos;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class ToggleButtonApp extends Application {
 private static String cssFilePath = "C:\\Users\\krishna\\Documents\\Study\\javaFX\\demo.css";

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
  ToggleGroup toggleGroup = new ToggleGroup();

  ToggleButton toggleButton1 = new ToggleButton("Red");
  toggleButton1.setUserData(Color.RED);
  toggleButton1.getStyleClass().add("toggle-button1");

  ToggleButton toggleButton2 = new ToggleButton("Green");
  toggleButton2.setUserData(Color.GREEN);
  toggleButton2.getStyleClass().add("toggle-button2");

  ToggleButton toggleButton3 = new ToggleButton("Blue");
  toggleButton3.setUserData(Color.BLUE);
  toggleButton3.getStyleClass().add("toggle-button3");

  toggleButton1.setToggleGroup(toggleGroup);
  toggleButton2.setToggleGroup(toggleGroup);
  toggleButton3.setToggleGroup(toggleGroup);

  Rectangle rect = new Rectangle();
  rect.setHeight(200);
  rect.setWidth(800);
  rect.setFill(Color.RED);
  rect.setStroke(Color.DARKGRAY);
  rect.setStrokeWidth(2);
  rect.setArcHeight(10);
  rect.setArcWidth(10);

  toggleGroup.selectedToggleProperty()
    .addListener((ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) -> {
     if (new_toggle == null)
      rect.setFill(Color.WHITE);
     else
      rect.setFill((Color) toggleGroup.getSelectedToggle().getUserData());
    });

  HBox hbox = new HBox(toggleButton1, toggleButton2, toggleButton3);
  VBox vbox = new VBox(hbox, rect);

  hbox.setAlignment(Pos.TOP_CENTER);

  Scene scene = new Scene(vbox, 800, 400);
  scene.getStylesheets().add(new File(cssFilePath).toURI().toURL().toExternalForm());

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Toggle Button Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}


TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(ToggleButtonApp.class, args);
 }
}




Previous                                                 Next                                                 Home

JavaFX: Set behavior to toggle button

By adding ChangeListener to the ToggleGroup instance, we can set the behavior to toggle button.

Ex
         toggleGroup.selectedToggleProperty()
         .addListener((ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) -> {
                  if (new_toggle == null)
                           rect.setFill(Color.WHITE);
                  else
                           rect.setFill((Color) toggleGroup.getSelectedToggle().getUserData());
         });
        
Find the below working application.

ToggleButtonApp.java
package com.sample.demos;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class ToggleButtonApp extends Application {

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException {
  ToggleGroup toggleGroup = new ToggleGroup();

  ToggleButton toggleButton1 = new ToggleButton("Red");
  toggleButton1.setUserData(Color.RED);

  ToggleButton toggleButton2 = new ToggleButton("Green");
  toggleButton2.setUserData(Color.GREEN);

  ToggleButton toggleButton3 = new ToggleButton("Blue");
  toggleButton3.setUserData(Color.BLUE);
  
  ToggleButton toggleButton4 = new ToggleButton("Aqua");
  toggleButton4.setUserData(Color.AQUA);

  toggleButton1.setToggleGroup(toggleGroup);
  toggleButton2.setToggleGroup(toggleGroup);
  toggleButton3.setToggleGroup(toggleGroup);
  toggleButton4.setToggleGroup(toggleGroup);

  Rectangle rect = new Rectangle();
  rect.setHeight(200);
  rect.setWidth(800);
  rect.setFill(Color.RED);
  rect.setStroke(Color.DARKGRAY);
  rect.setStrokeWidth(2);
  rect.setArcHeight(10);
  rect.setArcWidth(10);

  toggleGroup.selectedToggleProperty()
    .addListener((ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) -> {
     if (new_toggle == null)
      rect.setFill(Color.WHITE);
     else
      rect.setFill((Color) toggleGroup.getSelectedToggle().getUserData());
    });

  HBox hbox = new HBox(toggleButton1, toggleButton2, toggleButton3, toggleButton4);
  VBox vbox = new VBox(hbox, rect);

  hbox.setAlignment(Pos.TOP_CENTER);

  Scene scene = new Scene(vbox, 800, 400);

  /* Set the scene to primaryStage, and call the show method */
  primaryStage.setTitle("JavaFX Toggle Button Example");
  primaryStage.setScene(scene);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

public class TestFX {
 public static void main(String args[]) {
  Application.launch(ToggleButtonApp.class, args);
 }
}

Depends on the selection of toggle button, rectangle color will change.


Previous                                                 Next                                                 Home