Skip to content

Instantly share code, notes, and snippets.

@senleft
Created January 16, 2014 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senleft/8455861 to your computer and use it in GitHub Desktop.
Save senleft/8455861 to your computer and use it in GitHub Desktop.
package tableviewsample.swing;
import javafx.application.Platform;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.util.Callback;
import javafx.util.converter.DateTimeStringConverter;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.HeadlessException;
import java.util.Date;
public class SwingTableViewSample extends JFrame {
public static final String EMPTY_STRING = "";
private TableView<Person> table = new TableView<>();
private Person person;
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com", new Date()),
new Person("Isabella", "Johnson", "isabella.johnson@example.com", new Date()),
new Person("Ethan", "Williams", "ethan.williams@example.com", new Date()),
new Person("Emma", "Jones", "emma.jones@example.com", new Date()),
new Person("Michael", "Brown", "michael.brown@example.com", new Date()));
final HBox hb = new HBox();
private TextField firstNameField;
private TextField lastNameField;
private TextField emailField;
private JFXPanel jfxPanel = new JFXPanel();
private VBox vbox = new VBox();
private Label label;
public SwingTableViewSample() throws HeadlessException {
super("Table View Sample");
setSize(650, 550);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(jfxPanel);
Platform.runLater(new Runnable() {
@Override
public void run() {
initComponents();
}
});
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SwingTableViewSample();
}
});
}
private void initComponents() {
Scene scene = new Scene(new Group());
label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName"));
firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
firstNameCol.setOnEditCommit(
new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
(t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setFirstName(t.getNewValue());
}
}
);
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));
lastNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
lastNameCol.setOnEditCommit(
new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
(t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setLastName(t.getNewValue());
}
}
);
TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));
emailCol.setCellFactory(TextFieldTableCell.forTableColumn());
emailCol.setOnEditCommit(
new EventHandler<CellEditEvent<Person, String>>() {
@Override
public void handle(CellEditEvent<Person, String> t) {
(t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setEmail(t.getNewValue());
}
}
);
TableColumn<Person, Date> modificationDateCol = new TableColumn<>("Modified");
modificationDateCol.setMinWidth(200);
modificationDateCol.setCellValueFactory(
new PropertyValueFactory<Person, Date>("modificationDate"));
modificationDateCol.setCellFactory(TextFieldTableCell.<Person, Date>forTableColumn(new DateTimeStringConverter("dd.MM.yyyy HH:mm:ss")));
Callback<TableColumn, TableCell> cellFactory = new TableCallback();
firstNameCol.setCellFactory(cellFactory);
lastNameCol.setCellFactory(cellFactory);
emailCol.setCellFactory(cellFactory);
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, modificationDateCol);
firstNameField = new TextField();
firstNameField.setPromptText("First Name");
firstNameField.setMaxWidth(firstNameCol.getPrefWidth());
lastNameField = new TextField();
lastNameField.setMaxWidth(lastNameCol.getPrefWidth());
lastNameField.setPromptText("Last Name");
emailField = new TextField();
emailField.setMaxWidth(emailCol.getPrefWidth());
emailField.setPromptText("Email");
final Button addButton = new Button("Save");
addButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
String text = firstNameField.getText();
String newFirstName = EMPTY_STRING.equals(text) ? firstNameField.getPromptText() : text;
text = lastNameField.getText();
String newLastName = EMPTY_STRING.equals(text) ? lastNameField.getPromptText() : text;
text = emailField.getText();
String newEmail = EMPTY_STRING.equals(text) ? emailField.getPromptText() : text;
if (person == null) {
data.add(new Person(
newFirstName,
newLastName,
newEmail,
new Date()));
firstNameField.clear();
lastNameField.clear();
emailField.clear();
} else {
person.setEmail(newEmail);
person.setFirstName(newFirstName);
person.setLastName(newLastName);
person.setModificationDate(new Date());
}
openTable();
}
});
hb.getChildren().addAll(firstNameField, lastNameField, emailField, addButton);
hb.setSpacing(3);
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
jfxPanel.setScene(scene);
}
private void openTable() {
Platform.runLater(new Runnable() {
@Override
public void run() {
ObservableList<Node> children = vbox.getChildren();
children.clear();
children.addAll(label, table);
}
});
}
private void hideTable() {
Platform.runLater(new Runnable() {
@Override
public void run() {
ObservableList<Node> children = vbox.getChildren();
children.clear();
children.addAll(label, hb);
}
});
}
private class TableCallback implements Callback<TableColumn, TableCell> {
@Override
public TableCell call(TableColumn tableColumn) {
TableCell cell = new TableCell() {
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : getString());
setGraphic(null);
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
};
cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new TableMouseEventHandler());
return cell;
}
}
private class TableMouseEventHandler implements EventHandler<MouseEvent> {
@Override
public void handle(MouseEvent event) {
if (event.getClickCount() == 1
&& event.getButton().compareTo(MouseButton.PRIMARY) == 0) {
handleEvent(event);
} else if (event.getClickCount() > 1
&& event.getButton().compareTo(MouseButton.PRIMARY) == 0) {
hideTable();
handleEvent(event);
}
}
private void handleEvent(MouseEvent event) {
TableCell selectedCell = (TableCell) event.getSource();
person = (Person) selectedCell.getTableRow().getItem();
if (person != null) {
populateFields();
} else {
clearFields();
}
}
private void clearFields() {
firstNameField.clear();
lastNameField.clear();
emailField.clear();
}
}
private void populateFields() {
firstNameField.setText(person.getFirstName());
lastNameField.setText(person.getLastName());
emailField.setText(person.getEmail());
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private final SimpleObjectProperty<Date> modificationDate;
private Person(String fName, String lName, String email, Date modificationDate) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
this.modificationDate = new SimpleObjectProperty<>(modificationDate);
}
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);
}
public Date getModificationDate() {
return modificationDate.get();
}
public SimpleObjectProperty<Date> modificationDateProperty() {
return modificationDate;
}
public void setModificationDate(Date modificationDate) {
this.modificationDate.set(modificationDate);
}
public SimpleStringProperty firstNameProperty() {
return firstName;
}
public SimpleStringProperty lastNameProperty() {
return lastName;
}
public SimpleStringProperty emailProperty() {
return email;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment