将javafx中的Integer tableview显示为字符串

我试图显示一个tableview,其中当表中的值是例如1的整数时,我想显示一个String。到目前为止,我试图获得像这样的cellValue:

public void changeView() {
        if(intervall.getcellFactory().call(intervall).equals(1)) {
            intervall.getcellFactory().call(intervall).setText("Täglich");
        }
    }

然后在设置单元格值后在初始化中调用该方法。

public void initializetable() {

        try {

            // Ablesen
            Statement pStatement = connection.createStatement();
            flightList = FXCollections.observableArrayList();
            ResultSet myRs = pStatement
                    .executeQuery("select * from fluglinie where fluggesellschaft =\"" + fluggesellschaft + "\"");
            while (myRs.next()) {
                flightList.add(new flightRouteAddmodel(myRs.getString("startFlughafen"),myRs.getString("zielFlughafen"),myRs.getString("startDatum"),myRs.getString("flugzeug"),myRs.getInt("intervall"),myRs.getInt("anzahlEconomy"),myRs.getInt("anzahlBusiness"),myRs.getFloat("preisEconomy"),myRs.getFloat("preisBusines"),myRs.getFloat("distanz")));

            }

        } catch (Exception e) {
            System.out.println(e);

        }


        startairport.setCellValueFactory(new PropertyValueFactory<>("startairport"));
        targetairport.setCellValueFactory(new PropertyValueFactory<>("targetairport"));
        flightDate.setCellValueFactory(new PropertyValueFactory<>("flightDate"));
        airplane.setCellValueFactory(new PropertyValueFactory<>("airPlane"));
        intervall.setCellValueFactory(new PropertyValueFactory<>("intervall"));
        seatCountEco.setCellValueFactory(new PropertyValueFactory<>("countEconomy"));
        seatCountBus.setCellValueFactory(new PropertyValueFactory<>("countBusiness"));
        priceEco.setCellValueFactory(new PropertyValueFactory<>("priceEconomy"));
        priceBus.setCellValueFactory(new PropertyValueFactory<>("priceBusiness"));
        distance.setCellValueFactory(new PropertyValueFactory<>("distance"));

        table.setItems(flightList);
        changeView();

    }

但是没有用,有人可以看看吗?我知道更改数据库可能是一个更好的解决方案,但我有点想尝试这种解决方法

zqf123456zqf 回答:将javafx中的Integer tableview显示为字符串

cellFactory返回TableCell。自己调用它不会导致某个单元格成为TableView的一部分(或成为其一部分)。如果传递了1(或任何其他TableCell),则任何带有适当equals方法的true都不会产生Integer

假设您总是要显示Täglich而不是1,那么解决方法是为此列使用自定义cellFactory

intervall.setCellFactory(col -> new TableCell<flightRouteAddModel,Integer>() {

    @Override
    protected void updateItem(Integer item,boolean empty) {
        String text = "";
        if (item != null) {
            switch(item) {
            case 1:
                text = "Täglich";
                break;
            case 7:
                text = "Wöchentlich";
                break;
            default:
                text = String.format("Alle %d Tage",item);
                break;
            }
        }
        setText(text);
    }

});

顺便说一句:请了解Java命名约定。因为所有Java API(据我所知)都使用这些约定,所以这使得代码对于其他人来说更容易阅读,并且甚至应该对于您自己也更具可读性:类型名称以大写字母开头。

本文链接:https://www.f2er.com/3157475.html

大家都在问