From e54846d27c8ed52b73640ca318d20b042f555875 Mon Sep 17 00:00:00 2001 From: weisj Date: Wed, 25 Mar 2020 21:46:06 +0100 Subject: [PATCH] Fixed NPE when trying to access drop target in headless mode. Fixes #119 --- .../github/weisj/darklaf/ui/tabbedpane/DarkHandler.java | 4 +++- .../weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java | 8 ++++++-- .../github/weisj/darklaf/ui/tabframe/DarkTabFrameUI.java | 8 ++++++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkHandler.java b/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkHandler.java index 5d236a36..1eb81d03 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkHandler.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkHandler.java @@ -29,6 +29,7 @@ import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.plaf.UIResource; import java.awt.*; +import java.awt.dnd.DropTarget; import java.awt.event.ContainerEvent; import java.awt.event.MouseEvent; import java.beans.PropertyChangeEvent; @@ -108,7 +109,8 @@ public class DarkHandler extends TabbedPaneHandler { ui.tabPane.doLayout(); } else if (DarkTabbedPaneUI.KEY_DND.equals(key)) { ui.dndEnabled = Boolean.TRUE.equals(ui.tabPane.getClientProperty(DarkTabbedPaneUI.KEY_DND)); - ui.tabPane.getDropTarget().setActive(ui.dndEnabled); + DropTarget dropTarget = ui.tabPane.getDropTarget(); + if (dropTarget != null) dropTarget.setActive(ui.dndEnabled); } else if (PropertyKey.COMPONENT_ORIENTATION.equals(key)) { ui.tabPane.doLayout(); ui.tabPane.repaint(); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java index 57274f28..81197c92 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/tabbedpane/DarkTabbedPaneUI.java @@ -32,6 +32,7 @@ import javax.swing.*; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.UIResource; import java.awt.*; +import java.awt.dnd.DropTarget; import java.awt.event.AWTEventListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; @@ -147,8 +148,11 @@ public class DarkTabbedPaneUI extends DarkTabbedPaneUIBridge { protected void installDragSupport() { tabPane.setTransferHandler(TRANSFER_HANDLER); try { - tabPane.getDropTarget().addDropTargetListener(TRANSFER_HANDLER); - tabPane.getDropTarget().setActive(dndEnabled); + DropTarget target = tabPane.getDropTarget(); + if (target != null) { + target.addDropTargetListener(TRANSFER_HANDLER); + target.setActive(dndEnabled); + } } catch (TooManyListenersException e) { e.printStackTrace(); } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/tabframe/DarkTabFrameUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/tabframe/DarkTabFrameUI.java index c8676b3c..6a99624d 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/tabframe/DarkTabFrameUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/tabframe/DarkTabFrameUI.java @@ -35,6 +35,7 @@ import org.pbjar.jxlayer.plaf.ext.transform.TransformUtils; import javax.swing.*; import javax.swing.plaf.ComponentUI; import java.awt.*; +import java.awt.dnd.DropTarget; import java.awt.event.AWTEventListener; import java.awt.event.MouseEvent; import java.util.List; @@ -144,8 +145,11 @@ public class DarkTabFrameUI extends TabFrameUI implements AWTEventListener { protected void installDnD() { tabFrame.setTransferHandler(TRANSFER_HANDLER); try { - tabFrame.getDropTarget().addDropTargetListener(TRANSFER_HANDLER); - tabFrame.getDropTarget().setActive(tabFrame.isDndEnabled()); + DropTarget dropTarget = tabFrame.getDropTarget(); + if (dropTarget != null) { + dropTarget.addDropTargetListener(TRANSFER_HANDLER); + dropTarget.setActive(tabFrame.isDndEnabled()); + } } catch (TooManyListenersException e) { e.printStackTrace(); }