sclass JFileDropTarget extends MetaWithChangeListeners is Swingable { settable S msg = "Drop file here or click the button"; // currently dropped file settableWithVar File file; settable File defaultDirectory; // file can also be null event fileSelected(File file); SingleComponentPanel scp = scp(); *() { varFile().onChange(file -> { updateComponent(); fileSelected(file); }); } cachedVisualize { if (!scp.hasComponent()) scp.setComponent(makeComponent()); ret jHandleFileDrop((IVF1) l1 setFile, scp); } void updateComponent { scp.setComponent(makeComponent()); } JComponent makeComponent() { var file = file(); ret file == null ? noFileComponent() : componentForFile(file); } // What to show before a file was selected/dropped JComponent noFileComponent() { ret withBorder(BorderFactory.createDashedBorder(Color.gray, 2, 5, 5, true), jfullcenter(vstackWithSpacing( jcenteredlabel(msg), jThreadedButton("Open file...", l0 openDialog) ))); } swappable JComponent componentForFile(File file) { ret jfullcenter(jFilePathLabel(file).visualize()); } void setComponent(JComponent component) { scp.setComponent(component); } void openDialog { new JFileChooser fc; fc.setDialogTitle("Open file"); fc.setCurrentDirectory(or(defaultDirectory(), userDir())); if (fc.showOpenDialog(scp) == JFileChooser.APPROVE_OPTION) setFile(fc.getSelectedFile()); } }