Created
December 2, 2013 21:02
-
-
Save james-d/7758918 to your computer and use it in GitHub Desktop.
TableView with context menu applied to table rows. Based on the example in the tutorial at http://docs.oracle.com/javafx/2/ui_controls/table-view.htm#CJAGAAEE. (I don't necessarily like the style of some of this, but wanted to keep changes to the standard example minimal.)
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 java.util.Arrays; | |
import javafx.application.Application; | |
import javafx.beans.binding.Bindings; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Insets; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.ContextMenu; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.MenuItem; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableRow; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.text.Font; | |
import javafx.stage.Stage; | |
import javafx.util.Callback; | |
public class TableViewSample extends Application { | |
private TableView<Person> table = new TableView<Person>(); | |
private final ObservableList<Person> data = | |
FXCollections.observableArrayList( | |
new Person("Jacob", "Smith", "[email protected]"), | |
new Person("Isabella", "Johnson", "[email protected]"), | |
new Person("Ethan", "Williams", "[email protected]"), | |
new Person("Emma", "Jones", "[email protected]"), | |
new Person("Michael", "Brown", "[email protected]") | |
); | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage stage) { | |
Scene scene = new Scene(new Group()); | |
stage.setTitle("Table View Sample"); | |
stage.setWidth(450); | |
stage.setHeight(500); | |
final Label label = new Label("Address Book"); | |
label.setFont(new Font("Arial", 20)); | |
table.setEditable(true); | |
TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name"); | |
firstNameCol.setMinWidth(100); | |
firstNameCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("firstName")); | |
TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name"); | |
lastNameCol.setMinWidth(100); | |
lastNameCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("lastName")); | |
TableColumn<Person, String> emailCol = new TableColumn<>("Email"); | |
emailCol.setMinWidth(200); | |
emailCol.setCellValueFactory( | |
new PropertyValueFactory<Person, String>("email")); | |
table.setItems(data); | |
table.getColumns().addAll(Arrays.asList(firstNameCol, lastNameCol, emailCol)); | |
table.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() { | |
@Override | |
public TableRow<Person> call(TableView<Person> tableView) { | |
final TableRow<Person> row = new TableRow<>(); | |
final ContextMenu contextMenu = new ContextMenu(); | |
final MenuItem removeMenuItem = new MenuItem("Remove"); | |
removeMenuItem.setOnAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent event) { | |
table.getItems().remove(row.getItem()); | |
} | |
}); | |
contextMenu.getItems().add(removeMenuItem); | |
// Set context menu on row, but use a binding to make it only show for non-empty rows: | |
row.contextMenuProperty().bind( | |
Bindings.when(row.emptyProperty()) | |
.then((ContextMenu)null) | |
.otherwise(contextMenu) | |
); | |
return row ; | |
} | |
}); | |
final VBox vbox = new VBox(); | |
vbox.setSpacing(5); | |
vbox.setPadding(new Insets(10, 0, 0, 10)); | |
vbox.getChildren().addAll(label, table); | |
((Group) scene.getRoot()).getChildren().addAll(vbox); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
public static class Person { | |
private final SimpleStringProperty firstName; | |
private final SimpleStringProperty lastName; | |
private final SimpleStringProperty email; | |
private Person(String fName, String lName, String email) { | |
this.firstName = new SimpleStringProperty(fName); | |
this.lastName = new SimpleStringProperty(lName); | |
this.email = new SimpleStringProperty(email); | |
} | |
public String getFirstName() { | |
return firstName.get(); | |
} | |
public void setFirstName(String fName) { | |
firstName.set(fName); | |
} | |
public String getLastName() { | |
return lastName.get(); | |
} | |
public void setLastName(String fName) { | |
lastName.set(fName); | |
} | |
public String getEmail() { | |
return email.get(); | |
} | |
public void setEmail(String fName) { | |
email.set(fName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, it's working like a charm, good job.