gpt4 book ai didi

java - NPE javafx.scene.control.behavior.ListViewBehavior.mousePressed

转载 作者:行者123 更新时间:2023-12-01 18:10:47 25 4
gpt4 key购买 nike

感谢您花时间查看我的帖子。

我已经实现了带有嵌套 TitledPanes 的 ListViewListView 工作得很好 - 但是,当单击 ListView 中的 TitledPane(单击 Pane 而不是标题)时,会引发 NPE。

在目标区域内单击可以顺利进行,没有任何问题 enter image description here

单击此处将导致 NPE 被抛出。 enter image description here

代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Example extends Application {

@Override
public void start(final Stage primaryStage) throws Exception {
final ExpandableListView<Item> listView = new ExpandableListView<Item>();
listView.setContentProvider(new ExpandableListView.ContentProvider<Item>() {
@Override
public String getTitleOf(final Item item) {
return item.getTitle();
}

@Override
public String getContentOf(final Item item) {
return item.getContent();
}
});
for (int i = 0; i < 100000; i++) {
listView.getItems().add(new Item("title" + i, "content" + i));
}

final BorderPane group = new BorderPane();
group.setCenter(listView);
final Scene scene = new Scene(group);
primaryStage.setScene(scene);
primaryStage.show();
}

public static class Item {

String title;
String content;

public Item(String title, String content) {
this.title = title;
this.content = content;
}

public String getTitle() {
return title;
}

public String getContent() {
return content;
}
}

public static void main(String[] args) {
launch(args);
}
}



import java.util.HashSet;
import java.util.Set;

import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TitledPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.util.Callback;

public class ExpandableListView<E> extends ListView<E> {

private ContentProvider<E> contentProvider=new ContentProvider<E>(){@Override public String getTitleOf(final E item){return item.toString();}

@Override public String getContentOf(final E item){return getTitleOf(item);}};

private final Set<E> expandedItems = new HashSet<E>();

public ExpandableListView() {
setSelectionModel(null);
setCellFactory(new Callback<ListView<E>, ListCell<E>>() {
@Override
public ListCell<E> call(final ListView<E> param) {
final TitledPane titledPane = new TitledPane();
final Text contentArea = new Text();

titledPane.setAnimated(false);
titledPane.setCollapsible(true);
titledPane.setExpanded(false);

final BorderPane contentAreaWrapper = new BorderPane();
contentAreaWrapper.setLeft(contentArea);
titledPane.setContent(contentAreaWrapper);

titledPane.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(final MouseEvent event) {
final boolean expanded = titledPane.isExpanded();
final E item = (E) titledPane.getUserData();
if (item == null) {
return;
}
if (expanded) {
expandedItems.add(item);
} else {
expandedItems.remove(item);
}
}
});
return new ListCell<E>() {
@Override
protected void updateItem(final E item, final boolean empty) {
super.updateItem(item, empty);
if (empty) {
titledPane.setText("");
contentArea.setText("");
return;
}
final boolean expanded = isExpanded(item);
titledPane.setUserData(item);
titledPane.setExpanded(expanded);
titledPane.setText(contentProvider.getTitleOf(item));
contentArea.setText(contentProvider.getContentOf(item));
setGraphic(titledPane);
}
};
}
});

}

public void setContentProvider(final ContentProvider<E> contentProvider) {
this.contentProvider = contentProvider;
}

public void expand(E item) {
expand(item, true);
}

public void collapse(E item) {
expand(item, false);
}

private void expand(E item, boolean expand) {
if (expand) {
this.expandedItems.add(item);
} else {
this.expandedItems.remove(item);
}

ObservableList<E> o = getItems();
setItems(null);
setItems(o);
}

public boolean isExpanded(E item) {
return this.expandedItems.contains(item);
}

public static interface ContentProvider<E> {

String getTitleOf(E item);

String getContentOf(E item);

}
}

NPE:

java.lang.NullPointerException
at com.sun.javafx.scene.control.behavior.ListViewBehavior.mousePressed(ListViewBehavior.java:366)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)

最佳答案

NullPointerException 是由以下行引起的:

setSelectionModel(null);

提供您自己的自定义 no selection model .

关于java - NPE javafx.scene.control.behavior.ListViewBehavior.mousePressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60471824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com