Skip to content

Instantly share code, notes, and snippets.

@ZeroPie
Created June 10, 2017 13:27
Show Gist options
  • Save ZeroPie/676f9eb4477597705cb99c3a07cd9d79 to your computer and use it in GitHub Desktop.
Save ZeroPie/676f9eb4477597705cb99c3a07cd9d79 to your computer and use it in GitHub Desktop.
GUI
package sample;
import javafx.fxml.FXML;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.control.ListView;
import javafx.scene.control.Slider;
public class Controller {
@FXML
private ListView<String> myListView;
@FXML
private Slider mySlider;
@FXML
private void initialize() {
initializeList();
//Event Handler
mySlider.valueProperty().addListener((observable, oldValue, newValue) -> {
slideListElement(newValue.intValue());
});
}
private void slideListElement(int index) {
myListView.getSelectionModel().select(index);
myListView.getFocusModel().focus(index);
}
private void initializeList() {
myListView.setOrientation(Orientation.HORIZONTAL);
ObservableList<String> listViewData = FXCollections.observableArrayList ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
myListView.setItems(listViewData);
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("numberSlider.fxml"));
AnchorPane anchorpane = new AnchorPane();
Scene scene = new Scene(anchorpane);
primaryStage.setScene(scene);
primaryStage.setScene(new Scene(root, 400, 300));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane minWidth="300.0" prefHeight="350.0" prefWidth="450.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<VBox id="VBox" layoutX="14.0" layoutY="17.0" spacing="5.0" alignment="CENTER" />
<ListView fx:id="myListView" layoutX="90.0" layoutY="80.0" prefHeight="36.0" prefWidth="253.0" />
<Slider fx:id="mySlider" layoutX="90.0" layoutY="182.0" max="9.0" prefHeight="12.0" prefWidth="253.0" blockIncrement="1.0" />
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment