Спасибо, что нашли время для просмотра моего поста.
Я реализовал ListView с вложенными TitledPanes. ListView отлично работает - однако при нажатии на TitledPane в ListView (панель щелчка, но не заголовок) NPE
Щелчок в пределах предполагаемой области работает без проблем
Нажатие здесь вызовет выброс NPE.
Код:
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)