设置JavaFX TableView Cells的字体颜色?

发布于 2024-11-28 13:37:42 字数 860 浏览 7 评论 0原文

在我的 Java 桌面应用程序中,我有一个包含 3 列的 JavaFX 表。我想将第三列的字体颜色设置为红色。我根本无法设置 Tableb 的字体颜色。我查看了 CSS,但没有发现任何东西。有没有办法用 CSS 来做到这一点?我还寻找了 setFont(),希望能这样设置。那里什么也没有。我什至无法找到在某个单元格上设置某些内容的方法。

TableView<TableData> myTable = new TableView<TableData>();
ObservableList<TableData> myTableData = FXCollections.observableArreyList(
    new TableData("data", "data", "data"),
    new TableData("data", "data", "data"));

TableColumn firstColumn = new TableColumn("First Column");
firstColumn.setProperty("one");
TableColumn secondColumn = new TableColumn("Second Column");
secondColumn .setProperty("two");
TableColumn thirdColumn = new TableColumn("Third Column");
thirdColumn .setProperty("three");

myTable.setItems(myTableData);
myTable.getColumns.addAll(firstColumn, secondColumn, thirdColumn);

我怎样才能做到这一点?如何设置字体颜色?任何帮助将不胜感激。

In my Java Desktop Application I have a JavaFX Table with 3 columns. I want to set the font color of the 3rd column to red. I have not been able to set the font color of the Tableb at all. I looked into CSS and I did not find anything. Is there a way to do it with CSS? I also looked for setFont() with the hope of setting it that way. Nothing there. I could not even figure a way to set something on a certain cell.

TableView<TableData> myTable = new TableView<TableData>();
ObservableList<TableData> myTableData = FXCollections.observableArreyList(
    new TableData("data", "data", "data"),
    new TableData("data", "data", "data"));

TableColumn firstColumn = new TableColumn("First Column");
firstColumn.setProperty("one");
TableColumn secondColumn = new TableColumn("Second Column");
secondColumn .setProperty("two");
TableColumn thirdColumn = new TableColumn("Third Column");
thirdColumn .setProperty("three");

myTable.setItems(myTableData);
myTable.getColumns.addAll(firstColumn, secondColumn, thirdColumn);

How can I accomplish this? How can I set to font color? Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云巢 2024-12-05 13:37:42

您需要重写 CellFactory。

仅第三列的部分代码:

    TableColumn thirdColumn = new TableColumn("Third Column");  
    thirdColumn.setCellValueFactory(new PropertyValueFactory<TableData,String>("three"));

    // ** The TableCell class has the method setTextFill(Paint p) that you 
    // ** need to override the text color
    //   To obtain the TableCell we need to replace the Default CellFactory 
    //   with one that returns a new TableCell instance, 
    //   and @Override the updateItem(String item, boolean empty) method.
    //
    thirdColumn.setCellFactory(new Callback<TableColumn, TableCell>() {
        public TableCell call(TableColumn param) {
            return new TableCell<TableData, String>() {

                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!isEmpty()) {
                        this.setTextFill(Color.RED);
                        // Get fancy and change color based on data
                        if(item.contains("@")) 
                            this.setTextFill(Color.BLUEVIOLET);
                        setText(item);
                    }
                }
            };
        }
    });

整个代码示例:

package tablecelltextcolorexample;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 *
 * @author jKaufmann
 */
public class TableCellTextColorExample extends Application {
public static class TableData {
    SimpleStringProperty one,two,three;
    public TableData(String one, String two, String three) {
        this.one = new SimpleStringProperty(one);
        this.two = new SimpleStringProperty(two);
        this.three = new SimpleStringProperty(three);
    }
    public String getOne() {
        return one.get();
    }

    public void setOne(String one) {
        this.one.set(one);
    }

    public String getThree() {
        return three.get();
    }

    public void setThree(String three) {
        this.three.set(three);
    }

    public String getTwo() {
        return two.get();
    }

    public void setTwo(String two) {
        this.two.set(two);
    }

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

@Override
public void start(Stage stage) {
    VBox vbox = new VBox();
    Scene scene = new Scene(vbox, 200, 200);
    stage.setTitle("Table View - Change color of a particular column");
    stage.setWidth(400);
    stage.setHeight(500);


    TableView<TableData> myTable = new TableView<TableData>();
    ObservableList<TableData> myTableData = FXCollections.observableArrayList(
            new TableData("data", "data", "data"),
            new TableData("data", "data", "data"),
            new TableData("Name the song","867-5309","[email protected]"));  

    TableColumn firstColumn = new TableColumn("First Column"); 
    firstColumn.setCellValueFactory(new PropertyValueFactory<TableData,String>("one"));

    TableColumn secondColumn = new TableColumn("Second Column"); 
    secondColumn.setCellValueFactory(new PropertyValueFactory<TableData,String>("two"));

    TableColumn thirdColumn = new TableColumn("Third Column");  
    thirdColumn.setCellValueFactory(new PropertyValueFactory<TableData,String>("three"));

    // ** The TableCell class has the method setTextFill(Paint p) that you 
    // ** need to override the text color
    //   To obtain the TableCell we need to replace the Default CellFactory 
    //   with one that returns a new TableCell instance, 
    //   and @Override the updateItem(String item, boolean empty) method.
    //
    thirdColumn.setCellFactory(new Callback<TableColumn, TableCell>() {
        public TableCell call(TableColumn param) {
            return new TableCell<TableData, String>() {

                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!isEmpty()) {
                        this.setTextFill(Color.RED);
                        // Get fancy and change color based on data
                        if(item.contains("@")) 
                            this.setTextFill(Color.BLUEVIOLET);
                        setText(item);
                    }
                }
            };
        }
    });

    myTable.setItems(myTableData); 
    myTable.getColumns().addAll(firstColumn, secondColumn, thirdColumn);

    vbox.getChildren().addAll(myTable);
    VBox.setVgrow(myTable, Priority.ALWAYS);

    stage.setScene(scene);
    stage.show();
}
}

You need to override the CellFactory.

Partial code just of the third column:

    TableColumn thirdColumn = new TableColumn("Third Column");  
    thirdColumn.setCellValueFactory(new PropertyValueFactory<TableData,String>("three"));

    // ** The TableCell class has the method setTextFill(Paint p) that you 
    // ** need to override the text color
    //   To obtain the TableCell we need to replace the Default CellFactory 
    //   with one that returns a new TableCell instance, 
    //   and @Override the updateItem(String item, boolean empty) method.
    //
    thirdColumn.setCellFactory(new Callback<TableColumn, TableCell>() {
        public TableCell call(TableColumn param) {
            return new TableCell<TableData, String>() {

                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!isEmpty()) {
                        this.setTextFill(Color.RED);
                        // Get fancy and change color based on data
                        if(item.contains("@")) 
                            this.setTextFill(Color.BLUEVIOLET);
                        setText(item);
                    }
                }
            };
        }
    });

Entire Code Example:

package tablecelltextcolorexample;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 *
 * @author jKaufmann
 */
public class TableCellTextColorExample extends Application {
public static class TableData {
    SimpleStringProperty one,two,three;
    public TableData(String one, String two, String three) {
        this.one = new SimpleStringProperty(one);
        this.two = new SimpleStringProperty(two);
        this.three = new SimpleStringProperty(three);
    }
    public String getOne() {
        return one.get();
    }

    public void setOne(String one) {
        this.one.set(one);
    }

    public String getThree() {
        return three.get();
    }

    public void setThree(String three) {
        this.three.set(three);
    }

    public String getTwo() {
        return two.get();
    }

    public void setTwo(String two) {
        this.two.set(two);
    }

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

@Override
public void start(Stage stage) {
    VBox vbox = new VBox();
    Scene scene = new Scene(vbox, 200, 200);
    stage.setTitle("Table View - Change color of a particular column");
    stage.setWidth(400);
    stage.setHeight(500);


    TableView<TableData> myTable = new TableView<TableData>();
    ObservableList<TableData> myTableData = FXCollections.observableArrayList(
            new TableData("data", "data", "data"),
            new TableData("data", "data", "data"),
            new TableData("Name the song","867-5309","[email protected]"));  

    TableColumn firstColumn = new TableColumn("First Column"); 
    firstColumn.setCellValueFactory(new PropertyValueFactory<TableData,String>("one"));

    TableColumn secondColumn = new TableColumn("Second Column"); 
    secondColumn.setCellValueFactory(new PropertyValueFactory<TableData,String>("two"));

    TableColumn thirdColumn = new TableColumn("Third Column");  
    thirdColumn.setCellValueFactory(new PropertyValueFactory<TableData,String>("three"));

    // ** The TableCell class has the method setTextFill(Paint p) that you 
    // ** need to override the text color
    //   To obtain the TableCell we need to replace the Default CellFactory 
    //   with one that returns a new TableCell instance, 
    //   and @Override the updateItem(String item, boolean empty) method.
    //
    thirdColumn.setCellFactory(new Callback<TableColumn, TableCell>() {
        public TableCell call(TableColumn param) {
            return new TableCell<TableData, String>() {

                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!isEmpty()) {
                        this.setTextFill(Color.RED);
                        // Get fancy and change color based on data
                        if(item.contains("@")) 
                            this.setTextFill(Color.BLUEVIOLET);
                        setText(item);
                    }
                }
            };
        }
    });

    myTable.setItems(myTableData); 
    myTable.getColumns().addAll(firstColumn, secondColumn, thirdColumn);

    vbox.getChildren().addAll(myTable);
    VBox.setVgrow(myTable, Priority.ALWAYS);

    stage.setScene(scene);
    stage.show();
}
}
稳稳的幸福 2024-12-05 13:37:42

我找到了CSS代码。将此 red-column 类应用到您的列中。

.red-column.table-cell {
    -fx-padding: 0.5em;
    -fx-border-color: transparent -fx-box-border transparent transparent;
    -fx-font: 13px "Arial";
    -fx-text-fill: red;
}

你的桌子将会是这样的。

输入图像描述这里

表视图的完整 css 是 此处

I found a CSS code. Apply this red-column class into your column.

.red-column.table-cell {
    -fx-padding: 0.5em;
    -fx-border-color: transparent -fx-box-border transparent transparent;
    -fx-font: 13px "Arial";
    -fx-text-fill: red;
}

Your table will be like this.

enter image description here

Full css for table-view is here.

情泪▽动烟 2024-12-05 13:37:42

代码需要稍加修改:

// Method for displaying data in table
    protected void displayDataInTable(){
        tblColID.setCellValueFactory(new PropertyValueFactory<Person, String>("id"));

        // Table cell coloring
        tblColID.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
            @Override
            public TableCell<Person, String> call(TableColumn<Person, String> param) {
                return new TableCell<Person, String>() {

                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!isEmpty()) {
                            this.setTextFill(Color.RED);
                            // Get fancy and change color based on data
                            if(item.contains("@")) 
                                this.setTextFill(Color.BLUEVIOLET);
                            setText(item);

                        }
                    }

                };
            }
        });

The Code need to be little changed:

// Method for displaying data in table
    protected void displayDataInTable(){
        tblColID.setCellValueFactory(new PropertyValueFactory<Person, String>("id"));

        // Table cell coloring
        tblColID.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
            @Override
            public TableCell<Person, String> call(TableColumn<Person, String> param) {
                return new TableCell<Person, String>() {

                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!isEmpty()) {
                            this.setTextFill(Color.RED);
                            // Get fancy and change color based on data
                            if(item.contains("@")) 
                                this.setTextFill(Color.BLUEVIOLET);
                            setText(item);

                        }
                    }

                };
            }
        });
下雨或天晴 2024-12-05 13:37:42

或者,类似于 Panduka,

tableColumn.setStyle("-fx-text-fill: red");

Or, similar to Panduka,

tableColumn.setStyle("-fx-text-fill: red");
说好的呢 2024-12-05 13:37:42

对于多行单元格:

    Callback<TableColumn<MyDTO, String>, TableCell<MyDTO, String>> multilineRedCallback = param -> {
        TableCell<MyDTO, String> cell = new TableCell<MyDTO, String>();
        Text text = new Text();
        cell.setGraphic( text );
        cell.setPrefHeight( Region.USE_COMPUTED_SIZE );
        text.setFill( Color.RED );
        text.wrappingWidthProperty().bind( cell.widthProperty() );
        text.textProperty().bind( cell.itemProperty() );
        return cell;
    };
    this.colMultilineRed.setCellFactory( multilineRedCallback );

For a multiline cell :

    Callback<TableColumn<MyDTO, String>, TableCell<MyDTO, String>> multilineRedCallback = param -> {
        TableCell<MyDTO, String> cell = new TableCell<MyDTO, String>();
        Text text = new Text();
        cell.setGraphic( text );
        cell.setPrefHeight( Region.USE_COMPUTED_SIZE );
        text.setFill( Color.RED );
        text.wrappingWidthProperty().bind( cell.widthProperty() );
        text.textProperty().bind( cell.itemProperty() );
        return cell;
    };
    this.colMultilineRed.setCellFactory( multilineRedCallback );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文