将悬停侦听器添加到javafx表中特定列的单元格

时间:2017-05-21 07:27:02

标签: java javafx javafx-2 javafx-8

我想为OnMouseOver的特定列的单元格创建一个TableView侦听器,这样当我将鼠标悬停在其单元格上时,弹出窗口应显示一些消息。这就是我创建表及其列的方式。

private TableView<Stock> stockTable = new TableView<>();
ObservableList columns = stockTable.getColumns();
public TableColumn createTextColumn(String columnName, String columnCaption,BooleanProperty editingStarted) {
    TableColumn column = new TableColumn(columnCaption);
    column.setCellValueFactory(new PropertyValueFactory<>(columnName));
    column.setCellFactory(TextFieldTableCell.forTableColumn());
    return column;
}
final TableColumn nameColumn 
                         = createTextColumn("name", "Product Name", editingStarted);
columns.add(nameColumn);

所以可以这样做。我知道如何为行创建侦听器。但我在互联网上没有发现任何关于听桌子的细节。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您已经使用了需要重写的TextFieldTableCell.forTableColumn()来满足您的需求

import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.MouseEvent;
import javafx.stage.Popup;
import javafx.util.Callback;
import javafx.util.StringConverter;
import javafx.util.converter.DefaultStringConverter;

public final class CustomTextFieldTableCell<S, T> extends TextFieldTableCell<S, T> {

    public static <S> Callback<TableColumn<S, String>, TableCell<S, String>> forTableColumn() {
        return forTableColumn(new DefaultStringConverter());
    }

    public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> forTableColumn(final StringConverter<T> converter) {
        return new Callback<TableColumn<S, T>, TableCell<S, T>>() {
            @Override
            public TableCell<S, T> call(TableColumn<S, T> list) {
                final TextFieldTableCell<S, T> result = new TextFieldTableCell<S, T>(converter);
                final Popup popup = new Popup();
                popup.setAutoHide(true);

                final EventHandler<MouseEvent> hoverListener = new EventHandler<MouseEvent>() {

                    @Override
                    public void handle(MouseEvent event) {
                        final Label popupContent = new Label("this is a popup showing the content = '" + result.getText() + "'");
                        popupContent.setStyle("-fx-background-color: #64b5f6; -fx-border-color: #000000; -fx-border-width: 1px; -fx-padding: 5px; -fx-text-fill: white;");

                        popup.getContent().clear();
                        popup.getContent().addAll(popupContent);

                        if (event.getEventType() == MouseEvent.MOUSE_EXITED) {
                            popup.hide();
                        } else if (event.getEventType() == MouseEvent.MOUSE_ENTERED) {
                            popup.show(result, event.getScreenX() + 10, event.getScreenY());                
                        }
                    }
                };

                result.setOnMouseEntered(hoverListener);
                result.setOnMouseExited(hoverListener);
                return result;
            }
        };
    }
}

要使用它,您需要将代码更改为

column.setCellFactory(CustomTextFieldTableCell.forTableColumn());

结果如下所示

enter image description here