Created
June 20, 2012 13:13
-
-
Save Zookey/2959838 to your computer and use it in GitHub Desktop.
JavaFX 2 Horizontal ListView Handle Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Orientation; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.ListView; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
/** | |
* | |
* @author zoranpavlovic.blogspot.com | |
*/ | |
public class ListViewMain extends Application { | |
/** | |
* @param args | |
* the command line arguments | |
*/ | |
VBox vb = new VBox(); | |
Label lbl = new Label(); | |
ListView<String> list = new ListView<>(); | |
ObservableList<String> data = FXCollections.observableArrayList( | |
"List index 0", "List index 1", "List index 2", "List index 3"); | |
public static void main(String[] args) { | |
Application.launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) { | |
primaryStage.setTitle("JavaFX 2.0 ListView"); | |
// Adding data to ListView | |
list.setItems(data); | |
// Set Horizontal orientation of this ListView | |
list.setOrientation(Orientation.HORIZONTAL); | |
// Actions on clicked list item | |
initActions(); | |
// Adding BorderPane to the scene | |
vb.getChildren().addAll(list, lbl); | |
Scene scene = new Scene(vb, 200, 60); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
public void initActions() { | |
// Detecting mouse clicked | |
list.setOnMouseClicked(new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent arg0) { | |
// Check wich list index is selected then set txtContent value | |
// for that index | |
if (list.getSelectionModel().getSelectedIndex() == 0) { | |
lbl.setText("Selected index: 0"); | |
} | |
else if (list.getSelectionModel().getSelectedIndex() == 1) { | |
lbl.setText("Selected index: 1"); | |
} | |
else if (list.getSelectionModel().getSelectedIndex() == 2) { | |
lbl.setText("Selected index: 2"); | |
} | |
else if (list.getSelectionModel().getSelectedIndex() == 3) { | |
lbl.setText("Selected index: 3"); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment