栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

设置JavaFX TableView单元的字体颜色?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

设置JavaFX TableView单元的字体颜色?

您需要覆盖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;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);    }} public static void main(String[] args) {    Application.launch(args);}@Overridepublic 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();}}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595768.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号