テーブルセルの背景色を変更すると、選択色を削除するのは難しい読みになり

三部作:

私はこの変更を行いました。

交互の行の色を維持しながらテーブルビューの変更TableColumnの背景、?

それは良い非選択になります。

未選択

しかし、あなたは、セルを選択した場合:

選択

それはこのように見えますが、私は、選択したときに、それが正常に動作したいと思います/焦点を当てました。

私はスタイルクラスを使用するにはかなり確信して、私の必要性です、しかし、私は属性はあなたが他のすべての機能を維持するために必要なものを知らないTableCellだけで、異なる色の背景を。また、私は、セル・レベルまたは列レベルでスタイルクラスを適用していますか?

更新

私のCSSファイル:のcustom.css

.customhighlight .table-cell {
    -fx-background-color: rgba(0, 128, 0, 0.3);
}

.customhighlight .table-cell:selected {
    -fx-background-color: inherit;
}

どのように私は1つの列にこれを適用するのですか?

私は試した

table.getStyleClass().add("customhighlight");

しかし、それはテーブル全体を変更しました。

私は試した

tableCol.getStyleClass().add("customhighlight");

そして、それは何もしませんでした。

私はまた、細胞レベルでそれを試してみました...

なスロー:

私が正しくあなたを理解していれば、あなたがしたいです:

  • 列のすべてのセルは、半透明の背景を持っています。
  • これらの細胞は、選択したときに、デフォルトのようになりますmodena.css選択した表情。
    • 換言すれば、その暗い青色半透明の背景を交換し、テキストが白になります。

あなたはその後、CSSファイルで使用できる適切な細胞にスタイルクラスを追加する必要があります。ここで小さな例を示します。

Main.java

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

import javafx.application.Application;
import javafx.collections.FXCollections;
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.stage.Stage;
import javafx.util.Pair;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        var table = System.getProperties().stringPropertyNames().stream()
                .map(name -> new Pair<>(name, System.getProperty(name)))
                .collect(collectingAndThen(toCollection(FXCollections::observableArrayList), TableView::new));
        table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
        table.getSelectionModel().setCellSelectionEnabled(true); // Not sure if you're using cell or row selection

        var keyCol = new TableColumn<Pair<String, String>, String>("Key");
        keyCol.setCellValueFactory(new PropertyValueFactory<>("key"));
        table.getColumns().add(keyCol);

        var valCol = new TableColumn<Pair<String, String>, String>("Value");
        valCol.setCellValueFactory(new PropertyValueFactory<>("value"));
        valCol.setCellFactory(tc -> new TableCell<>() {
            { getStyleClass().add("highlighted-table-cell"); }
            @Override protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if (empty || item == null) {
                    setText(null);
                } else {
                    setText(item);
                }
            }
        });
        table.getColumns().add(valCol);

        var scene = new Scene(table, 600, 400);
        scene.getStylesheets().add("Main.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

あるmain.css

.highlighted-table-cell {
    -fx-background-color: rgba(0, 128, 0, 0.3);
}

/* Needed by cell selection mode */
.highlighted-table-cell:selected {
    -fx-background-color: inherit;
}

/* Needed by row selection mode */
.table-row-cell:selected > .highlighted-table-cell {
    -fx-background-color: inherit;
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=203096&siteId=1