久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將文件從操作系統拖放到 Java 應用程序 (Swing)

Drag#39;n drop files from the OS to Java application (Swing)(將文件從操作系統拖放到 Java 應用程序 (Swing))
本文介紹了將文件從操作系統拖放到 Java 應用程序 (Swing)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

首先讓我說我一直在閱讀 drag'n drop tutorial 和關于 SO 的類似問題,但不幸的是,我對這件事感到更加困惑.我想要實現的目標相對簡單,所以我很驚訝它已經給我帶來了這么多麻煩.我正在編寫一個小型實用程序應用程序,它將一堆結果文件(自定義的 xml 類型)合并到一個大的制表符分隔的文本文件中.大多數功能已經編碼,但是我想為它制作一個像樣的 GUI.

Let me start by saying that I have been reading the drag'n drop tutorial and similar questions asked on SO, but unfortunately I have only gotten more confused about this matter. What I want to achieve is relatively simple so I am surprised that it got me in so much trouble already. I am writing a small utility application which will consolidate a bunch of result files (custom defined xml-type) into a large tab-separated text file. Most of the functionality is already coded, however I wanted to make a decent GUI for it.

我想要的是能夠將文件拖放到組件中(例如 JTextArea)以一種很好的方式(閱讀:不是完整路徑,而是一個小圖標和名稱).我希望能夠提供一個 JFileChooser 來瀏覽文件.然后我將依次解析文件以生成我想要的矩陣.

What I want is to be able to drag'n drop files into a component (for instance JTextArea) in a nice and gracious way (read: not full paths, but instead a small icon and name). I would like to be able to supply a JFileChooser to browse for files as well. I will then parse the files sequentially to produce the matrix I want.

到目前為止,我了解到該框架已經存在,但是任何其他功能都需要自定義構建.我在 Netbeans 中創建了一個測試 GUI,并試圖將一堆文件拖到 JTextArea 上,但它們顯示為文件路徑,并且不可否認它看起來很丑陋.

What I have learned so far is that the framework is already there however any additional functionality needs to be custom built. I have created a test GUI in Netbeans and tried to drag a bunch of files onto a JTextArea, but they appear as file paths, and admittedly it looks very ugly.

我非常感謝有關如何以簡潔的方式解決(或澄清)此問題的任何提示和指導.請注意,我確實打算在多個不同的操作系統(Mac、Win 和 Linux)上使用該軟件.

I would really appreciate any tips and guidance on how to solve (or clarify) this problem in a neat way. Note that I do intend to use the software on multiple different OS (Mac,Win and Linux).

到目前為止,我的代碼基于 Sun 教程中的一個示例,如下所示

the code I have so far is based on one of the examples from Sun tutorials and is as follows

import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.*;

public class ConsolidatorDemo extends JPanel implements ActionListener {
    private static final long serialVersionUID = -4487732343062917781L;
    JFileChooser fc;
    JButton clear;
    JTextArea dropZone, console;
    JSplitPane childSplitPane, parentSplitPane;
    PrintStream ps;

  public ConsolidatorDemo() {
    super(new BorderLayout());

    fc = new JFileChooser();;
    fc.setMultiSelectionEnabled(true);
    fc.setDragEnabled(true);
    fc.setControlButtonsAreShown(false);
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);               


    JPanel fcPanel = new JPanel(new BorderLayout());
    fcPanel.add(fc, BorderLayout.CENTER);

    clear = new JButton("Clear All");
    clear.addActionListener(this);
    JPanel buttonPanel = new JPanel(new BorderLayout());
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    buttonPanel.add(clear, BorderLayout.LINE_END);

    JPanel leftUpperPanel = new JPanel(new BorderLayout());
    leftUpperPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    leftUpperPanel.add(fcPanel, BorderLayout.CENTER);
    leftUpperPanel.add(buttonPanel, BorderLayout.PAGE_END);


    JScrollPane leftLowerPanel = new javax.swing.JScrollPane();
    leftLowerPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    dropZone = new JTextArea();
    dropZone.setColumns(20);
    dropZone.setLineWrap(true);
    dropZone.setRows(5);
    dropZone.setDragEnabled(true);
    dropZone.setDropMode(javax.swing.DropMode.INSERT);
    dropZone.setBorder(new TitledBorder("Selected files/folders"));
    leftLowerPanel.setViewportView(dropZone);

    childSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
            leftUpperPanel, leftLowerPanel);
    childSplitPane.setDividerLocation(400);
    childSplitPane.setPreferredSize(new Dimension(480, 650));

    console = new JTextArea();
    console.setColumns(40);
    console.setLineWrap(true);
    console.setBorder(new TitledBorder("Console"));

    parentSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                    childSplitPane, console);
    parentSplitPane.setDividerLocation(480);
    parentSplitPane.setPreferredSize(new Dimension(800, 650));

    add(parentSplitPane, BorderLayout.CENTER);

}

public void setDefaultButton() {
    getRootPane().setDefaultButton(clear);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == clear) {
        dropZone.setText("");

    }
}

/**
 * Create the GUI and show it. For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);
    try {
      //UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaBlackStarLookAndFeel");
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    }catch (Exception e){
      e.printStackTrace();
    }

    //Create and set up the window.
    JFrame frame = new JFrame("Consolidator!");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    //Create and set up the menu bar and content pane.
    ConsolidatorDemo demo = new ConsolidatorDemo();
    demo.setOpaque(true); //content panes must be opaque
    frame.setContentPane(demo);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    demo.setDefaultButton();
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

推薦答案

這里有一個快速的片段,用于將實際的文件導入 JList(而不是將其字符串表示導入文本組件)并使用自定義渲染器來呈現它很好.它改編自 BasicDnD(在教程中):

here's a quick snippet to import the actual Files into a JList (as opposed to importing its String representation into a text component) and use a custom renderer to present it nicely. It's adapted from the BasicDnD (in the tutorial):

    fileDropper = new JList(new DefaultListModel());
    fileDropper.setDragEnabled(true);
    leftLowerPanel.setViewportView(fileDropper);

    TransferHandler handler =   new TransferHandler() {

        @Override
        public boolean canImport(TransferHandler.TransferSupport info) {
            // we only import FileList
            if (!info.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                return false;
            }
            return true;
        }

        @Override
        public boolean importData(TransferHandler.TransferSupport info) {
            if (!info.isDrop()) {
                return false;
            }

            // Check for FileList flavor
            if (!info.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                displayDropLocation("List doesn't accept a drop of this type.");
                return false;
            }

            // Get the fileList that is being dropped.
            Transferable t = info.getTransferable();
            List<File> data;
            try {
                data = (List<File>)t.getTransferData(DataFlavor.javaFileListFlavor);
            } 
            catch (Exception e) { return false; }
            DefaultListModel model = (DefaultListModel) fileDropper.getModel();
            for (File file : data) {
                model.addElement(file);
            }
            return true;
        }

        private void displayDropLocation(String string) {
            System.out.println(string);
        }
    };
    fileDropper.setTransferHandler(handler);
    fileDropper.setCellRenderer(new DefaultListRenderer(
          StringValues.FILE_NAME, IconValues.FILE_ICON));

無法拒絕顯示 SwingX 渲染器配置 :-) 在核心 java 中,您可以手動完成,類似于

Couldn't resist to showing SwingX renderer config :-) In core java, you would do it manually, something like

   class MyRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(...) {
            super.getList...
            if (value instanceof File) {
                setText(FileSystemView.getFileSystemView().getDisplayName(value);
                setIcon(FileSystemView.getFileSystemView().getSystemIcon(value);
            } 
            return this;
        }

   }

這篇關于將文件從操作系統拖放到 Java 應用程序 (Swing)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 欧美激情国产精品 | 日韩在线观看一区 | 亚洲成av人片在线观看无码 | 一区二区av| 国产欧美精品一区二区 | 网站国产 | 黄色免费在线观看 | 久久亚洲欧美日韩精品专区 | 在线看亚洲 | 色香婷婷 | 91精品国产一区二区三区香蕉 | 成人亚洲视频 | 久久久久久亚洲精品 | 91精品久久久久久久久久入口 | 美女视频一区二区三区 | 国产精品久久久久久一区二区三区 | 国产日韩欧美中文 | 精品国产欧美一区二区三区成人 | 亚洲性爰 | 欧美区日韩区 | 免费能直接在线观看黄的视频 | 国产中文在线 | 亚州综合在线 | 精品一区二区三区视频在线观看 | 韩国av影院 | 成人免费视频网站在线看 | 97精品国产97久久久久久免费 | 中文字字幕一区二区三区四区五区 | 国产一区二区 | 在线亚洲免费视频 | 日韩在线一区二区三区 | 国产xxx在线观看 | 成人激情视频免费在线观看 | 国产一区二区三区视频 | 天天弄| 国产一区二区三区久久久久久久久 | 精品国产一二三区 | 亚洲视频一| 精品免费国产一区二区三区 | 亚洲成人一区二区三区 | 在线视频成人 |