Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

62
LINES

< > BotCompany Repo | #1035146 // JFileDropTarget

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (14860L/88K).

1  
sclass JFileDropTarget extends MetaWithChangeListeners is Swingable {
2  
  settable S msg = "Drop file here or click the button";
3  
  
4  
  // currently dropped file
5  
  settableWithVar File file;
6  
  
7  
  settable File defaultDirectory;
8  
  
9  
  // file can also be null
10  
  event fileSelected(File file);
11  
  
12  
  SingleComponentPanel scp = scp();
13  
  
14  
  *() {
15  
    varFile().onChange(file -> {
16  
      updateComponent();
17  
      fileSelected(file);
18  
    });
19  
  }
20  
  
21  
  cachedVisualize {
22  
    if (!scp.hasComponent())
23  
      scp.setComponent(makeComponent());
24  
    ret jHandleFileDrop((IVF1<File>) l1 setFile, scp);
25  
  }
26  
  
27  
  void updateComponent {
28  
    scp.setComponent(makeComponent());
29  
  }
30  
31  
  JComponent makeComponent() {
32  
    var file = file();
33  
    ret file == null
34  
      ? noFileComponent()
35  
      : componentForFile(file);
36  
  }
37  
  
38  
  // What to show before a file was selected/dropped
39  
  JComponent noFileComponent() {
40  
    ret withBorder(BorderFactory.createDashedBorder(Color.gray, 2, 5, 5, true),
41  
      jfullcenter(vstackWithSpacing(
42  
        jcenteredlabel(msg),
43  
        jThreadedButton("Open file...", l0 openDialog)
44  
      )));
45  
  }
46  
  
47  
  swappable JComponent componentForFile(File file) {
48  
    ret jfullcenter(jFilePathLabel(file).visualize());
49  
  }
50  
  
51  
  void setComponent(JComponent component) {
52  
    scp.setComponent(component);
53  
  }
54  
  
55  
  void openDialog {
56  
    new JFileChooser fc;
57  
    fc.setDialogTitle("Open file");
58  
    fc.setCurrentDirectory(or(defaultDirectory(), userDir()));
59  
    if (fc.showOpenDialog(scp) == JFileChooser.APPROVE_OPTION)
60  
      setFile(fc.getSelectedFile());
61  
  }
62  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035146
Snippet name: JFileDropTarget
Eternal ID of this version: #1035146/11
Text MD5: 7bb34338122681235926aaa31834d643
Transpilation MD5: acfb29376523b37dbf70dd52b4f00922
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-28 15:34:51
Source code size: 1611 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 199 / 350
Version history: 10 change(s)
Referenced in: [show references]