1

だから私は興味深い問題を抱えています。テーブルの各セルにプログレスバーを配置したいアプリケーションがあります。安全のために、自分がやりたいことを実行するスタンドアロンアプリを作成しましたが、既存のコードに組み込むのに問題があります。各ファイルから必要なものに迷いました。アプリテーブルとプログレスバーテーブルコードを投稿しています。これらの2つのメッシュを取得するか、単純にアプリテーブルのプログレスバーが必要です。助けていただければ幸いです。

アプリケーションテーブル

package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.AlgorithmParameters;
import java.security.spec.KeySpec;
import javax.crypto.Cipher; 
import javax.crypto.CipherInputStream; 
import javax.crypto.CipherOutputStream; 
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class FileTable extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JTable table;
    private DefaultTableModel tableModel = 
            new DefaultTableModel(new String[]  
                        {"File",      "Size", "Status" }, 0);
    private File dir;
    private File temp;
    private JPopupMenu popup;
    private String key;
    private PasswordStorage passwordStorage;
    private JFileChooser fileChooser;
    private static String salt = "loquetdeliciouslysalty";
    private static byte[] IV;

    public FileTable() {

        // Set Layout Manager
        setLayout(new BorderLayout());

        // Create Swing Components
        table = new JTable();
        table.setModel(tableModel);
        table.setDropTarget(new TableDnD(table));
        table.setShowGrid(false);
        table.setFillsViewportHeight(true);
        table.getColumnModel().getColumn(0).setPreferredWidth(250);

        passwordStorage = new PasswordStorage();
        fileChooser = new JFileChooser();
        popup = new JPopupMenu();

        JMenuItem removeItem = new JMenuItem("Remove");
        removeItem.setIcon(new ImageIcon("removeMenu.png"));
        popup.add(removeItem);

        // Add Components to pane
        add(new JScrollPane(table), BorderLayout.CENTER);

        table.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                int row = table.rowAtPoint(e.getPoint());
                table.getSelectionModel().setSelectionInterval(row, row);

                if(e.getButton() == MouseEvent.BUTTON3) {
                    popup.show(table, e.getX(), e.getY());
                }
            }
        });

        removeItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                int row = table.getSelectedRow();
                if(row > -1) {
                    tableModel.removeRow(table.getSelectedRow());
                }
            }
           });
       }

    public boolean isTableEmpty() {

        if(tableModel.getRowCount() == 0) {
            return true;
        }
        else {
            return false;
        }
    }

    public void addFile(File file) {
         tableModel.addRow(
            new Object[]{file, file.length() + " kb",Not Processed"});
    }

    public void removeFile() {
        int[] rows = table.getSelectedRows();

        for(int i = 0; i < rows.length; i++) {
            tableModel.removeRow(rows[i]-i);
        }
    }

    public void clearTable()
    {
        int rowCount = tableModel.getRowCount();

        for(int i = 0; i < rowCount; i++) {
            tableModel.removeRow(0);
        }

        table.removeAll();
    }
}

プログレスバーテーブル

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;


public class UpdateTable 
{           
    public static void main(String[] args) 
    {
        new UpdateTable();
    }

    public UpdateTable() 
    {
        EventQueue.invokeLater(new Runnable() 
        {
            @Override
            public void run() 
            {
                try 
                {                                  
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | 
                        InstantiationException | IllegalAccessException |    
                                UnsupportedLookAndFeelException ex) {
                }

                UpdatableTableModel model = new UpdatableTableModel();
                JTable table = new JTable();
                table.setModel(model);

                table.getColumn("Status").setCellRenderer(new ProgressCellRender());
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                FileFinderWorker worker = new FileFinderWorker(model);
                worker.execute();

            }
        });
    }

    public class ProgressCellRender extends JProgressBar implements TableCellRenderer 
    {       
        @Override
        public Component getTableCellRendererComponent(JTable
            table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
        {
            int progress = 0;
            if (value instanceof Float) 
            {
                progress = Math.round(((Float) value) * 100f);
            } else if (value instanceof Integer) 
            {
                progress = (int) value;
            }
            setValue(progress);
            return this;
        }
    }

    public class RowData
    {       
        private File file;
        private String type;
        private long length;
        private float status;

        public RowData(File file, String type) 
        {
            this.file = file;
            this.type = type;
            this.length = file.length();
            this.status = 0f;
        }

        public File getFile() 
        {
            return file;
        }   

        public long getLength() 
        {
            return length;
        }

        public float getStatus() 
        {
            return status;
        }

        public String getType() 
        {
            return type;
        }

        public void setStatus(float status) 
        {
            this.status = status;
        }
    }

    public class UpdatableTableModel extends AbstractTableModel 
    {
        private List<RowData> rows;
        private Map<File, RowData> mapLookup;

        public UpdatableTableModel() 
        {
            rows = new ArrayList<>(25);
            mapLookup = new HashMap<>(25);
        }

        @Override
        public int getRowCount() 
        {
            return rows.size();
        }

        @Override
        public int getColumnCount() 
        {
            return 4;
        }

        @Override
        public String getColumnName(int column) 
        {
            String name = "??";
            switch (column) {
                case 0:
                    name = "File";
                    break;
                case 1:
                    name = "File Type";
                    break;
                case 2:
                    name = "Size";
                    break;
                case 3:
                    name = "Status";
                    break;
            }
            return name;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) 
        {
            RowData rowData = rows.get(rowIndex);
            Object value = null;
            switch (columnIndex) 
            {
                case 0:
                    value = rowData.getFile();
                    break;
                case 1:
                    value = rowData.getType();
                    break;
                case 2:
                    value = rowData.getLength();
                    break;
                case 3:
                    value = rowData.getStatus();
                    break;
            }
            return value;
        }

        @Override//new value  //row to change  //column to change
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) 
        {
            RowData rowData = rows.get(rowIndex);
            switch (columnIndex)
            {
                case 3:
                    if (aValue instanceof Float) 
                    {
                        rowData.setStatus((float) aValue);
                    }
                    break;
            }
        }

        public void addFile(File file) 
        {
            RowData rowData = new RowData(file, "A File");
            mapLookup.put(file, rowData);
            rows.add(rowData);
            fireTableRowsInserted(rows.size() - 1, rows.size() - 1);
        }

        protected void updateStatus(File file, int progress) 
        {
            RowData rowData = mapLookup.get(file);
            if (rowData != null) {
                int row = rows.indexOf(rowData);
                float p = (float) progress / 100f;
                setValueAt(p, row, 3);
                fireTableCellUpdated(row, 3);
            }
        }
    }

    public class FileFinderWorker extends SwingWorker<List<File>, File> 
    {
        private UpdatableTableModel model;

        public FileFinderWorker(UpdatableTableModel model) 
        {
            this.model = model;
        }

        @Override
        protected void process(List<File> chunks) //data chunks 
        {
            for (File file : chunks) 
            {
                model.addFile(file);
            }
        }

        @Override
        protected List<File> doInBackground() throws Exception
        {
            String usrHome = "user.home";   // takes the entire home dir
                                                                // user.dir will use working directory
                                                                // (wherever you save your Java shit)
            File files[] = new 
                File(System.getProperty(usrHome)).listFiles();//grabbing all files from directory.
            List<File> lstFiles = new ArrayList<>(Arrays.asList(files));
            for (File file : lstFiles) 
            {
                publish(file);
            }
            return lstFiles;
        }

        @Override
        protected void done() 
        {
            try {
                List<File> files = get();
                for (File file : files) 
                {
                    new FileReaderWorker(model, file).execute();
                }
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        }
    }

    public class FileReaderWorker extends SwingWorker<File, File> 
    {
        private File currentFile;
        private UpdatableTableModel model;

        public FileReaderWorker(UpdatableTableModel model, File file) 
        {
            this.currentFile = file;
            this.model = model;

            addPropertyChangeListener(new PropertyChangeListener() 
            {
                @Override
                public void propertyChange(PropertyChangeEvent evt) 
                {
                    if (evt.getPropertyName().equals("progress")) 
                    {
                        FileReaderWorker.this.model.updateStatus(currentFile, (int) evt.getNewValue());
                    }
                }
            });
        }

        @Override
        protected File doInBackground() throws Exception
        {
            if (currentFile.isFile()) 
            {
                setProgress(0);
                long fileLength = currentFile.length();
                BufferedReader reader = null;
                char[] cbuf = new char[1024];//change this if everything loads too fast
                try {                       

                    reader = new BufferedReader(new FileReader(currentFile));
                    int bytesRead = -1;
                    int totalBytesRead = 0;
                    while ((bytesRead = reader.read(cbuf)) != -1) {
                        totalBytesRead += bytesRead;
                        int progress = (int) Math.round(((double) totalBytesRead / (double) fileLength) *
                                    100d);
                        setProgress(progress);
                        Thread.sleep(25);
                    }
                    setProgress(100);
                } catch (Exception e) {
                    e.printStackTrace();
                    setProgress(100);
                } finally {
                    try {
                        reader.close();
                    } catch (Exception e) {
                    }
                }
            } else {
                setProgress(100);
            }
            return currentFile;
        }
    }
}

プログレスバーはアプリケーションに入る必要があります

4

1 に答える 1

1

AOPアスペクトJを調べる必要があります

これを使用して、タスクを実行できます

于 2012-12-17T06:53:16.997 に答える