File Browser in Java

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • File Browser in Java

    Hallo. Ich möchte sowas wie im Anhang gezeigt in mein Javaprogramm einbauen.

    Aber ich habe keine Ahnung wie. Wenn jemand helfen könnte, wäre ich sehr dankbar. Aber bitte kein fertigen Code, sondern villeicht ein paar Informationen womit und wie man das macht. Das Programm möchte ich selbst hinkriegen. Vielen Dank im voraus.
    Bilder
    • FileBrowser1.JPG

      36,76 kB, 249×287, 4.084 mal angesehen
  • Mein Programm, das Lines of Code in einem vorgegebenem Verzeichniss zählen soll ist noch nicht vertig, aber ich kann ein Beispiel hier posten, wie man ein Fenster mit Verzeichnissbaum programmieren kann.

    Quellcode

    1. /**
    2. *
    3. * ===================================================
    4. * This program contains code from the book "Swing"
    5. * 2nd Edition by Matthew Robinson and Pavel Vorobiev
    6. * http://www.spindoczine.com/sbe
    7. * ===================================================
    8. */
    9. import java.awt.*;
    10. import java.io.*;
    11. import java.util.*;
    12. import javax.swing.*;
    13. import javax.swing.tree.*;
    14. import javax.swing.border.*;
    15. import javax.swing.event.*;
    16. public class DirTree
    17. extends JFrame {
    18. public static final String APP_NAME = "Directories Tree";
    19. public static final ImageIcon ICON_COMPUTER =
    20. new ImageIcon("computer.gif");
    21. public static final ImageIcon ICON_DISK =
    22. new ImageIcon("disk.gif");
    23. public static final ImageIcon ICON_FOLDER =
    24. new ImageIcon("folder.gif");
    25. public static final ImageIcon ICON_EXPANDEDFOLDER =
    26. new ImageIcon("expandedfolder.gif");
    27. protected JTree m_tree;
    28. protected DefaultTreeModel m_model;
    29. protected JTextField m_display;
    30. public DirTree() {
    31. super(APP_NAME);
    32. setSize(400, 300);
    33. DefaultMutableTreeNode top = new DefaultMutableTreeNode(
    34. new IconData(ICON_COMPUTER, null, "Computer"));
    35. DefaultMutableTreeNode node;
    36. File[] roots = File.listRoots();
    37. for (int k=0; k<roots.length; k++) {
    38. node = new DefaultMutableTreeNode(new IconData(ICON_DISK,
    39. null, new FileNode(roots[k])));
    40. top.add(node);
    41. node.add( new DefaultMutableTreeNode(new Boolean(true)));
    42. }
    43. m_model = new DefaultTreeModel(top);
    44. m_tree = new JTree(m_model);
    45. m_tree.putClientProperty("JTree.lineStyle", "Angled");
    46. IconCellRenderer renderer = new
    47. IconCellRenderer();
    48. m_tree.setCellRenderer(renderer);
    49. m_tree.addTreeExpansionListener(new
    50. DirExpansionListener());
    51. m_tree.addTreeSelectionListener(new
    52. DirSelectionListener());
    53. m_tree.getSelectionModel().setSelectionMode(
    54. TreeSelectionModel.SINGLE_TREE_SELECTION);
    55. m_tree.setShowsRootHandles(true);
    56. m_tree.setEditable(false);
    57. JScrollPane s = new JScrollPane();
    58. s.getViewport().add(m_tree);
    59. getContentPane().add(s, BorderLayout.CENTER);
    60. m_display = new JTextField();
    61. m_display.setEditable(false);
    62. m_display.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
    63. getContentPane().add(m_display, BorderLayout.NORTH);
    64. }
    65. DefaultMutableTreeNode getTreeNode(TreePath path) {
    66. return (DefaultMutableTreeNode)(path.getLastPathComponent());
    67. }
    68. FileNode getFileNode(DefaultMutableTreeNode node) {
    69. if (node == null)
    70. return null;
    71. Object obj = node.getUserObject();
    72. if (obj instanceof IconData)
    73. obj = ((IconData)obj).getObject();
    74. if (obj instanceof FileNode)
    75. return (FileNode)obj;
    76. else
    77. return null;
    78. }
    79. // Make sure expansion is threaded and updating the tree model
    80. // only occurs within the event dispatching thread.
    81. class DirExpansionListener implements TreeExpansionListener {
    82. public void treeExpanded(TreeExpansionEvent event) {
    83. final DefaultMutableTreeNode node = getTreeNode(
    84. event.getPath());
    85. final FileNode fnode = getFileNode(node);
    86. Thread runner = new Thread() {
    87. public void run() {
    88. if (fnode != null && fnode.expand(node)) {
    89. Runnable runnable = new Runnable() {
    90. public void run() {
    91. m_model.reload(node);
    92. }
    93. };
    94. SwingUtilities.invokeLater(runnable);
    95. }
    96. }
    97. };
    98. runner.start();
    99. }
    100. public void treeCollapsed(TreeExpansionEvent event) {}
    101. }
    102. class DirSelectionListener
    103. implements TreeSelectionListener {
    104. public void valueChanged(TreeSelectionEvent event) {
    105. DefaultMutableTreeNode node = getTreeNode(
    106. event.getPath());
    107. FileNode fnode = getFileNode(node);
    108. if (fnode != null)
    109. m_display.setText(fnode.getFile().
    110. getAbsolutePath());
    111. else
    112. m_display.setText("");
    113. }
    114. }
    115. public static void main(String argv[]) {
    116. DirTree frame = new DirTree();
    117. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    118. frame.setVisible(true);
    119. }
    120. }
    121. class IconCellRenderer
    122. extends DefaultTreeCellRenderer {
    123. public IconCellRenderer() {
    124. setLeafIcon(null);
    125. setOpenIcon(null);
    126. }
    127. public Component getTreeCellRendererComponent(JTree tree,
    128. Object value, boolean sel, boolean expanded, boolean leaf,
    129. int row, boolean hasFocus) {
    130. // Invoke default implementation
    131. Component result = super.getTreeCellRendererComponent(tree,
    132. value, sel, expanded, leaf, row, hasFocus);
    133. DefaultMutableTreeNode node =
    134. (DefaultMutableTreeNode)value;
    135. Object obj = node.getUserObject();
    136. setText(obj.toString());
    137. if (obj instanceof Boolean)
    138. setText("Retrieving data...");
    139. if (obj instanceof IconData) {
    140. IconData idata = (IconData)obj;
    141. if (expanded)
    142. setIcon(idata.getExpandedIcon());
    143. else
    144. setIcon(idata.getIcon());
    145. }
    146. else
    147. setIcon(null);
    148. return result;
    149. }
    150. }
    151. class IconData {
    152. protected Icon m_icon;
    153. protected Icon m_expandedIcon;
    154. protected Object m_data;
    155. public IconData(Icon icon, Object data) {
    156. m_icon = icon;
    157. m_expandedIcon = null;
    158. m_data = data;
    159. }
    160. public IconData(Icon icon, Icon expandedIcon, Object data) {
    161. m_icon = icon;
    162. m_expandedIcon = expandedIcon;
    163. m_data = data;
    164. }
    165. public Icon getIcon() {
    166. return m_icon;
    167. }
    168. public Icon getExpandedIcon() {
    169. return m_expandedIcon!=null ? m_expandedIcon : m_icon;
    170. }
    171. public Object getObject() {
    172. return m_data;
    173. }
    174. public String toString() {
    175. return m_data.toString();
    176. }
    177. }
    178. class FileNode {
    179. protected File m_file;
    180. public FileNode(File file) {
    181. m_file = file;
    182. }
    183. public File getFile() {
    184. return m_file;
    185. }
    186. public String toString() {
    187. return m_file.getName().length() > 0 ? m_file.getName() :
    188. m_file.getPath();
    189. }
    190. // Alternatively we copud sub-class TreeNode
    191. public boolean expand(DefaultMutableTreeNode parent) {
    192. DefaultMutableTreeNode flag =
    193. (DefaultMutableTreeNode)parent.getFirstChild();
    194. if (flag==null) // No flag
    195. return false;
    196. Object obj = flag.getUserObject();
    197. if (!(obj instanceof Boolean))
    198. return false; // Already expanded
    199. parent.removeAllChildren(); // Remove Flag
    200. File[] files = listFiles();
    201. if (files == null)
    202. return true;
    203. Vector v = new Vector();
    204. for (int k=0; k<files.length; k++) {
    205. File f = files[k];
    206. if (!(f.isDirectory()))
    207. continue;
    208. FileNode newNode = new FileNode(f);
    209. boolean isAdded = false;
    210. for (int i=0; i<v.size(); i++) {
    211. FileNode nd = (FileNode)v.elementAt(i);
    212. if (newNode.compareTo(nd) < 0) {
    213. v.insertElementAt(newNode, i);
    214. isAdded = true;
    215. break;
    216. }
    217. }
    218. if (!isAdded)
    219. v.addElement(newNode);
    220. }
    221. for (int i=0; i<v.size(); i++) {
    222. FileNode nd = (FileNode)v.elementAt(i);
    223. IconData idata = new IconData(DirTree.ICON_FOLDER,
    224. DirTree.ICON_EXPANDEDFOLDER, nd);
    225. DefaultMutableTreeNode node = new
    226. DefaultMutableTreeNode(idata);
    227. parent.add(node);
    228. if (nd.hasSubDirs())
    229. node.add(new DefaultMutableTreeNode(
    230. new Boolean(true) ));
    231. }
    232. return true;
    233. }
    234. public boolean hasSubDirs() {
    235. File[] files = listFiles();
    236. if (files == null)
    237. return false;
    238. for (int k=0; k<files.length; k++) {
    239. if (files[k].isDirectory())
    240. return true;
    241. }
    242. return false;
    243. }
    244. public int compareTo(FileNode toCompare) {
    245. return m_file.getName().compareToIgnoreCase(
    246. toCompare.m_file.getName() );
    247. }
    248. protected File[] listFiles() {
    249. if (!m_file.isDirectory())
    250. return null;
    251. try {
    252. return m_file.listFiles();
    253. }
    254. catch (Exception ex) {
    255. JOptionPane.showMessageDialog(null,
    256. "Error reading directory "+m_file.getAbsolutePath(),
    257. DirTree.APP_NAME, JOptionPane.WARNING_MESSAGE);
    258. return null;
    259. }
    260. }
    261. }
    Alles anzeigen
  • Dateien öffnen, die mit Tree/JTree ausgewählt wurden?

    Munna schrieb:

    Jedoch möchte ich keine Baumstruktur, sondern will die Ordner öffnen können[...]
    Hallo Leute! :)
    Ich hab mich mal so in eurem Blog umgelesen und mich gefragt, ob nicht beides geht (Baum/Tree UND Öffnen von somit ausgewählten Dateien).
    Falls jemand darauf eine Antwort hat, bitte melden!
    Grüße,
    GuestUser