See Related Tutorials:
how to create toggle button in javaswing.
How To Create HelloWord Application In JavaFx Using Netbeans.
Combo box in java-fx
Cosmetic Planet
CODE:
import javafx.application.Application;import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.media.AudioClip;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class mybej extends Application
{
@Override
public void start(Stage primaryStage) {
Group g = new Group();
primaryStage.setTitle("Toggle Button In JavaFx");
final ToggleGroup group = new ToggleGroup();
final ToggleButton tb1 = new ToggleButton("ON");
tb1.setToggleGroup(group);
tb1.setSelected(false);
final Rectangle rect = new Rectangle(145, 50);
rect.setFill(Color.RED);
rect.setLayoutX(40);
rect.setLayoutY(40);
g.getChildren().addAll(tb1,rect);
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
public void changed(ObservableValue<? extends Toggle> ov,
Toggle toggle, Toggle new_toggle) {
if (new_toggle == null)
{
rect.setFill(Color.RED);
tb1.setText("ON");
}
else
{
rect.setFill(Color.BLUE);
tb1.setText("OFF");
}
}
});
Scene sc = new Scene(g,400,300);
primaryStage.setScene(sc);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
0 comments:
Post a Comment