2017-01-23 28 views
0

一個複選框,選中該列我有這樣的情況: Example如何突出,我通過JavaFX中

在我選擇一個複選框(一個或多個)的那一刻,我想強調相應的列。

我想這個愚蠢和粗魯solution,但它只能在列的標題中工作。

+0

[它不工作時,我禁用複選框(https://i.stack.imgur.com/etE3A.png) –

+0

它們是相互排斥的複選框? –

+0

不,我可以選擇任何複選框。 對於每個選定的複選框,我想顯示其對應的列突出顯示,併爲每個取消選中的複選框,我不想顯示其相應的列突出顯示。 –

回答

0

如果要突出顯示整列,則需要在列上使用單元格工廠並突出顯示單元格。這與JavaFX - Detect & highlight TableColumn being dragged onto類似,但您需要能夠突出顯示多列。要做到這一點,請使用ObservableSet<TableColumn<?,?>來跟蹤應突出顯示哪些列,並從複選框中添加/刪除列。

下面是一個例子,適於從上面鏈接的例子:

import java.util.Arrays; 
import java.util.List; 
import java.util.function.Function; 

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 
import javafx.beans.value.ObservableValue; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableSet; 
import javafx.collections.SetChangeListener.Change; 
import javafx.css.PseudoClass; 
import javafx.scene.Scene; 
import javafx.scene.control.CheckBox; 
import javafx.scene.control.TableCell; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.util.Callback; 

public class TableColumnHighlightByCheckBox extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     TableView<Person> table = new TableView<>(); 
     table.getColumns().add(column("First Name", Person::firstNameProperty)); 
     table.getColumns().add(column("Last Name", Person::lastNameProperty)); 
     table.getColumns().add(column("Email", Person::emailProperty)); 

     ObservableSet<TableColumn<?,?>> highlightColumns = FXCollections.observableSet(); 

     table.getItems().addAll(createData()); 

     VBox checkBoxes = new VBox(5); 
     checkBoxes.getStyleClass().add("controls"); 
     table.getColumns().forEach(col -> 
      checkBoxes.getChildren().add(createHighlightColumnCheckBox(col, highlightColumns))); 

     table.getColumns().forEach(col -> highlightColumnWhenNeeded(col, highlightColumns)); 

     BorderPane root = new BorderPane(table); 
     root.setTop(checkBoxes); 

     Scene scene = new Scene(root, 800, 600); 
     scene.getStylesheets().add("style.css"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private <S,T> CheckBox createHighlightColumnCheckBox(TableColumn<S,T> column, ObservableSet<TableColumn<?,?>> highlightColumns) { 


     CheckBox checkBox = new CheckBox(); 
     checkBox.textProperty().bind(column.textProperty()); 
     checkBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> { 
      if (isNowSelected) { 
       highlightColumns.add(column); 
      } else { 
       highlightColumns.remove(column); 
      } 
     }); 

     return checkBox ; 
    } 

    private <S,T> void highlightColumnWhenNeeded(TableColumn<S,T> column, ObservableSet<TableColumn<?,?>> highlightColumns) { 

     Callback<TableColumn<S,T>, TableCell<S,T>> currentCellFactory = column.getCellFactory() ; 

     PseudoClass highlight = PseudoClass.getPseudoClass("highlight"); 

     column.setCellFactory(tc -> { 
      TableCell<S,T> cell = currentCellFactory.call(tc); 
      highlightColumns.addListener((Change<? extends TableColumn<?,?>> c) -> 
        cell.pseudoClassStateChanged(highlight, 
         highlightColumns.contains(column))); 

      cell.pseudoClassStateChanged(highlight, highlightColumns.contains(column)); 

      return cell ; 
     }); 
    } 

    private static <S,T> TableColumn<S,T> column(String text, Function<S, ObservableValue<T>> property) { 
     TableColumn<S,T> col = new TableColumn<>(text); 
     col.setCellValueFactory(cellData -> property.apply(cellData.getValue())); 
     return col ; 
    } 

    private List<Person> createData() { 
     return Arrays.asList(
       new Person("Jacob", "Smith", "[email protected]"), 
       new Person("Isabella", "Johnson", "[email protected]"), 
       new Person("Ethan", "Williams", "[email protected]"), 
       new Person("Emma", "Jones", "[email protected]"), 
       new Person("Michael", "Brown", "[email protected]")  
     ); 
    } 

    public static class Person { 
     private final StringProperty firstName = new SimpleStringProperty(); 
     private final StringProperty lastName = new SimpleStringProperty(); 
     private final StringProperty email = new SimpleStringProperty(); 


     public Person(String firstName, String lastName, String email) { 
      setFirstName(firstName); 
      setLastName(lastName); 
      setEmail(email); 
     } 


     public final StringProperty firstNameProperty() { 
      return this.firstName; 
     } 



     public final String getFirstName() { 
      return this.firstNameProperty().get(); 
     } 



     public final void setFirstName(final String firstName) { 
      this.firstNameProperty().set(firstName); 
     } 



     public final StringProperty lastNameProperty() { 
      return this.lastName; 
     } 



     public final String getLastName() { 
      return this.lastNameProperty().get(); 
     } 



     public final void setLastName(final String lastName) { 
      this.lastNameProperty().set(lastName); 
     } 



     public final StringProperty emailProperty() { 
      return this.email; 
     } 



     public final String getEmail() { 
      return this.emailProperty().get(); 
     } 



     public final void setEmail(final String email) { 
      this.emailProperty().set(email); 
     } 



    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

而樣式表:

.table-cell:highlight { 
    -fx-background-color: -fx-background ; 
    -fx-background: yellow ; 
    -fx-border-color: -fx-table-cell-border-color -fx-table-cell-border-color transparent transparent ; 
} 
.controls { 
    -fx-padding: 10 ; 
} 

我剛製成亮黃色,如果它是在列被高亮顯示整個小區,但是您可以修改樣式表以使用您喜歡的任何樣式進行高亮顯示。

enter image description here

+0

感謝您的解決方案幫助了我和[這是我的簡單解決方案](http://pastebin.com/2R8Sz0p5)。 我是一名java初學者,能否向我解釋你的highlightColumnWhenNeeded方法?我不太明白通配符和泛型類型 –

+0

'TableColumn '只是表示「某些未知類型的TableColumn」。既然你只是把它們存儲在一個集合中,你不關心它們是什麼類型的,所以保留這個一般就可以。 –

+0

好的,謝謝! 但是,有一個問題:在選擇複選框後,如果我滾動表格視圖,某些行將不會突出顯示。 它突出顯示所有單元格,我只選擇其他複選框... [看這個視頻來了解問題](https://www.youtube.com/watch?v=t4DnnOFIUtc)。 這是fxml或java代碼中的佈局問題? –

相關問題