2016-06-13 1 views
0

JavaFXでtableView.getSelectionModel().setCellSelectionEnabled(true);を使わずにTableViewでTableCellをスタイルする方法はありますか?setCellSelectionEnabledを使わずにフォーカスを当ててTableCellスタイルを変更する

私は、このソリューションhttps://community.oracle.com/thread/3528543?start=0&tstart=0を試してみましたが、それはランダムに行に

元強調するために失敗します。

tableView.getSelectionModel().setCellSelectionEnabled(true); 
    final ObservableSet<Integer> selectedRowIndexes = FXCollections.observableSet(); 
    final PseudoClass selectedRowPseudoClass = PseudoClass.getPseudoClass("selected-row"); 


    tableView.getSelectionModel().getSelectedCells().addListener((Change<? extends TablePosition> change) -> { 
     selectedRowIndexes.clear(); 
     tableView.getSelectionModel().getSelectedCells().stream().map(TablePosition::getRow).forEach(row -> { 
      selectedRowIndexes.add(row); 
     }); 
    }); 

    tableView.setRowFactory(tableView -> { 
     final TableRow<List<StringProperty>> row = new TableRow<>(); 
     BooleanBinding selectedRow = Bindings.createBooleanBinding(() -> 
       selectedRowIndexes.contains(row.getIndex()), row.indexProperty(), selectedRowIndexes); 
     selectedRow.addListener((observable, oldValue, newValue) -> { 
        row.pseudoClassStateChanged(selectedRowPseudoClass, newValue); 
       } 
     ); 
     return row; 
    }); 
+0

を使用して可能です。スタイルを常に 'TableCell'に適用したいのですか、あるいは行が選択されたときだけですか? –

+0

行が選択されているとき。私は通常、tableView.getFocusModel()。getFocusedCell()でセルの位置を取得しますが、スタイルクラスを適用するためにTableCellオブジェクトを取得できないため、スタイルを設定することはできません。 – gadelkareem

+0

'TableRow'が選択されたときに、' TableCell'をフォーカスさせることなく 'TableCell'を' TableRow'でスタイルしたいと思っていますか? –

答えて

0

私はフォーカスリスナーを使用して、それを解決することができませんでしたが、それは私が、私はあなたがあなたの例から欲しいものを理解していないMouseEvent.MOUSE_CLICKED

column.setCellFactory(column1 -> { 
    TableCell<List<StringProperty>, String> cell = new TextFieldTableCell<>(); 
    cell.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> 
      cell.setStyle("-fx-border-color:black black black black;-fx-background-color:#005BD1;-fx-text-fill:white") 
    ); 
    cell.addEventFilter(MouseEvent.MOUSE_EXITED, e -> 
      cell.setStyle("") 
    ); 
    return cell; 

}); 

全例にhttps://github.com/gadelkareem/aws-client/blob/master/src/main/java/com/gadelkareem/awsclient/application/Controller.java#L443

0

大丈夫。あなたのセルが実際に焦点を当てている限り、異なるスタイルを設定する列のセルファクトリにカスタムTableCellを指定すると、TableCellを定義しているので、TableCellの任意のプロパティを聴くことができます。以下は、TableCellのfocusedPropertyを聴いて、それが起こったときのスタイルを変更する方法の例です。

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.layout.BorderPane; 
import javafx.stage.Stage; 

public class MCVE3 extends Application { 
    @Override 
    public void start(Stage stage) { 
     TableView<ObservableList<String>> table = new TableView<ObservableList<String>>(); 

     // I have no idea how to get focus on a cell unless you enable cell selection. It does not seem to be possible at all. 
     table.getSelectionModel().setCellSelectionEnabled(true); 

     // Initializes a column and adds it to the table. 
     TableColumn<ObservableList<String>, String> col = new TableColumn<ObservableList<String>, String>("Column"); 
     col.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().get(0))); 

     // Initializes a column and adds it to the table. 
     TableColumn<ObservableList<String>, String> col2 = new TableColumn<ObservableList<String>, String>("Column 2"); 
     col2.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().get(1))); 

     // We add a custom cell factory to second column. This enables us to customize the behaviour of the cell. 
     col2.setCellFactory(e -> new FocusStyleCell()); 

     table.getColumns().addAll(col, col2); 

     // Add data to the table. 
     table.getItems().add(FXCollections.observableArrayList("One", "OneTwo")); 
     table.getItems().add(FXCollections.observableArrayList("Two", "TwoTwo")); 
     table.getItems().add(FXCollections.observableArrayList("Three", "ThreeTwo")); 
     table.getItems().add(FXCollections.observableArrayList("Four", "FourTwo")); 

     BorderPane view = new BorderPane(); 
     view.setCenter(table); 

     stage.setScene(new Scene(view)); 
     stage.show(); 
    } 

    /** 
    * A custom TableCell that will change style on focus. 
    */ 
    class FocusStyleCell extends TableCell<ObservableList<String>, String> { 
     // You always need to override updateItem. It's very important that you don't forget to call super.updateItem when you do this. 
     @Override 
     public void updateItem(String item, boolean empty) { 
      super.updateItem(item, empty); 
      if (item == null || empty) { 
       setText(null); 
      } else { 
       setText(item); 
      } 
     } 

     public FocusStyleCell() { 
      // We add a listener to the focusedProperty. newValue will be true when the cell gets focused. 
      focusedProperty().addListener((obs, oldValue, newValue) -> { 
       if (newValue) { 
        setStyle("-fx-background-color: black;"); 

        // // Or add some custom style class: 
        // if (getStyleClass().contains("focused-cell")) { 
        // getStyleClass().add("focused-cell"); 
        // } 
       } else { 
        // If you instead wish to use style classes you need to 
        // remove that style class once focus is lost. 
        // getStyleClass().remove("focused-cell"); 
        setStyle("-fx-background-color: -fx-background"); 
       } 
      }); 
     } 
    } 

    public static void main(String[] args) { 
     launch(); 
    } 
} 
関連する問題