2012-12-04 32 views
14

tôi cần tạo JavaFx TableView với các hàng nhiều màu (color1 cho ưu tiên thấp, color2 cho mức độ ưu tiên trung bình, v.v.). Tôi đã tạo ra CellFactoryMàu xem bảng JavaFX

public class TaskCellFactory implements Callback<TableColumn, TableCell> { 

@Override 
public TableCell call(TableColumn p) { 

    TableCell cell = new TableCell<Task, Object>() { 
     @Override 
     public void updateItem(Object item, boolean empty) { 
      super.updateItem(item, empty); 
      setText(empty ? null : getString()); 
      setGraphic(null); 
      TableRow currentRow = getTableRow(); 
      Task currentTask = currentRow == null ? null : (Task)currentRow.getItem(); 
      if(currentTask != null){ 
       Priority priority = currentTask.getPriority(); 
       clearPriorityStyle(); 
       if(!isHover() && !isSelected() && !isFocused()){ 
        setPriorityStyle(priority); 
       } 
      } 
     } 

     @Override 
     public void updateSelected(boolean upd){ 
      super.updateSelected(upd); 
      System.out.println("is update"); 
     } 

     private void clearPriorityStyle(){ 
      ObservableList<String> styleClasses = getStyleClass(); 
      styleClasses.remove("priorityLow"); 
      styleClasses.remove("priorityMedium"); 
      styleClasses.remove("priorityHigh"); 
     } 

     private void setPriorityStyle(Priority priority){ 
      switch(priority){ 
       case LOW: 
        getStyleClass().add("priorityLow"); 
        break; 
       case MEDIUM: 
        getStyleClass().add("priorityMedium"); 
        break; 
       case HIGH: 
        getStyleClass().add("priorityHigh"); 
        break; 
      } 
      System.out.println(getStyleClass()); 
     } 

     private String getString() { 
      return getItem() == null ? "" : getItem().toString(); 
     } 
    }; 
    return cell; 
} } 

và css

.priorityLow{ -fx-background-color: palegreen; } 
.priorityMedium{ -fx-background-color: skyblue;} 
.priorityHigh{ -fx-background-color: palevioletred;} 

Nhưng tôi vẫn cần phải làm nổi bật hàng lựa chọn. Làm thế nào tôi có thể làm điều đó?

Trả lời

17

Thay vì đặt màu nền cho toàn bộ ô trong css của bạn, chỉ cần đặt -fx-control-inner-background. Sau đó, bạn sẽ có các nút nhấn mặc định, di chuột và tiêu điểm vẫn có sẵn. Ngoài ra, hãy xóa câu lệnh if xung quanh cuộc gọi setPriorityStyle của bạn.

Nếu bạn cũng muốn ghi đè những thứ như màu mặc định (được chọn) hoặc màu di chuột, bạn cũng có thể thực hiện điều này như trong css bên dưới - không chắc chắn các ghi đè nổi bật có thực sự được đề xuất hay không trên ứng dụng của bạn và trải nghiệm người dùng mong muốn.

.priorityLow { 
    -fx-control-inner-background: palegreen; 
    -fx-accent: derive(-fx-control-inner-background, -40%); 
    -fx-cell-hover-color: derive(-fx-control-inner-background, -20%); 
} 

.priorityMedium { 
    -fx-control-inner-background: skyblue; 
    -fx-accent: derive(-fx-control-inner-background, -40%); 
    -fx-cell-hover-color: derive(-fx-control-inner-background, -20%); 
} 

.priorityHigh { 
    -fx-control-inner-background: palevioletred; 
    -fx-accent: derive(-fx-control-inner-background, -40%); 
    -fx-cell-hover-color: derive(-fx-control-inner-background, -20%); 
} 

rowhighlight


thông tin phong cách chi tiết cho JavaFX có thể được tìm thấy trong các mặc định caspian.css stylesheet cho JavaFX 2.2 và JavaFX 2 CSS reference guide. Để tìm caspian.css cho phiên bản JavaFX của bạn, bạn có thể mở jfxrt.jar (đôi khi được tìm thấy trong thư mục jre/lib).

+1

Cảm ơn bạn)) Bạn có thể trả lời, tôi có thể tìm danh sách các thuộc tính css này (như -fx-control-inner-background, -fx-accent) hoặc một số mẫu không? –

+0

Đã cập nhật câu trả lời để trỏ đến các tài nguyên này. – jewelsea

+0

Tôi có một câu hỏi khác: tôi có thể làm nổi bật tương tự ô đơn lẻ trong hàng không? –

Các vấn đề liên quan