单击鼠标中键上的TableView行选择

问题描述 投票:1回答:2

目前在TableView中,单击鼠标中键时不会进行行选择。如果我们右键或左键单击,则选择该行。我试图在中间按钮点击选择行的功能。

我已经知道在表行工厂中包含一个事件处理程序可以解决这个问题。但我有一个自定义表视图,为我的应用程序提供了许多自定义功能。这个自定义TableView广泛应用于我的应用程序。我的主要问题是,我不能要求每个表行工厂都包含此修复程序。

我正在寻找一种在更高级别(可能在TableView级别上)执行此操作的方法,以便行工厂不需要关心它。

任何想法/帮助都非常感谢。

下面是我想要实现的一个简单示例。

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class TableRowSelectionOnMiddleButtonDemo extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        ObservableList<Person> persons = FXCollections.observableArrayList();
        for (int i = 0; i < 4; i++) {
            persons.add(new Person("First name" + i, "Last Name" + i));
        }

        CustomTableView<Person> tableView = new CustomTableView<>();
        TableColumn<Person, String> fnCol = new TableColumn<>("First Name");
        fnCol.setCellValueFactory(param -> param.getValue().firstNameProperty());

        TableColumn<Person, String> lnCol = new TableColumn<>("Last Name");
        lnCol.setCellValueFactory(param -> param.getValue().lastNameProperty());

        tableView.getColumns().addAll(fnCol, lnCol);
        tableView.getItems().addAll(persons);

        tableView.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
            @Override
            public TableRow<Person> call(TableView<Person> param) {
                return new TableRow<Person>(){
                    {
                        /* This will fix my issue, but I dont want each tableView rowfactory to set this behavior.*/
//                        addEventHandler(MouseEvent.MOUSE_PRESSED,e->{
//                            getTableView().getSelectionModel().select(getItem());
//                        });
                    }
                    @Override
                    protected void updateItem(Person item, boolean empty) {
                        super.updateItem(item, empty);
                    }
                };
            }
        });

        VBox sp = new VBox();
        sp.setAlignment(Pos.TOP_LEFT);
        sp.getChildren().addAll(tableView);
        Scene sc = new Scene(sp);
        primaryStage.setScene(sc);
        primaryStage.show();
    }

    public static void main(String... a) {
        Application.launch(a);
    }

    /**
     * My custom tableView.
     * @param <S>
     */
    class CustomTableView<S> extends TableView<S> {
        public CustomTableView() {
            // A lot of custom behavior is included to this TableView.
        }
    }

    class Person {
        private StringProperty firstName = new SimpleStringProperty();
        private StringProperty lastName = new SimpleStringProperty();

        public Person(String fn, String ln) {
            setFirstName(fn);
            setLastName(ln);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public StringProperty firstNameProperty() {
            return firstName;
        }

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

        public String getLastName() {
            return lastName.get();
        }

        public StringProperty lastNameProperty() {
            return lastName;
        }

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

    }
}
javafx tableview javafx-8
2个回答
1
投票

任何全局自定义(每个控件)功能/行为的入口点是控件的外观。更具体地说:用户交互由皮肤的行为控制 - 遗憾的是它没有进入公共范围,因此其访问/修改需要变脏(如访问内部类/方法,部分通过反射)。

假设允许这样的访问,将调整鼠标交互的步骤以相同的方式对中间的反应以及对于TableCell的主按钮的步骤是

  • 实现自定义TableCellSkin
  • 反思地访问它的行为
  • 在行为的inputMap中找到mousePressed处理程序
  • 用自定义处理程序替换原始处理程序,该处理程序用来自主按钮的mouseEvent替换来自中间按钮的mouseEvent
  • 通过css使自定义TableCellSkin成为默认值

注意:负责在表右侧的可用空间中处理mouseEvents的TableRowSkin不会将中间按钮分开,因此目前无关。如果将来发生变化,那么简单应用与表格单元格相同的技巧。

例:

public class TableRowCustomMouse extends Application {

    public static class CustomMouseTableCellSkin<T, S> extends TableCellSkin<T, S> {

        EventHandler<MouseEvent> original;

        public CustomMouseTableCellSkin(TableCell<T, S> control) {
            super(control);
            adjustMouseBehavior();
        }

        private void adjustMouseBehavior() {
            // dirty: reflective access to behavior, use your custom reflective accessor
            TableCellBehavior<T, S> behavior = 
                (TableCellBehavior<T, S>) FXUtils.invokeGetFieldValue(TableCellSkin.class, this, "behavior");
            InputMap<TableCell<T, S>> inputMap = behavior.getInputMap();
            ObservableList<Mapping<?>> mappings = inputMap.getMappings();
            List<Mapping<?>> pressedMapping = mappings.stream()
                    .filter(mapping -> mapping.getEventType() == MouseEvent.MOUSE_PRESSED)
                    .collect(Collectors.toList());
            if (pressedMapping.size() == 1) {
                Mapping<?> originalMapping = pressedMapping.get(0);
                original = (EventHandler<MouseEvent>) pressedMapping.get(0).getEventHandler();
                if (original != null) {
                    EventHandler<MouseEvent> replaced = this::replaceMouseEvent;
                    mappings.remove(originalMapping);
                    mappings.add(new MouseMapping(MouseEvent.MOUSE_PRESSED, replaced));
                }
            }
        }

        private void replaceMouseEvent(MouseEvent e) {
            MouseEvent replaced = e;
            if (e.isMiddleButtonDown()) {
                replaced = new MouseEvent(e.getSource(), e.getTarget(), e.getEventType(),
                    e.getX(), e.getY(),
                    e.getScreenX(), e.getScreenY(),
                    MouseButton.PRIMARY,
                    e.getClickCount(),
                    e.isShiftDown(), e.isControlDown(), e.isAltDown(), e.isMetaDown(),
                    true, false, false,
                    e.isSynthesized(), e.isPopupTrigger(), e.isStillSincePress(),
                    null
                    );
            }
            original.handle(replaced);
        }

    }
    private Parent createContent() {
        TableView<Person> table = new TableView<>(Person.persons());
        TableColumn<Person, String> first = new TableColumn("First Name");
        first.setCellValueFactory(cc -> cc.getValue().firstNameProperty());
        TableColumn<Person, String> last = new TableColumn<>("Last Name");
        last.setCellValueFactory(cc -> cc.getValue().lastNameProperty());
        table.getColumns().addAll(first, last);
        BorderPane content = new BorderPane(table);
        return content;
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(createContent()));
        // load the default css 
        stage.getScene().getStylesheets()
            .add(getClass().getResource("customtablecellskin.css").toExternalForm());
        stage.setTitle(FXUtils.version());
        stage.show();
    }

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

    @SuppressWarnings("unused")
    private static final Logger LOG = Logger
            .getLogger(TableRowCustomMouse.class.getName());

}

具有TableCell自定义外观的CSS:

.table-cell {
    -fx-skin: "<yourpackage>.TableRowCustomMouse$CustomMouseTableCellSkin";
}

1
投票

接受@kleopatra的方法作为答案。然而,我的问题的解决方案与@ kleopatra的答案有点不同。但核心思想仍然是一样的。

我使用该方法来覆盖TableCellBehavior的doSelect方法

    @Override
    protected void doSelect(double x, double y, MouseButton button, int clickCount, boolean shiftDown, boolean shortcutDown) {
        MouseButton btn = button;
        if (button == MouseButton.MIDDLE) {
            btn = MouseButton.PRIMARY;
        }
        super.doSelect(x, y, btn, clickCount, shiftDown, shortcutDown);
    }

以下是解决我的问题的工作演示:

DemoClass:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class TableRowSelectionOnMiddleButtonDemo extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        ObservableList<Person> persons = FXCollections.observableArrayList();
        for (int i = 0; i < 4; i++) {
            persons.add(new Person("First name" + i, "Last Name" + i));
        }

        CustomTableView<Person> tableView = new CustomTableView<>();
        TableColumn<Person, String> fnCol = new TableColumn<>("First Name");
        fnCol.setCellValueFactory(param -> param.getValue().firstNameProperty());

        TableColumn<Person, String> lnCol = new TableColumn<>("Last Name");
        lnCol.setCellValueFactory(param -> param.getValue().lastNameProperty());

        tableView.getColumns().addAll(fnCol, lnCol);
        tableView.getItems().addAll(persons);

        tableView.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
            @Override
            public TableRow<Person> call(TableView<Person> param) {
                return new TableRow<Person>() {
                    {
                        /* This will fix my issue, but I dont want each tableView rowfactory to set this behavior.*/
//                        addEventHandler(MouseEvent.MOUSE_PRESSED,e->{
//                            getTableView().getSelectionModel().select(getItem());
//                        });
                    }

                    @Override
                    protected void updateItem(Person item, boolean empty) {
                        super.updateItem(item, empty);
                    }
                };
            }
        });

        VBox sp = new VBox();
        sp.setAlignment(Pos.TOP_LEFT);
        sp.getChildren().addAll(tableView);
        Scene sc = new Scene(sp);
        sc.getStylesheets().add(this.getClass().getResource("tableRowSelectionOnMiddleButton.css").toExternalForm());
        primaryStage.setScene(sc);
        primaryStage.show();
    }

    public static void main(String... a) {
        Application.launch(a);
    }

    /**
     * My custom tableView.
     *
     * @param <S>
     */
    class CustomTableView<S> extends TableView<S> {
        public CustomTableView() {
            // A lot of custom behavior is included to this TableView.
        }
    }


    class Person {
        private StringProperty firstName = new SimpleStringProperty();
        private StringProperty lastName = new SimpleStringProperty();

        public Person(String fn, String ln) {
            setFirstName(fn);
            setLastName(ln);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public StringProperty firstNameProperty() {
            return firstName;
        }

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

        public String getLastName() {
            return lastName.get();
        }

        public StringProperty lastNameProperty() {
            return lastName;
        }

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

    }
}

CustomTableCellSkin类:

import com.sun.javafx.scene.control.behavior.TableCellBehavior;
import com.sun.javafx.scene.control.skin.TableCellSkinBase;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.input.MouseButton;

public class CustomTableCellSkin<S, T> extends TableCellSkinBase<TableCell<S, T>, TableCellBehavior<S, T>> {

    private final TableColumn<S, T> tableColumn;

    public CustomTableCellSkin(TableCell<S, T> tableCell) {
        super(tableCell, new CustomTableCellBehavior<S, T>(tableCell));
        this.tableColumn = tableCell.getTableColumn();
        super.init(tableCell);
    }

    @Override
    protected BooleanProperty columnVisibleProperty() {
        return tableColumn.visibleProperty();
    }

    @Override
    protected ReadOnlyDoubleProperty columnWidthProperty() {
        return tableColumn.widthProperty();
    }
}

class CustomTableCellBehavior<S, T> extends TableCellBehavior<S, T> {
    public CustomTableCellBehavior(TableCell<S, T> control) {
        super(control);
    }

    @Override
    protected void doSelect(double x, double y, MouseButton button, int clickCount, boolean shiftDown, boolean shortcutDown) {
        MouseButton btn = button;
        if (button == MouseButton.MIDDLE) {
            btn = MouseButton.PRIMARY;
        }
        super.doSelect(x, y, btn, clickCount, shiftDown, shortcutDown);
    }
}

tableRowSelectionOnMiddleButton.css

.table-cell {
    -fx-skin: "<package>.CustomTableCellSkin";
}
© www.soinside.com 2019 - 2024. All rights reserved.