提问者:小点点

JAVAFX鼠标单击某些值


当JavaFX表格单元格中的字符串值为“Chrome”并且右键单击该单元格时,寻找打开新场景。当我单击应用程序列中的任何单元格时,它不会输出第二个按钮。

                        @Override
                        public void handle(MouseEvent event) {
                            MouseButton button = event.getButton();
                            if (button == MouseButton.PRIMARY) {
                                System.out.println("primany buton");
                            } else if (button == MouseButton.SECONDARY && applicationname_column.getCellValueFactory().toString().contentEquals("Chrome"))
                            {
                                System.out.println("second button");
                                Parent Chromepage = null;
                                try {
                                    Chromepage = FXMLLoader.load(getClass().getResource("Chrome_logs.fxml"));
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

                                Stage stage = new Stage();
                                Scene scene = new Scene(Chromepage);
                                stage.setScene(scene);
                                URL url = this.getClass().getResource("DarkTheme.css");
                                if (url == null) {
                                    System.out.println("Resource not found. Aborting.");
                                    System.exit(-1);
                                }
                                String css = url.toExternalForm();
                                scene.getStylesheets().add(css);
                                stage.setTitle("Chrome Logs");
                                stage.show();
                            } else if (button == MouseButton.MIDDLE) {
                                System.out.println("middle button");
                            }
                        }
                    });
                    return cell;
                }
                }

共1个答案

匿名用户

您可以检索单元格正在呈现的项目,并检查其值。这样做

...
else if (button == MouseButton.SECONDARY && "Chrome".equals(cell.getItem()))
{
    ...
}
...