Used TableView form ensemble and used a SortedList.
An Exception occurs. It is clear that it is a problem to compare objects which are not comparable, but it should not happen that the application does not start. Why not using a default object comparator in that case?
Exception
SEVERE: javafx.scene.control.Control loadSkinClass Failed to load skin 'com.sun.javafx.scene.control.skin.TableViewSkin' for control TableView@41d05d[styleClass=table-view]
java.lang.ClassCastException: Cannot use natural comparison as underlying list contains element(s) that are not Comparable
at javafx.collections.SortedList.size(SortedList.java:290)
at com.sun.javafx.scene.control.skin.TableViewSkin.getItemCount(TableViewSkin.java:345)
at com.sun.javafx.scene.control.skin.TableViewSkin.updatePlaceholderRegionVisibility(TableViewSkin.java:302)
at com.sun.javafx.scene.control.skin.TableViewSkin.updateVisibleColumnCount(TableViewSkin.java:275)
at com.sun.javafx.scene.control.skin.TableViewSkin.<init>(TableViewSkin.java:93)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at javafx.scene.control.Control.loadSkinClass(Control.java:690)
at javafx.scene.control.Control.impl_cssSet(Control.java:761)
at javafx.scene.Node.impl_cssSet(Node.java:5448)
at com.sun.javafx.css.StyleHelper.transitionToState(StyleHelper.java:280)
at javafx.scene.Node.impl_processCSS(Node.java:5375)
at javafx.scene.Parent.impl_processCSS(Parent.java:943)
at javafx.scene.control.Control.impl_processCSS(Control.java:783)
at javafx.scene.Parent.impl_processCSS(Parent.java:950)
at javafx.stage.Window.impl_visibleChanging(Window.java:554)
at javafx.stage.Stage.impl_visibleChanging(Stage.java:481)
at javafx.stage.Window$12.invalidated(Window.java:462)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:62)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:116)
at javafx.stage.Window.setVisible(Window.java:534)
at javafx.stage.Stage.setVisible(Stage.java:105)
at com.sibvisions.javafx.TableSample.start(TableSample.java:65)
at com.sun.javafx.application.LauncherImpl$3.run(LauncherImpl.java:130)
at com.sun.javafx.application.PlatformImpl$3.run(PlatformImpl.java:87)
at com.sun.javafx.application.PlatformImpl$2.run(PlatformImpl.java:65)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$1$1.run(WinApplication.java:49)
at java.lang.Thread.run(Thread.java:619)
Example:
public static void main(String[] args) {
Application.launch(args);
}
@SuppressWarnings("unchecked")
@Override
public void start(Stage pStage)
{
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com" ),
new Person("Isabella", "Johnson", "isabella.johnson@example.com" ),
new Person("Ethan", "Williams", "ethan.williams@example.com" ),
new Person("Emma", "Jones", "emma.jones@example.com" ),
new Person("Michael", "Brown", "michael.brown@example.com" )
);
TableColumn<String> firstNameCol = new TableColumn<String>();
firstNameCol.setText("First");
firstNameCol.setDataRetriever(new Callback<TableColumn.CellDataFeatures<String>, String>() {
@Override public String call(CellDataFeatures<String> p) {
return ((Person)p.getValue()).firstName;
}
});
TableColumn lastNameCol = new TableColumn();
lastNameCol.setText("Last");
lastNameCol.setDataRetriever(new Callback<TableColumn.CellDataFeatures<String>, String>() {
@Override public String call(CellDataFeatures<String> p) {
return ((Person)p.getValue()).lastName;
}
});
TableColumn emailCol = new TableColumn();
emailCol.setText("Email");
emailCol.setMinWidth(200);
emailCol.setDataRetriever(new Callback<TableColumn.CellDataFeatures<String>, String>() {
@Override public String call(CellDataFeatures<String> p) {
return ((Person)p.getValue()).email;
}
});
TableView tableView = new TableView();
tableView.setItems(new SortedList(data));
tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
BorderPane bpane = new BorderPane();
bpane.setCenter(tableView);
Scene scene = new Scene(bpane);
pStage.setScene(scene);
pStage.setVisible(true);
pStage.setFocused(true);
}
public static class Person {
String firstName;
String lastName;
String email;
private Person(String fName, String lName, String email) {
this.firstName = fName;
this.lastName = lName;
this.email = email;
}
}
An Exception occurs. It is clear that it is a problem to compare objects which are not comparable, but it should not happen that the application does not start. Why not using a default object comparator in that case?
Exception
SEVERE: javafx.scene.control.Control loadSkinClass Failed to load skin 'com.sun.javafx.scene.control.skin.TableViewSkin' for control TableView@41d05d[styleClass=table-view]
java.lang.ClassCastException: Cannot use natural comparison as underlying list contains element(s) that are not Comparable
at javafx.collections.SortedList.size(SortedList.java:290)
at com.sun.javafx.scene.control.skin.TableViewSkin.getItemCount(TableViewSkin.java:345)
at com.sun.javafx.scene.control.skin.TableViewSkin.updatePlaceholderRegionVisibility(TableViewSkin.java:302)
at com.sun.javafx.scene.control.skin.TableViewSkin.updateVisibleColumnCount(TableViewSkin.java:275)
at com.sun.javafx.scene.control.skin.TableViewSkin.<init>(TableViewSkin.java:93)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at javafx.scene.control.Control.loadSkinClass(Control.java:690)
at javafx.scene.control.Control.impl_cssSet(Control.java:761)
at javafx.scene.Node.impl_cssSet(Node.java:5448)
at com.sun.javafx.css.StyleHelper.transitionToState(StyleHelper.java:280)
at javafx.scene.Node.impl_processCSS(Node.java:5375)
at javafx.scene.Parent.impl_processCSS(Parent.java:943)
at javafx.scene.control.Control.impl_processCSS(Control.java:783)
at javafx.scene.Parent.impl_processCSS(Parent.java:950)
at javafx.stage.Window.impl_visibleChanging(Window.java:554)
at javafx.stage.Stage.impl_visibleChanging(Stage.java:481)
at javafx.stage.Window$12.invalidated(Window.java:462)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:62)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:116)
at javafx.stage.Window.setVisible(Window.java:534)
at javafx.stage.Stage.setVisible(Stage.java:105)
at com.sibvisions.javafx.TableSample.start(TableSample.java:65)
at com.sun.javafx.application.LauncherImpl$3.run(LauncherImpl.java:130)
at com.sun.javafx.application.PlatformImpl$3.run(PlatformImpl.java:87)
at com.sun.javafx.application.PlatformImpl$2.run(PlatformImpl.java:65)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$1$1.run(WinApplication.java:49)
at java.lang.Thread.run(Thread.java:619)
Example:
public static void main(String[] args) {
Application.launch(args);
}
@SuppressWarnings("unchecked")
@Override
public void start(Stage pStage)
{
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com" ),
new Person("Isabella", "Johnson", "isabella.johnson@example.com" ),
new Person("Ethan", "Williams", "ethan.williams@example.com" ),
new Person("Emma", "Jones", "emma.jones@example.com" ),
new Person("Michael", "Brown", "michael.brown@example.com" )
);
TableColumn<String> firstNameCol = new TableColumn<String>();
firstNameCol.setText("First");
firstNameCol.setDataRetriever(new Callback<TableColumn.CellDataFeatures<String>, String>() {
@Override public String call(CellDataFeatures<String> p) {
return ((Person)p.getValue()).firstName;
}
});
TableColumn lastNameCol = new TableColumn();
lastNameCol.setText("Last");
lastNameCol.setDataRetriever(new Callback<TableColumn.CellDataFeatures<String>, String>() {
@Override public String call(CellDataFeatures<String> p) {
return ((Person)p.getValue()).lastName;
}
});
TableColumn emailCol = new TableColumn();
emailCol.setText("Email");
emailCol.setMinWidth(200);
emailCol.setDataRetriever(new Callback<TableColumn.CellDataFeatures<String>, String>() {
@Override public String call(CellDataFeatures<String> p) {
return ((Person)p.getValue()).email;
}
});
TableView tableView = new TableView();
tableView.setItems(new SortedList(data));
tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
BorderPane bpane = new BorderPane();
bpane.setCenter(tableView);
Scene scene = new Scene(bpane);
pStage.setScene(scene);
pStage.setVisible(true);
pStage.setFocused(true);
}
public static class Person {
String firstName;
String lastName;
String email;
private Person(String fName, String lName, String email) {
this.firstName = fName;
this.lastName = lName;
this.email = email;
}
}