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

Wednesday, 30 May 2018

JavaFX: Add toggle buttons to a group

By using 'setToggleGroup' method of ToggleButton class, you can add toggle buttons to a group. At any point of time, you can able to select only one toggle button from a specific group.

Ex
ToggleGroup toggleGroup = new ToggleGroup();

ToggleButton toggleButton1 = new ToggleButton("Toggle Button 1");
ToggleButton toggleButton2 = new ToggleButton("Toggle Button 2");
ToggleButton toggleButton3 = new ToggleButton("toggle button 3");

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

ToggleButtonApp.java
package com.sample.demos;

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

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
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("Toggle Button 1");
  ToggleButton toggleButton2 = new ToggleButton("Toggle Button 2");
  ToggleButton toggleButton3 = new ToggleButton("toggle button 3");

  toggleButton1.setToggleGroup(toggleGroup);
  toggleButton2.setToggleGroup(toggleGroup);
  toggleButton3.setToggleGroup(toggleGroup);
  
  HBox hbox = new HBox(toggleButton1, toggleButton2, toggleButton3);

  hbox.setAlignment(Pos.TOP_CENTER);

  Scene scene = new Scene(hbox, 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);
 }
}



Previous                                                 Next                                                 Home

JavaFX: Create Toggle Button

ToggleButton class provides below constructors to create ToggleButton objects.

public ToggleButton()
public ToggleButton(String text)
public ToggleButton(String text, Node graphic)

Ex
ToggleButton toggleButton1 = new ToggleButton();
toggleButton1.setText("Toggle Button 1");

ToggleButton toggleButton2 = new ToggleButton("Toggle Button 2");

InputStream is = new FileInputStream(imageFilePath);
Image image = new Image(is);
ToggleButton toggleButton3 = new ToggleButton("toggle button 3", new ImageView(image));

Find the below working example.

ToggleButtonApp.java
package com.sample.demos;

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

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ToggleButtonApp extends Application {
 private static String imageFilePath = "C:\\Users\\krishna\\Documents\\Study\\javaFX\\javaIcon.png";

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

  ToggleButton toggleButton1 = new ToggleButton();
  toggleButton1.setText("Toggle Button 1");

  ToggleButton toggleButton2 = new ToggleButton("Toggle Button 2");

  InputStream is = new FileInputStream(imageFilePath);
  Image image = new Image(is);
  ToggleButton toggleButton3 = new ToggleButton("toggle button 3", new ImageView(image));

  toggleButton1.setToggleGroup(toggleGroup);
  toggleButton2.setToggleGroup(toggleGroup);
  toggleButton3.setToggleGroup(toggleGroup);
  
  HBox hbox = new HBox(toggleButton1, toggleButton2, toggleButton3);

  hbox.setAlignment(Pos.TOP_CENTER);

  Scene scene = new Scene(hbox, 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);
 }
}





Previous                                                 Next                                                 Home