1
我有一個主窗口,用戶可以通過單擊ListView項目來選擇程序功能。然而,即使他完全在物品外點擊(白色箭頭),程序仍會啓動「數字生成器」功能。我不喜歡那樣。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());
}
}
});
}
}
}
}
'setOnMouseClicked(EV - > doMagic());'不過爲什麼我結識點擊了哪個小區??? – Tomasz 2015-02-08 11:22:25
您是否閱讀過JavaDocs?在你的'ListCell'中,你可以通過'getItem()'來獲取單元格的當前項目... – eckig 2015-02-08 11:22:59
這種小功能的很多變化。我做了一些改變,它的工作原理!謝謝! (我也把整個固定代碼) – Tomasz 2015-02-08 11:38:37