2011-08-09 41 views
6

Trong ứng dụng Java Desktop của tôi, tôi có Bảng JavaFX với 3 cột. Tôi muốn đặt màu phông chữ của cột thứ 3 thành màu đỏ. Tôi đã không thể thiết lập màu phông chữ của Tableb cả. Tôi nhìn vào CSS và tôi không tìm thấy gì cả. Có cách nào để làm điều đó với CSS? Tôi cũng tìm setFont() với hy vọng thiết lập nó theo cách đó. Không co gi ở đo. Tôi thậm chí không thể tìm ra cách để thiết lập thứ gì đó trên một tế bào nào đó.Đặt màu phông chữ của JavaFX TableView Cells?

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

Làm cách nào để thực hiện điều này? Làm cách nào để đặt màu phông chữ? Bất kỳ trợ giúp sẽ được đánh giá cao.

Trả lời

20

Bạn cần ghi đè CellFactory.

đang

phần chỉ của cột thứ ba:

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

Toàn bộ Mã số Ví dụ:

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

Xin lỗi, tôi đã figured it ngoài. Tôi chỉ không có cơ hội để đăng câu trả lời. Nhưng bạn là một câu trả lời hay. – Dorothy

+1

Cảm ơn! Tôi đã tìm được nhiều từ các bài viết khác của bạn - nhưng tôi đã nhìn thấy câu hỏi này bật lên nhiều lần ở những nơi khác để nó không được trả lời. – jkaufmann

+0

Xin lỗi vì tôi đã trễ bữa tiệc, nhưng làm thế nào để bạn vô hiệu hóa điền văn bản khi hàng của bảng được chọn? Kính trọng –

2

Bộ luật cần phải được thay đổi chút:

// 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); 

         } 
        } 

       }; 
      } 
     }); 
Các vấn đề liên quan