我有一个主窗口,用户可以通过单击ListView项目来选择程序功能。然而,即使他完全点击项目(白色箭头)程序仍然会启动"数字生成器"特征。我不喜欢那样。
public class MainRootController2 extends Controller implements Initializable {
@FXML
StackPane stackPane;
@FXML
ListView<String> listView;
private ObservableList<String> list;
@Override
public void initialize(URL location, ResourceBundle resources) {
//making a list and setting a background
list = FXCollections.observableArrayList("Number Generator", "Cards");
listView.setBackground(new Background(new BackgroundImage(new Image("MainRootBackground.jpg"),
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.CENTER, BackgroundSize.DEFAULT)));
listView.setItems(list);
listView.getSelectionModel().clearSelection();
}
@Override
public Pane getMainPane() {
return stackPane;
}
/************************ EVENTS *************************/
@FXML
private void listViewMouseDoubleClicked(MouseEvent e){
if(e.getClickCount() > 1){
String s = listView.getSelectionModel().getSelectedItem();
switch(s){
case "Number Generator":
ChangeTheRoot change = new ChangeTheRoot();
if( change.initialize(Main.primaryStage, true, Main.NUMBER_GENERATOR_ROOT, "Number Generator", true, new NumberGeneratorController()) == false)
Dialogs.create().title("Error").masthead(null).message("Couldn't load the components!").showInformation();
change.doFadingTransition(stackPane, Main.fadeOutMillis, Main.fadeInMillis, true);
break;
default:
break;
}
}
}
}
编辑....修正版:
public class MainRootController2 extends Controller implements Initializable {
@FXML
StackPane stackPane;
@FXML
ListView<String> listView;
private ObservableList<String> list;
@Override
public void initialize(URL location, ResourceBundle resources) {
//making a list and setting a background
list = FXCollections.observableArrayList("Number Generator", "Cards");
listView.setBackground(new Background(new BackgroundImage(new Image("MainRootBackground.jpg"),
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.CENTER, BackgroundSize.DEFAULT)));
listView.setItems(list);
listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> list) {
return new MouseClickListCell();
}
});
listView.getSelectionModel().clearSelection();
}
@Override
public Pane getMainPane() {
return stackPane;
}
/************************ EVENTS *************************/
private void fireUpTheFeature(String name){
String s = listView.getSelectionModel().getSelectedItem();
switch(s){
case "Number Generator":
ChangeTheRoot change = new ChangeTheRoot();
if( change.initialize(Main.primaryStage, true, Main.NUMBER_GENERATOR_ROOT, "Number Generator", true, new NumberGeneratorController()) == false)
Dialogs.create().title("Error").masthead(null).message("Couldn't load the components!").showInformation();
change.doFadingTransition(stackPane, Main.fadeOutMillis, Main.fadeInMillis, true);
break;
case "Cards":
break;
default:
break;
}
}
public class MouseClickListCell extends ListCell<String>
{
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if(empty) {
setText(null);
setOnMouseClicked(null);
}
else {
setText(item.toString());
setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getClickCount() > 1){
fireUpTheFeature(getItem());
}
}
});
}
}
}
}
答案 0 :(得分:5)
我没有看到你的FXML文件,但是根据你的命名,我猜你的控制器方法private void listViewMouseDoubleClicked(MouseEvent e)
会在你点击ListView
时触发。
只要用户点击ListView
区域内的任何位置,就会触发此事件。您想要的是删除onMouseClicked
处理程序并将该代码移到ListCell
渲染器中。
如果仅将鼠标侦听器附加到非空单元格,则只有在单击ListCell
时才会触发代码。
例如:
public static class MouseClickListCell<T> extends ListCell<T>
{
@Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if(empty) {
setText(null);
setOnMouseClicked(null);
}
else {
setText(item.toString());
setOnMouseClicked(ev -> doMagic());
}
}
}