Created
May 31, 2014 07:18
-
-
Save ryanjaeb/390351632a2d0c3814d9 to your computer and use it in GitHub Desktop.
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.beans.property.ReadOnlyStringProperty; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.beans.property.StringProperty; | |
import javafx.collections.ListChangeListener; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.SelectionMode; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
public class MultiToSingleTableItemSelection extends Application { | |
@Override | |
public void start(final Stage primaryStage) throws Exception { | |
// table | |
final TableView<Item> table = new TableView<>(); | |
table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); | |
// table columns | |
final TableColumn<Item, String> nameColumn = new TableColumn<>("Name"); | |
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); | |
final TableColumn<Item, String> descriptionColumn = new TableColumn<>("Description"); | |
descriptionColumn.setCellValueFactory(new PropertyValueFactory<>("description")); | |
table.getColumns().addAll(nameColumn, descriptionColumn); | |
// selection change handling | |
table.getSelectionModel().getSelectedItems().addListener(this::processListChange); | |
// table items | |
final Item one = new Item(); | |
one.name.set("name one"); | |
one.description.set("description one"); | |
final Item two = new Item(); | |
two.name.set("name two"); | |
two.description.set("description two"); | |
table.getItems().addAll(one, two); | |
// os / version labels | |
Label osLabel = new Label(System.getProperty("os.name")); | |
Label jvmLabel = new Label( | |
System.getProperty("java.version") + | |
"-" + System.getProperty("java.vm.version") + | |
" (" + System.getProperty("os.arch") + ")" | |
); | |
// scene / stage | |
primaryStage.setScene(new Scene( | |
new BorderPane( | |
table, | |
null, | |
null, | |
new VBox(osLabel, jvmLabel), | |
null | |
) | |
)); | |
primaryStage.setWidth(600); | |
primaryStage.setHeight(400); | |
primaryStage.setTitle("Table Item Reselection"); | |
primaryStage.show(); | |
} | |
private void processListChange(ListChangeListener.Change<? extends Item> c) { | |
System.out.println("Change event..."); | |
while (c.next()) { | |
System.out.println(" Change:"); | |
if (c.wasRemoved()) { | |
c.getRemoved().forEach(item -> | |
System.out.println(" Removed: " + item.getName())); | |
} | |
if (c.wasAdded()) { | |
c.getAddedSubList().forEach(item -> | |
System.out.println(" Added: " + item.getName())); | |
} | |
} | |
} | |
public static class Item { | |
private final StringProperty name = new SimpleStringProperty("name"); | |
public final ReadOnlyStringProperty nameProperty() {return name;} | |
public final String getName() {return name.get();} | |
private final StringProperty description = new SimpleStringProperty("description"); | |
public final ReadOnlyStringProperty descriptionProperty() {return description;} | |
public final String getDescription() {return description.get();} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment