简体   繁体   English

如何选择JavaFX TableCell颜色?

[英]How do i change the JavaFX TableCell color when it is selected?

Well, I created a custom TableCell for my TableView. 好吧,我为TableView创建了一个自定义TableCell。 This custom TableCell contains a Link and opens the browser when clicked. 此自定义TableCell包含一个链接,并在单击时打开浏览器。 Everything is working fine, what I want to do is change the text color of this TableCell when it is selected... This is what I am trying to do: 一切都工作正常,我想要做的是在选择时更改此TableCell的文本颜色......这就是我想要做的:

    callback = new Callback<TableColumn, TableCell>(){
        @Override
        public TableCell call(TableColumn param) {
            return new TableCell<Test, String>(){
                EventHandler handler = new EventHandler<MouseEvent>() {
                    final AM_RSS_FX RSS = AM_RSS_FX.this;
                    @Override
                    public void handle(MouseEvent param) {
                        try {
                            java.awt.Desktop.getDesktop().browse(new URI(RSS.link));
                        } catch (IOException | URISyntaxException ex) {
                            Logger.getLogger(AM_RSS_FX.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                };
                @Override
                public void updateItem(String item, boolean empty){
                    super.updateItem(item, empty);
                    if(!isEmpty()){
                        final AM_RSS_FX RSS = AM_RSS_FX.this;
                        this.setTextFill(Color.BLUE);
                        setText(item);
                        RSS.link = this.getText();
                        this.addEventHandler(MouseEvent.MOUSE_CLICKED, handler);
                    }
                }

                @Override
                public void updateSelected(boolean arg0){
                    super.updateSelected(arg0);
                    if(isSelected()){
                        this.setTextFill(Color.AQUA);
                    }
                }

            };
        }
    };

I don't know which method i need to Override =/ I tried to Override the updateSelected, but didn't worked =/ 我不知道我需要哪种方法覆盖= /我试图覆盖updateSelected,但没有工作= /

Can someone help me? 有人能帮我吗?

1- You are adding a mouse event handler on TableCell instance and that event is firing when you click on it. 1-您正在TableCell实例上添加一个鼠标事件处理程序,当您单击该事件时该事件将被触发。 However the table cell is still not being selected. 但是表单元仍未被选中。 Instead the table row cell selection is being fired. 而是触发表单元格选择。 To enable cell selection do: 要启用单元格选择,请执

table.getSelectionModel().setCellSelectionEnabled(true);

2- No need to override updateSelected() to manage style, instead use CSS selectors from caspian.css: 2-无需覆盖updateSelected()来管理样式,而是使用来自caspian.css的CSS选择器:

.table-cell:selected {
    -fx-background-color: lightgreen;
    -fx-text-fill: green;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM