Note that there are some explanatory texts on larger screens.

plurals
  1. PORefreshing a fileSystemViewer in java
    primarykey
    data
    text
    <p>Im Trying to develop a small application that uses a gui to move files from anywhere on the system. I have the code to move the files and they do indeed move when selected and buttons pressed but I dont know how to refresh the filesystem viewer to reflect the change. The code I have to set up the system viewer is below:</p> <pre><code>public class FileMover { //Start of Global Variables private JTree tree; private DefaultTreeModel treeModel; private FileSystemView fileSystemView; protected File currentFile; protected LinkedList fileLocations; protected JTree movedTree; protected JPanel areaLeft; protected JPanel areaRight; protected JPanel areaMiddle; protected final JFrame openFrame; //end of global variables. //Constructor for FileMover public FileMover() { openFrame = new JFrame("File Mover"); createFileMover(); } public void createFileMover(){ Container contentPane = this.openFrame.getContentPane(); fileLocations = new LinkedList(); contentPane.setLayout(new BorderLayout()); areaLeft = new JPanel(); areaRight = new JPanel(); areaMiddle = new JPanel(); contentPane.add(areaLeft, BorderLayout.WEST); contentPane.add(areaRight, BorderLayout.EAST); contentPane.add(areaMiddle, BorderLayout.CENTER); areaLeft.add(createSystemView()); movedTree = new JTree(fileLocations.toArray()); JScrollPane movedPane = new JScrollPane(movedTree); JButton moveRightButton = new JButton("-&gt;"); JButton moveLeftButton = new JButton("&lt;-"); JButton refresh = new JButton("Refresh"); areaMiddle.setLayout(new GridLayout(1,2)); areaMiddle.add(moveRightButton); areaMiddle.add(refresh); areaMiddle.add(moveLeftButton); //actualy move the file moveRightButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.out.println("Moving file: "+ currentFile.getName()); fileLocations.add(currentFile); try { //move the file to the correct location. moveFile(currentFile); } catch (IOException ex) { Logger.getLogger(FileMover.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(fileLocations.getFirst().toString()); } }); //refresh the gui refresh.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ refresh(); } }); //finish setting up the frame openFrame.setSize(1280, 768); openFrame.setLocationRelativeTo(null); openFrame.setDefaultCloseOperation(3); openFrame.setResizable(false); openFrame.pack(); openFrame.setVisible(true); } /** Add the files that are contained within the directory of this node. */ private void showChildren(final DefaultMutableTreeNode node) { tree.setEnabled(false); SwingWorker&lt;Void, File&gt; worker = new SwingWorker&lt;Void, File&gt;() { @Override public Void doInBackground() { File file = (File) node.getUserObject(); if (file.isDirectory()) { File[] files = fileSystemView.getFiles(file, true); //!! if (node.isLeaf()) { for (File child : files) { publish(child); } } } return null; } @Override protected void process(List&lt;File&gt; chunks) { for (File child : chunks) { node.add(new DefaultMutableTreeNode(child)); } } @Override protected void done() { tree.setEnabled(true); } }; worker.execute(); } /** Update the File details view with the details of this File. */ private void setFileDetails(File file) { System.out.println("Path: "+ file.getPath()); System.out.println("Name: "+ fileSystemView.getSystemDisplayName(file)); } private void refresh(){ //refresh the tree here } private JScrollPane createSystemView(){ //file syatem hierarchy fileSystemView = FileSystemView.getFileSystemView(); // the File tree DefaultMutableTreeNode root = new DefaultMutableTreeNode(); treeModel = new DefaultTreeModel(root); TreeSelectionListener treeSelectionListener = new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent tse){ DefaultMutableTreeNode node = (DefaultMutableTreeNode)tse.getPath().getLastPathComponent(); showChildren(node); setFileDetails((File)node.getUserObject()); currentFile = (File)node.getUserObject(); } }; // show the file system roots. File[] roots = fileSystemView.getRoots(); for (File fileSystemRoot : roots) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(fileSystemRoot); root.add( node ); File[] files = fileSystemView.getFiles(fileSystemRoot, true); for (File file : files) { if (file.isDirectory()) { node.add(new DefaultMutableTreeNode(file)); } } } tree = new JTree(treeModel); tree.setRootVisible(false); tree.addTreeSelectionListener(treeSelectionListener); tree.setCellRenderer(new FileTreeCellRenderer()); tree.expandRow(0); JScrollPane treeScroll = new JScrollPane(tree); tree.setVisibleRowCount(15); Dimension preferredSize = treeScroll.getPreferredSize(); Dimension widePreferred = new Dimension( 200, (int)preferredSize.getHeight()); treeScroll.setPreferredSize( widePreferred ); return treeScroll; } </code></pre> <p>The move left button and the area right are not finished but what I need is when I select a node in the tree and click the right arrow button the file/folder the node reflects is moved internally by my moveFile code and that works. but that change is not reflected in the tree so how can show this change in the tree i.e refresh the tree to show the current state of the filesystem?</p> <p>I've tried treeModel.reload(); but that doesnt seem to work and throws a null pointer exception. </p> <p>I've tried :</p> <pre><code>areaLeft.removeAll(); areaLeft.add(createSystemView()); </code></pre> <p>thinking that it may refresh it by recreating the system view but that doesnt seem to do anything.</p> <p>Help here would be most appreciated!</p> <p>Edit: Below is the requested code for the file tree renderer:</p> <pre><code>/** A TreeCellRenderer for a File. */ class FileTreeCellRenderer extends DefaultTreeCellRenderer { private static final long serialVersionUID = -7799441088157759804L; private FileSystemView fileSystemView; private JLabel label; FileTreeCellRenderer() { label = new JLabel(); label.setOpaque(true); fileSystemView = FileSystemView.getFileSystemView(); } @Override public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)value; File file = (File)node.getUserObject(); label.setIcon(fileSystemView.getSystemIcon(file)); label.setText(fileSystemView.getSystemDisplayName(file)); label.setToolTipText(file.getPath()); if (selected) { label.setBackground(backgroundSelectionColor); label.setForeground(textSelectionColor); } else { label.setBackground(backgroundNonSelectionColor); label.setForeground(textNonSelectionColor); } return label; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload