Javafx Tableview整数排序出现错误?

Javafx Tableview整数排序出现错误?,javafx,tableview,Javafx,Tableview,我有一个tableview,其中一列由整数值组成: 一, 444 -九, 如果我在这个列上排序,我希望444是“最高”的数字,因此是第一行,但是JavaFX认为9更高 有什么建议吗 MVCE: 公共类TableViewTEST扩展应用程序{ private TableView table=new TableView(); 最终ObservableList数据=FXCollections.observableArrayList( 新人(“1”、“史密斯”、“雅各布”。smith@example

我有一个tableview,其中一列由整数值组成:

  • 一,
  • 444 -九,
如果我在这个列上排序,我希望444是“最高”的数字,因此是第一行,但是JavaFX认为9更高

有什么建议吗

MVCE:

公共类TableViewTEST扩展应用程序{
private TableView table=new TableView();
最终ObservableList数据=FXCollections.observableArrayList(
新人(“1”、“史密斯”、“雅各布”。smith@example.com"),
新人(“9”,“约翰逊”,“伊莎贝拉”。johnson@example.com"),
新人(“444”,“威廉姆斯”,“伊桑”。williams@example.com")
);
@凌驾
公众假期开始(阶段){
table.getItems().addAll(数据);
场景=新场景(新组());
stage.setTitle(
“表格视图样本”);
舞台布景宽度(
300);
舞台布景高度(
500);
最终标签=新标签(“地址簿”);
label.setFont(
新字体(“Arial”,20));
TableColumn firstNameCol=新的TableColumn(“第一”);
TableColumn lastNameCol=新的TableColumn(“姓氏”);
TableColumn emailCol=新的TableColumn(“电子邮件”);
firstNameCol.setCellValueFactory(
新PropertyValueFactory(“名字”)
);
lastNameCol.setCellValueFactory(
新PropertyValueFactory(“姓氏”)
);
emailCol.setCellValueFactory(
新PropertyValueFactory(“电子邮件”)
);
表1.getColumns()
.addAll(firstNameCol、lastNameCol、emailCol);
最终VBox VBox=新的VBox();
vbox.setspace(
5);
vbox.setPadding(
新插图(10,0,0,10));
vbox.getChildren()
.addAll(标签、表格);
((组)scene.getRoot()).getChildren().addAll(vbox);
舞台场景;
stage.show();
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
发射(args);
}
}
编辑


好的,我发现这是因为我在模型中使用了字符串。。因此,更新后的问题是,如何在表列上创建新的排序功能,以便即使其中有字符串,也能将其作为整数排序

您的值是
String
s,因此它们按字典顺序排序;由于字符“4”位于字符“9”之前,“444”位于字符“9”之前

如果将这些字段设置为整数字段,则它们将按数字排序。在这里,使用正确键入的
TableView
s和
TableColumn
s,而不是原始类型是很有帮助的

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
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;

public class TableViewSortTest extends Application {

private TableView<Person> table = new TableView<>();
final ObservableList<Person> data = FXCollections.observableArrayList(
        new Person(1, "Smith", "jacob.smith@example.com"),
        new Person(9, "Johnson", "isabella.johnson@example.com"),
        new Person(444, "Williams", "ethan.williams@example.com")

);

@Override
public void start(Stage stage) {
    table.getItems().addAll(data);

    Scene scene = new Scene(new Group());

    stage.setTitle(
            "Table View Sample");
    stage.setWidth(
            300);
    stage.setHeight(
            500);

    final Label label = new Label("Address Book");

    label.setFont(
            new Font("Arial", 20));

    TableColumn<Person, Integer> idCol = new TableColumn<>("Id");
    TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
    TableColumn<Person, String> emailCol = new TableColumn<>("Email");

    idCol.setCellValueFactory(
            new PropertyValueFactory<Person, Integer>("id")
    );
    lastNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("name")
    );
    emailCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("email")
    );

    table.getColumns()
            .addAll(idCol, lastNameCol, emailCol);

    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 IntegerProperty id = new SimpleIntegerProperty(this, "id");
    private final StringProperty name = new SimpleStringProperty(this, "name");
    private final StringProperty email = new SimpleStringProperty(this, "email");

    public Person(int id, String name, String email) {
        this.id.set(id);
        this.name.set(name);
        this.email.set(email);
    }

    public final IntegerProperty idProperty() {
        return this.id;
    }

    public final int getId() {
        return this.idProperty().get();
    }

    public final void setId(final int id) {
        this.idProperty().set(id);
    }

    public final StringProperty nameProperty() {
        return this.name;
    }

    public final java.lang.String getName() {
        return this.nameProperty().get();
    }

    public final void setName(final java.lang.String name) {
        this.nameProperty().set(name);
    }

    public final StringProperty emailProperty() {
        return this.email;
    }

    public final java.lang.String getEmail() {
        return this.emailProperty().get();
    }

    public final void setEmail(final java.lang.String email) {
        this.emailProperty().set(email);
    }


}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}
导入javafx.application.application;
导入javafx.beans.property.IntegerProperty;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.geometry.Insets;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.Font;
导入javafx.stage.stage;
公共类TableViewSortTest扩展了应用程序{
private TableView table=new TableView();
最终ObservableList数据=FXCollections.observableArrayList(
新人(1),“史密斯”,“雅各布”。smith@example.com"),
新人(9),“约翰逊”,“伊莎贝拉。johnson@example.com"),
新人(444),“威廉姆斯”,“伊桑。williams@example.com")
);
@凌驾
公众假期开始(阶段){
table.getItems().addAll(数据);
场景=新场景(新组());
stage.setTitle(
“表格视图样本”);
舞台布景宽度(
300);
舞台布景高度(
500);
最终标签=新标签(“地址簿”);
label.setFont(
新字体(“Arial”,20));
TableColumn idCol=新的TableColumn(“Id”);
TableColumn lastNameCol=新的TableColumn(“姓氏”);
TableColumn emailCol=新的TableColumn(“电子邮件”);
idCol.setCellValueFactory(
新PropertyValueFactory(“id”)
);
lastNameCol.setCellValueFactory(
新PropertyValue工厂(“名称”)
);
emailCol.setCellValueFactory(
新PropertyValueFactory(“电子邮件”)
);
表1.getColumns()
.addAll(idCol、lastNameCol、emailCol);
最终VBox VBox=新的VBox();
vbox.setspace(
5);
vbox.setPadding(
新插图(10,0,0,10));
vbox.getChildren()
.addAll(标签、表格);
((组)scene.getRoot()).getChildren().addAll(vbox);
舞台场景;
stage.show();
}
公共静态类人员{
private final IntegerProperty id=新的SimpleIntegerProperty(该“id”);
私有最终StringProperty名称=新SimpleStringProperty(此“名称”);
私有最终StringProperty电子邮件=新SimpleStringProperty(本“电子邮件”);
公众人物(整数id、字符串名称、字符串电子邮件){
此.id.set(id);
this.name.set(name);
this.email.set(email);
}
公共最终整数属性idProperty(){
返回此.id;
}
公共final int getId(){
返回此.idProperty().get();
}
公共最终无效集合id(最终内部id){
此.idProperty().set(id);
}
公共最终字符串属性nameProperty(){
返回此.name;
}
public final java.lang.String getName(){
返回此.nameProperty().get();
}
public final void setName(final java.lang.String名称){
this.nameProperty().set(name);
}
公共最终StringProperty emailProperty(){
返回此电子邮件;
}
public final java.lang.String getEmail(){
返回此.emailProperty().get();
}
公共最终void setEmail(最终java.lang.String电子邮件){
this.emailProperty().set(电子邮件);
}
}
/**
*
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
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;

public class TableViewSortTest extends Application {

private TableView<Person> table = new TableView<>();
final ObservableList<Person> data = FXCollections.observableArrayList(
        new Person(1, "Smith", "jacob.smith@example.com"),
        new Person(9, "Johnson", "isabella.johnson@example.com"),
        new Person(444, "Williams", "ethan.williams@example.com")

);

@Override
public void start(Stage stage) {
    table.getItems().addAll(data);

    Scene scene = new Scene(new Group());

    stage.setTitle(
            "Table View Sample");
    stage.setWidth(
            300);
    stage.setHeight(
            500);

    final Label label = new Label("Address Book");

    label.setFont(
            new Font("Arial", 20));

    TableColumn<Person, Integer> idCol = new TableColumn<>("Id");
    TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
    TableColumn<Person, String> emailCol = new TableColumn<>("Email");

    idCol.setCellValueFactory(
            new PropertyValueFactory<Person, Integer>("id")
    );
    lastNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("name")
    );
    emailCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("email")
    );

    table.getColumns()
            .addAll(idCol, lastNameCol, emailCol);

    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 IntegerProperty id = new SimpleIntegerProperty(this, "id");
    private final StringProperty name = new SimpleStringProperty(this, "name");
    private final StringProperty email = new SimpleStringProperty(this, "email");

    public Person(int id, String name, String email) {
        this.id.set(id);
        this.name.set(name);
        this.email.set(email);
    }

    public final IntegerProperty idProperty() {
        return this.id;
    }

    public final int getId() {
        return this.idProperty().get();
    }

    public final void setId(final int id) {
        this.idProperty().set(id);
    }

    public final StringProperty nameProperty() {
        return this.name;
    }

    public final java.lang.String getName() {
        return this.nameProperty().get();
    }

    public final void setName(final java.lang.String name) {
        this.nameProperty().set(name);
    }

    public final StringProperty emailProperty() {
        return this.email;
    }

    public final java.lang.String getEmail() {
        return this.emailProperty().get();
    }

    public final void setEmail(final java.lang.String email) {
        this.emailProperty().set(email);
    }


}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}
firstNameCol.setComparator(new CustomComparator());
private class CustomComparator implements Comparator<String>{

    @Override
    public int compare(String o1, String o2) {
        if (o1 == null && o2 == null) return 0;
        if (o1 == null) return -1;
        if (o2 == null) return 1;

        Integer i1=null;
        try{ i1=Integer.valueOf(o1); } catch(NumberFormatException ignored){}
        Integer i2=null;
        try{ i2=Integer.valueOf(o2); } catch(NumberFormatException ignored){}

        if(i1==null && i2==null) return o1.compareTo(o2);
        if(i1==null) return -1;
        if(i2==null) return 1;

        return i1-i2;
    }
}