Skip to content

Instantly share code, notes, and snippets.

@varren
Created June 10, 2015 22:03
Show Gist options
  • Save varren/3872d5611a2726271be4 to your computer and use it in GitHub Desktop.
Save varren/3872d5611a2726271be4 to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
private static void swap(int[] array, int one, int two) {
int temp = array[one];
array[one] = array[two];
array[two] = temp;
}
@Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
final TextField textField = new TextField();
textField.getStyleClass().add("textfieldstyle");
Button buttonRemoveAll = new Button("Reset");
buttonRemoveAll.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
textField.getStyleClass().clear();
textField.getStyleClass().add("text-field");
textField.getStyleClass().add("text-input");
System.out.println(textField);
}
});
Button buttonRemoveStyle1 = new Button("Remove style 1");
buttonRemoveStyle1.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
textField.getStyleClass().remove("textfieldstyle");
System.out.println(textField);
}
});
Button buttonAddStyle1 = new Button("Add style 1 (red)");
buttonAddStyle1.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
textField.getStyleClass().add("textfieldstyle");
System.out.println(textField);
}
});
Button buttonRemoveStyle2 = new Button("Remove style 2");
buttonRemoveStyle2.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
textField.getStyleClass().remove("textfieldstyle");
System.out.println(textField);
}
});
Button buttonAddStyle2 = new Button("Add style 2 (green)");
buttonAddStyle2.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
textField.getStyleClass().add("textfieldstyle2");
System.out.println(textField);
}
});
root.getChildren().addAll(textField, buttonAddStyle1, buttonRemoveStyle1 ,buttonAddStyle2,buttonRemoveStyle2, buttonRemoveAll);
Scene scene = new Scene(root, 300,300);
String css = Main.class.getResource("/styles.css").toExternalForm();
scene.getStylesheets().clear();
scene.getStylesheets().add(css);
primaryStage.setScene(scene);
primaryStage.show();
}
}
.textfieldstyle{
-fx-background-color: red;
}
.textfieldstyle2{
-fx-background-color: green;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment