gpt4 book ai didi

javafx - Tableview 整数排序有问题吗?

转载 作者:行者123 更新时间:2023-12-02 06:36:53 25 4
gpt4 key购买 nike

我有一个表格 View ,其中一列由整数值组成:

  • 1
  • 444-9

如果我对此列进行排序,我预计 444 是“最高”数字,因此是第一行,但 JavaFX 认为 9 更高?

有什么建议吗?

MVCE:

public class TableViewTEST extends Application {

private TableView 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 firstNameCol = new TableColumn("First");
TableColumn lastNameCol = new TableColumn("Last Name");
TableColumn emailCol = new TableColumn("Email");

firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName")
);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName")
);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email")
);

table.getColumns()
.addAll(firstNameCol, 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();
}

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

}

编辑

好吧,我发现这是因为我在模型中使用了 String 。所以更新的问题是,如何在表列上创建一个新的排序功能,以便它按整数排序,即使有字符串在里面吗?

最佳答案

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

如果您将这些字段设置为整数字段,那么它们将按数字排序。此处使用正确类型的 TableViewTableColumn 而不是原始类型会很有帮助。

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 - Tableview 整数排序有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27062315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com