FXML TableView Cells JavaFX中的硬编码值

时间:2015-02-17 14:46:42

标签: javafx fxml

我一直疯狂地寻找答案。是否可以在FXML TableView中定义硬编码值?我要硬编码的部分是第一列。

<TableView fx:id="calendarPopulator" layoutX="13.0" layoutY="125.0" prefHeight="282.0" prefWidth="474.0">
        <columns>
            <TableColumn editable="false" prefWidth="43.0" text="Tid">
                <cellValueFactory>
                    <PropertyValueFactory property="Hi"/>
                </cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="mandagColumn" editable="false" prefWidth="81.2" sortable="false" text="Mandag" />
            <TableColumn fx:id="tirsdagColumn" editable="false" prefWidth="81.2" sortable="false" text="Tirsdag" />
            <TableColumn fx:id="onsdagColumn" editable="false" prefWidth="81.2" sortable="false" text="Onsdag" />
            <TableColumn fx:id="torsdagColumn" editable="false" prefWidth="81.2" sortable="false" text="Torsdag" />
            <TableColumn fx:id="fredagColumn" editable="false" prefWidth="81.2" sortable="false" text="Fredag" />
        </columns>
      </TableView>

1 个答案:

答案 0 :(得分:4)

只需在控制器中执行此操作即可。在列中添加fx:id

<TableColumn fx:id="tidColumn" editable="false" prefWidth="43.0" text="Tid" />

然后在控制器中:

public class Controller {
    @FXML
    private TableColumn<Something, String> tidColumn ;

    public void initialize() {
        tidColumn.setCellValueFactory(cellData -> new ReadOnlyStringWrapper("Hi"));
    }
}

如果您希望能够在FXML文件中定义“固定”值,我认为您需要在单独的类中创建单元格值工厂实现。例如:

package application;
import javafx.beans.NamedArg;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.util.Callback;


public class FixedValueCellFactory<T> implements Callback<CellDataFeatures<?,T>, ObservableValue<T>> {

    private final ObservableValue<T> valueWrapper ;

    public FixedValueCellFactory(@NamedArg("value") T value) {
        this.valueWrapper = new ReadOnlyObjectWrapper<>(value) ;
    }

    @Override
    public ObservableValue<T> call(CellDataFeatures<?, T> param) {
        return valueWrapper ;
    }

}

然后在你的FXML中你可以做到

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TableColumn?>
<?import application.FixedValueCellFactory?>
<?import javafx.scene.control.cell.PropertyValueFactory?>

<BorderPane xmlns:fx="http://javafx.com/fxml/1" >
    <center>
        <TableView fx:id="table">
            <columns>
                <TableColumn text="fixed">
                    <cellValueFactory>
                        <FixedValueCellFactory value="Hi"/>
                    </cellValueFactory>
                </TableColumn>
                <TableColumn text="Name">
                    <cellValueFactory>
                        <PropertyValueFactory property="name"/>
                    </cellValueFactory>
                </TableColumn>
            </columns>
        </TableView>
    </center>
</BorderPane>

这是一些测试代码:

package application;

import java.io.IOException;
import java.util.Map;
import java.util.stream.IntStream;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class FixedValueCellTest extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("FixedValueTableTest.fxml"));
        primaryStage.setScene(new Scene(loader.load(), 600, 400));

        // should do in controller, this is for brevity:
        Map<String, Object> namespace = loader.getNamespace();
        TableView<Item> table = (TableView<Item>) namespace.get("table");
        IntStream.rangeClosed(1, 20).mapToObj(i -> "Item "+i)
            .map(Item::new).forEach(table.getItems()::add);
        primaryStage.show();
    }

    public static class Item {
        private final StringProperty name = new SimpleStringProperty();
        public Item(String name) {
            setName(name);
        }
        public StringProperty nameProperty() {
            return name ;
        }
        public final String getName() {
            return nameProperty().get();
        }
        public final void setName(String name) {
            nameProperty().set(name);
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}