ListView,通过双击选择项目
ListView, Selecting items by double click
我有一个主要 window,用户可以在其中 select 通过单击 ListView 项来编程功能。然而,即使他完全在 Items(白色箭头)程序之外单击,仍然会启动 "Number Generator" 功能。我不喜欢那样。
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());
}
}
});
}
}
}
}
我没有看到你的 FXML 文件,但从你的命名我可以猜到只要你的 ListView
被点击,你的控制器方法 private void listViewMouseDoubleClicked(MouseEvent e)
就会被触发。
只要用户单击 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());
}
}
}
我有一个主要 window,用户可以在其中 select 通过单击 ListView 项来编程功能。然而,即使他完全在 Items(白色箭头)程序之外单击,仍然会启动 "Number Generator" 功能。我不喜欢那样。
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());
}
}
});
}
}
}
}
我没有看到你的 FXML 文件,但从你的命名我可以猜到只要你的 ListView
被点击,你的控制器方法 private void listViewMouseDoubleClicked(MouseEvent e)
就会被触发。
只要用户单击 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());
}
}
}