나는 JFileChooser와 JList를 사용한 프레임을 디자인했습니다. JFileChooser에서 JFileChooser에서 폴더를 열고이 폴더에서 내가 선택한 (example.txt) JListChooser에서 JList Contianing 폴더로 드래그 앤 드롭 기능을 수행하고 싶습니다 (예 : 내 JFileChooser에서 짧은 JList 컨티넨스 폴더 이름 인 a, b, c) 파일을 누른 다음 끌어서 JList contianing 폴더에이 수행합니다. 그래서 (example.txt) 파일을 폴더에 있어야합니다.JFileChooser에서 JList로 드래그 앤 드롭하는 방법 containig 폴더
how to do this things.and i have one more problem. in my screenshot Jlist contian Three folder(a,b,c).
so when i click on folder a but i m not able to get inside folder a.
this is my code by using this i have create screenshot.
이 코드에는 JFileChooser 및 JList가 있습니다. 모든 폴더를 JList에 추가했습니다. 이제 JFileChooser에서 폴더를 포함하는 JList로 끌어서 놓기를 수행하려고합니다.
이것은 내 스크린 샷을 만드는 데 사용하는 코드입니다. 이 코드에서는 JFileChooser 및 JList를 만듭니다. 모든 폴더를 JList에 추가했습니다. 이제 JFileChooser에서 폴더를 포함하는 JList로 끌어서 놓기를 수행하려고합니다.
package com.dhananjaynet.swing;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.io.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
import javax.swing.text.*;
import com.dhananjay.swing.upload.ReadAllFolderName;
import java.util.ArrayList;
import java.util.List;
public class ConsolidatorDemo extends JPanel implements ActionListener {
private static final long serialVersionUID = -4487732343062917781L;
JFileChooser fc;
JButton clear;
JTextArea console;
JList dropZone;
DefaultListModel listModel;
JSplitPane childSplitPane, parentSplitPane;
PrintStream ps;
ArrayList<String > directyName;
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));
listModel = new DefaultListModel();
directyName=ReadAllFolderName.ReadDirectory();
dropZone = new JList(listModel);
dropZone.setListData(directyName.toArray());
dropZone.setCellRenderer(new FileCellRenderer());
dropZone.setTransferHandler(new ListTransferHandler(dropZone));
dropZone.setDragEnabled(true);
dropZone.setDropMode(javax.swing.DropMode.INSERT);
dropZone.setBorder(new TitledBorder("Selected files/folders"));
leftLowerPanel.setViewportView(new JScrollPane(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) {
listModel.clear();
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.![this is my GUI][2]
*/
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();
}
});
}
}
class FileCellRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(
list,value,index,isSelected,cellHasFocus);
if (c instanceof JLabel && value instanceof File) {
JLabel l = (JLabel)c;
File f = (File)value;
l.setIcon(FileSystemView.getFileSystemView().getSystemIcon(f));
l.setText(f.getName());
l.setToolTipText(f.getAbsolutePath());
}
return c;
}
}
class ListTransferHandler extends TransferHandler {
private JList list;
ListTransferHandler(JList list) {
this.list = list;
}
@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) list.getModel();
for (Object file : data) {
model.addElement((File)file);
}
return true;
}
private void displayDropLocation(String string) {
System.out.println(string);
}
}
this my other class for Populating all the folder name into JList
package com.dhananjay.swing.upload;
import java.io.File;
import java.util.ArrayList;
public class ReadAllFolderName {
public static ArrayList<String> ReadDirectory() {
String path = "C:\\Users\\SonyBastian\\Desktop\\DMC";
ArrayList<String> files = new ArrayList<String>();
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
System.out.println("method 1");
if (listOfFiles[i].isDirectory()) {
files.add(listOfFiles[i].getName());
/*if (files.endsWith(".txt") || files.endsWith(".TXT")) {
System.out.println(files);
}*/
}
}
return files;
}
}
![enter image description here][3]
[1]: http://i.stack.imgur.com/YAP60.png
Thanks
TNX :
그래서 작성하고 다음과 같은
JList
를 채울하려고합니다. 하지만 내가 JFileChooser에서 JLIst까지 끌어서 놓기를 수행하면 fine.but로 JFileChooser에서 선택한 파일을 JList에 저장하려고합니다. JList가 포함 된 폴더가 세 개 (a, b, c) 있습니다. JFIleChooser에서 파일을 선택하면 JList의 폴더에서 끌어서 놓기를 수행합니다. 그 파일은 그 폴더 안에 있어야합니다. – user3143376