JAVAFX鼠标单击某个值

时间:2015-07-27 21:26:06

标签: java string javafx mouseevent

当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 个答案:

答案 0 :(得分:1)

您可以检索单元格正在渲染的项目,并检查其值。

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