ToggleButton
is a button kind of widget, which can be selected. In case of normal button, on
click we execute an action. A
ToggleButton on the other hand is simply a control with a Boolean indicating
whether it has been selected.
This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
Wednesday, 30 May 2018
JavaFX: Toggle Button
JavaFX: event handling for radio buttons
By adding
a ChangeListener to 'toggleGroup' we can receive the events on change in radio
button selection.
Ex
toggleGroup.selectedToggleProperty()
.addListener((ObservableValue<?
extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) -> {
if (toggleGroup.getSelectedToggle() !=
null) {
RadioButton radioButton =
(RadioButton) toggleGroup.getSelectedToggle();
System.out.println("User
selected : " + radioButton.getText());
}
});
Find the
below working application.
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.RadioButton; import javafx.scene.control.Toggle; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class RadioButtonApp extends Application { @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { ToggleGroup toggleGroup = new ToggleGroup(); RadioButton radioButton1 = new RadioButton("Java"); radioButton1.setToggleGroup(toggleGroup); radioButton1.setSelected(true); RadioButton radioButton2 = new RadioButton("Python"); radioButton2.setToggleGroup(toggleGroup); RadioButton radioButton3 = new RadioButton("Haskell"); radioButton3.setToggleGroup(toggleGroup); VBox vBox = new VBox(10, radioButton1, radioButton2, radioButton3); vBox.setAlignment(Pos.CENTER); toggleGroup.selectedToggleProperty() .addListener((ObservableValue<? extends Toggle> ov, Toggle old_toggle, Toggle new_toggle) -> { if (toggleGroup.getSelectedToggle() != null) { RadioButton radioButton = (RadioButton) toggleGroup.getSelectedToggle(); System.out.println("User selected : " + radioButton.getText()); } }); Scene scene = new Scene(vBox, 400, 400); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX Radio 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(RadioButtonApp.class, args); } }
When you
ran the application, it opens below window.
Change the
radio button selection, you can able to see the selected radio button text in
console.
Monday, 28 May 2018
JavaFX: Grouping radio buttons
By using
'ToggleGroup' instance, we can group the radio buttons. If you add 'n' radio
buttons to a group 'group1', then at a time one radio button of that group will
be selected.
Ex
ToggleGroup
toggleGroup = new ToggleGroup();
RadioButton
radioButton1 = new RadioButton("Java");
radioButton1.setToggleGroup(toggleGroup);
radioButton1.setSelected(true);
RadioButton
radioButton2 = new RadioButton("Python");
radioButton2.setToggleGroup(toggleGroup);
RadioButton
radioButton3 = new RadioButton("Haskell");
radioButton3.setToggleGroup(toggleGroup);
package com.sample.demos; import static javafx.geometry.Pos.CENTER; import java.io.FileNotFoundException; import java.net.MalformedURLException; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class RadioButtonApp extends Application { @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { ToggleGroup toggleGroup = new ToggleGroup(); RadioButton radioButton1 = new RadioButton("Java"); radioButton1.setToggleGroup(toggleGroup); radioButton1.setSelected(true); RadioButton radioButton2 = new RadioButton("Python"); radioButton2.setToggleGroup(toggleGroup); RadioButton radioButton3 = new RadioButton("Haskell"); radioButton3.setToggleGroup(toggleGroup); VBox vBox = new VBox(10, radioButton1, radioButton2, radioButton3); vBox.setAlignment(CENTER); Scene scene = new Scene(vBox, 400, 400); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX Radio 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(RadioButtonApp.class, args); } }
Sunday, 27 May 2018
JavaFX: Add an image to radio button
By using
'setGraphic' method, you can add graphic icon to the radio button.
Ex
RadioButton
radioButton = new RadioButton();
radioButton.setText("Java");
/* Add an
image to radio button */
InputStream
is = new FileInputStream(imageFilePath);
Image
image = new Image(is);
radioButton.setGraphic(new
ImageView(image));
Find the
below working application.
RadioButtonApp.java
package com.sample.demos; import static javafx.geometry.Pos.CENTER; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.net.MalformedURLException; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.RadioButton; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class RadioButtonApp extends Application { private static String imageFilePath = "C:\\Users\\krishna\\Documents\\Study\\javaFX\\javaIcon.png"; @Override public void start(Stage primaryStage) throws FileNotFoundException, MalformedURLException { RadioButton radioButton = new RadioButton(); radioButton.setText("Java"); /* Add an image to radio button */ InputStream is = new FileInputStream(imageFilePath); Image image = new Image(is); radioButton.setGraphic(new ImageView(image)); VBox vBox = new VBox(10, radioButton); vBox.setAlignment(CENTER); Scene scene = new Scene(vBox, 400, 400); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX Radio 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(RadioButtonApp.class, args); } }
Check whether an array is a primitive wrapper array or not
Arrays are objects in java. Every object in java has a class associated with it. By
using the class, we can check whether the array is of primitive type or not.
I
would recommend you go through my tutorial to know about the class of the array.
Below
program checks whether an array is a primitive wrapper array or not.
package com.sample.test; import java.util.HashSet; import java.util.Set; public class Test { private static final Set<Class<?>> PRIMITIVE_WRAPPER_CLASSES = new HashSet<>(); static { loadPrimitiveWrapperClasses(); } private static void loadPrimitiveWrapperClasses() { PRIMITIVE_WRAPPER_CLASSES.add(new Byte[0].getClass()); PRIMITIVE_WRAPPER_CLASSES.add(new Short[0].getClass()); PRIMITIVE_WRAPPER_CLASSES.add(new Integer[0].getClass()); PRIMITIVE_WRAPPER_CLASSES.add(new Long[0].getClass()); PRIMITIVE_WRAPPER_CLASSES.add(new Float[0].getClass()); PRIMITIVE_WRAPPER_CLASSES.add(new Double[0].getClass()); PRIMITIVE_WRAPPER_CLASSES.add(new Boolean[0].getClass()); PRIMITIVE_WRAPPER_CLASSES.add(new Character[0].getClass()); } public static boolean isprimitiveWrapperArray(Class<?> clazz) { if (clazz == null) { throw new IllegalArgumentException("clazz shouldn't be null"); } return PRIMITIVE_WRAPPER_CLASSES.contains(clazz); } private static class Employee { } public static void main(String args[]) { int intArr[] = new int[10]; Integer intWrapperArr[] = new Integer[10]; Employee emps[] = new Employee[10]; System.out.println("is intArr wrapper array? : " + isprimitiveWrapperArray(intArr.getClass())); System.out.println("is intWrapperArr wrapper array? : " + isprimitiveWrapperArray(intWrapperArr.getClass())); System.out.println("is emps wrapper array? : " + isprimitiveWrapperArray(emps.getClass())); } }
Output
is intArr wrapper array? : false is intWrapperArr wrapper array? : true is emps wrapper array? : false
Check whether an array is of primitive type or not
Arrays are objects in java. Every object in java has a class associated with it. By
using the class, we can check whether the array is of primitive type or not.
I
would recommend you go through my tutorial to know about the class of the array.
Is array a primitive
type or object in java?
Below
program checks whether an array is a primitive type array or not.
package com.sample.test; import java.util.HashSet; import java.util.Set; public class Test { private static final Set<Class<?>> PRIMITIVE_CLASSES = new HashSet<>(); static { loadPrimitiveClasses(); } private static void loadPrimitiveClasses() { PRIMITIVE_CLASSES.add(new byte[0].getClass()); PRIMITIVE_CLASSES.add(new short[0].getClass()); PRIMITIVE_CLASSES.add(new int[0].getClass()); PRIMITIVE_CLASSES.add(new long[0].getClass()); PRIMITIVE_CLASSES.add(new float[0].getClass()); PRIMITIVE_CLASSES.add(new double[0].getClass()); PRIMITIVE_CLASSES.add(new boolean[0].getClass()); PRIMITIVE_CLASSES.add(new char[0].getClass()); } public static boolean isprimitiveArray(Class<?> clazz) { if (clazz == null) { throw new IllegalArgumentException("clazz shouldn't be null"); } return PRIMITIVE_CLASSES.contains(clazz); } private static class Employee { } public static void main(String args[]) { int arr[] = new int[10]; Employee emps[] = new Employee[10]; System.out.println("is arr primitive? : " + isprimitiveArray(arr.getClass())); System.out.println("is emps primitive? : " + isprimitiveArray(emps.getClass())); } }
Output
is
arr primitive? : true
is emps primitive? : false
instanceof vs isInstance()
'isInstance()'
method, instanceof operation are used to check whether the specified object is
assignment-compatible with the object represented by this Class. It is the
dynamic equivalent of 'instanceof' operator.
By
using 'isInstance()', instanceof operator, you can check whether the object can
be casted to other type or not.
For
example, let’s see the definition of Integer class, it looks like below.
public
final class Integer extends Number implements Comparable<Integer> {
}
As
you see Integer is a subclass of Number, Comparable, and Object (Object is the
super class for all the classes). Now if you define, an integer variable, you
can assign it to Number, Comparable and Object types. You can check this
compatibility using 'isInstance' method.
Test.java
package com.sample.test; public class Test { public static void main(String args[]) { Integer var1 = new Integer(10); System.out.println("Can I assign var1 to Integer : " + Integer.class.isInstance(var1)); System.out.println("Can I assign var1 to Number : " + Number.class.isInstance(var1)); System.out.println("Can I assign var1 to Comparable : " + Comparable.class.isInstance(var1)); System.out.println("Can I assign var1 to Object : " + Object.class.isInstance(var1)); System.out.println("\nCan I assign var1 to Double : " + Double.class.isInstance(var1)); System.out.println("Can I assign var1 to Float : " + Float.class.isInstance(var1)); System.out.println("\nvar1 instanceof Integer : " + (var1 instanceof Integer)); System.out.println("var1 instanceof Number : " + (var1 instanceof Number)); System.out.println("var1 instanceof Comparable : " + (var1 instanceof Comparable)); System.out.println("var1 instanceof Object : " + (var1 instanceof Object)); } }
Output
Can I assign var1 to Integer : true Can I assign var1 to Number : true Can I assign var1 to Comparable : true Can I assign var1 to Object : true Can I assign var1 to Double : false Can I assign var1 to Float : false var1 instanceof Integer : true var1 instanceof Number : true var1 instanceof Comparable : true var1 instanceof Object : true
instanceof vs
isInstance()
If
you want to check the compatible class of given object at run time, then you
should use isIntance() method.
Find
the below working application.
Test.java
package com.sample.test; public class Test { /** * Return true, if we can assign object to the class represented by * <code>className</clode> * * @param className * @param object * @return */ private static boolean isCompatible(String className, Object object) { try { return Class.forName(className).isInstance(object); } catch (ClassNotFoundException e) { throw new RuntimeException("Class not found " + className + "," + e); } } public static void main(String args[]) { Integer var1 = new Integer(10); System.out.println("is var1 comptible with Integer? : " + isCompatible("java.lang.Integer", var1)); System.out.println("is var1 comptible with Number? : " + isCompatible("java.lang.Number", var1)); System.out.println("is var1 comptible with Comparable? : " + isCompatible("java.lang.Comparable", var1)); System.out.println("\nis var1 comptible with Float? : " + isCompatible("java.lang.Float", var1)); System.out.println("is var1 comptible with Boolean? : " + isCompatible("java.lang.Boolean", var1)); } }
Output
is var1 comptible with Integer? : true is var1 comptible with Number? : true is var1 comptible with Comparable? : true is var1 comptible with Float? : false is var1 comptible with Boolean? : false
Subscribe to:
Posts (Atom)


