设置 JavaFX TableView 单元格的字体颜色? [英] Setting font color of JavaFX TableView Cells?

查看:78
本文介绍了设置 JavaFX TableView 单元格的字体颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Java 桌面应用程序中,我有一个包含 3 列的 JavaFX 表.我想将第三列的字体颜色设置为红色.我根本无法设置 Tableb 的字体颜色.我查看了 CSS 并没有找到任何东西.有没有办法用 CSS 做到这一点?我还寻找 setFont() 并希望以这种方式设置它.空空如也.我什至想不出在某个单元格上设置某些内容的方法.

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.

推荐答案

您需要覆盖 CellFactory.

You need to override the 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","SomeEmail@gmail.com"));  

    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();
}
}

这篇关于设置 JavaFX TableView 单元格的字体颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆