Javafx 2 JavaFX:TableView:默认排序列的箭头

Javafx 2 JavaFX:TableView:默认排序列的箭头,javafx-2,tableview,sorted,tablecolumn,Javafx 2,Tableview,Sorted,Tablecolumn,我有一个简单的JavaFX应用程序,它有一个表视图。表视图显示按特定顺序排序的一些数据。是否有方法显示箭头(默认情况下)指示数据已按该顺序排序 我的代码如下: import java.util.Collections; import javafx.application.Application; import javafx.beans.property.SimpleIntegerProperty; import javafx.beans.property.SimpleStringPropert

我有一个简单的JavaFX应用程序,它有一个表视图。表视图显示按特定顺序排序的一些数据。是否有方法显示箭头(默认情况下)指示数据已按该顺序排序

我的代码如下:

import java.util.Collections;

import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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 TableViewSample extends Application
{

private TableView<Person>            tableView = new TableView<Person>();
private final ObservableList<Person> data      = FXCollections.observableArrayList(new Person(
                                                   "Allan", "Smith", "allan.smith@example.com",
                                                   22), new Person("Zombie", "Jack",
                                                   "zombie.jack@test.com", 23), new Person(
                                                   "Michael", "Rock", "michael.rock@yahoo.com",
                                                   24), new Person("Best", "Jones",
                                                   "best.jones@example.com", 11), new Person(
                                                   "Michael", "Brown",
                                                   "michael.brown@example.com", 14));

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(650);
    stage.setHeight(500);

    final Label label = new Label("Roster");
    label.setFont(new Font("Arial", 10));

    tableView.setEditable(true);

    TableColumn firstNameCol = new TableColumn("First Name");
    firstNameCol.setMinWidth(100);
    firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));


    TableColumn lastNameCol = new TableColumn("Last Name");
    lastNameCol.setMinWidth(100);
    lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));

    TableColumn emailCol = new TableColumn("Email");
    emailCol.setMinWidth(200);
    emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));

    TableColumn ageCol = new TableColumn("Age");
    ageCol.setMinWidth(200);
    ageCol.setCellValueFactory(new PropertyValueFactory<Person, Integer>("age"));

    Collections.sort(data);

    tableView.setItems(data);
    tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol, ageCol);


    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.getChildren().addAll(label, tableView);

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    stage.setScene(scene);
    stage.show();
}
public static class Person implements Comparable<Person>
{

    private final SimpleStringProperty  firstName;
    private final SimpleStringProperty  lastName;
    private final SimpleStringProperty  email;
    private final SimpleIntegerProperty age;


    private Person(String fName, String lName, String email, int age)
    {
        this.firstName = new SimpleStringProperty(fName);
        this.lastName = new SimpleStringProperty(lName);
        this.email = new SimpleStringProperty(email);
        this.age = new SimpleIntegerProperty(age);
    }

    public SimpleIntegerProperty getAge()
    {
        return age;
    }

    public void setAge(int passedAge)
    {
        age.set(passedAge);
    }

    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 int compareTo(Person o)
    {
        return firstName.get().compareTo(o.getFirstName());
    }
}
}
import java.util.Collections;
导入javafx.application.application;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入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;
公共类TableViewSample扩展了应用程序
{
private TableView TableView=new TableView();
私人最终ObservableList数据=FXCollections.observableArrayList(新人(
“艾伦”、“史密斯”、“艾伦”。smith@example.com",
22)、新人(“僵尸”、“杰克”,
“僵尸。jack@test.com“,23岁),新人(
“迈克尔”,“摇滚”,“迈克尔”。rock@yahoo.com",
24),新人(“最佳”,“琼斯”,
“最好。jones@example.com“,11),新人(
“迈克尔”,“布朗”,
“迈克尔。brown@example.com", 14));
公共静态void main(字符串[]args)
{
发射(args);
}
@凌驾
公众假期开始(阶段)
{
场景=新场景(新组());
stage.setTitle(“表格视图示例”);
舞台设置宽度(650);
舞台设置高度(500);
最终标签=新标签(“名册”);
label.setFont(新字体(“Arial”,10));
tableView.setEditable(true);
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”));
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(新属性ValueFactory(“lastName”));
TableColumn emailCol=新的TableColumn(“电子邮件”);
设置最小宽度(200);
emailCol.setCellValueFactory(新属性ValueFactory(“电子邮件”);
TableColumn ageCol=新的TableColumn(“年龄”);
ageCol.设置最小宽度(200);
ageCol.setCellValueFactory(新属性值工厂(“年龄”));
收集、分类(数据);
tableView.setItems(数据);
tableView.getColumns().addAll(firstNameCol、lastNameCol、emailCol、ageCol);
最终VBox VBox=新的VBox();
vbox.setspace(5);
vbox.getChildren().addAll(标签,tableView);
((组)scene.getRoot()).getChildren().addAll(vbox);
舞台场景;
stage.show();
}
公共静态类人员实现可比性
{
私有最终SimpleStringProperty名字;
私有最终SimpleStringProperty姓氏;
私人最终SimpleStringProperty电子邮件;
私人最终单一财产年龄;
私人(字符串fName、字符串lName、字符串电子邮件、整数)
{
this.firstName=新的SimpleStringProperty(fName);
this.lastName=新的SimpleStringProperty(lName);
this.email=新的SimpleStringProperty(电子邮件);
this.age=新的SimpleIntegerProperty(age);
}
公共SimpleIntegerProperty getAge()
{
回归年龄;
}
公共无效设置(int passedAge)
{
年龄组(通过年龄);
}
公共字符串getFirstName()
{
返回firstName.get();
}
public void setFirstName(字符串fName)
{
firstName.set(fName);
}
公共字符串getLastName()
{
返回lastName.get();
}
public void setLastName(字符串fName)
{
lastName.set(fName);
}
公共字符串getEmail()
{
返回email.get();
}
public void setEmail(字符串fName)
{
email.set(fName);
}
公共内部比较(o人)
{
返回firstName.get().compareTo(o.getFirstName());
}
}
}
试试看

另请参见以下讨论:

...
firstNameCol.setSortType(TableColumn.SortType.ASCENDING);
...
tableView.setItems(data);
tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol, ageCol);
tableView.getSortOrder().add(firstNameCol);
...