简体   繁体   English

将JList与模型一起使用?

[英]Using JList with a model?

I'm making an application that lets you add files and then compress them but how to get the files from my hard drive or any hard drive for that matter into my application? 我正在创建一个应用程序,允许您添加文件,然后压缩它们,但如何从我的硬盘驱动器或任何硬盘驱动器中获取文件进入我的应用程序? I can get the file through a filereader but how to put it into my GUI? 我可以通过文件阅读器获取文件但是如何将其放入我的GUI?

I read that defaultListModel is the way to go but am unsure. 我读到defaultListModel是要走的路,但我不确定。

public class LockNCompressWindow
{
    public static void main(String[] args)
    { 
        LockFrame w = new LockFrame();  
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        w.setSize(500,500);
        w.setResizable(false);
        w.setVisible(true);
    }
}

class LockFrame extends JFrame implements ActionListener
{
    //Declaring MenuBar and components 
    JMenuBar menuBar = new JMenuBar(); 
    JMenu menu = new JMenu("File");
    JMenuItem MenuItemClose = new JMenuItem("Close"); 

    //Declaring Panels 
    JPanel PanelNorth = new JPanel(); 
    JPanel PanelCenter = new JPanel();
    JPanel PanelSouth = new JPanel(); 

    //Declaring Buttons 
    JButton ButtonAddFile = new JButton("Add File");
    JButton ButtonDeleteFile = new JButton("Delete File"); 
    JButton ButtonLock = new JButton("Lock");
    JButton ButtonUnlock = new JButton("Unlock");

    //Declaring FileChooser
    JFileChooser chooser = new JFileChooser(); 

    public LockFrame()
    {
        //Title of the frame
        super("Lock and Zip");

        //Creating Menu bar
        super.setJMenuBar(menuBar);

        //Creating the Menu Tab 
        menuBar.add(menu);

        //Creating a Menu Item
        menu.add(MenuItemClose);

        //Adding North Panel 
        PanelNorth.setBorder(BorderFactory.createEtchedBorder());

        super.add(PanelNorth);

        PanelNorth.add(ButtonAddFile); 
        PanelNorth.add(ButtonDeleteFile);
        add(PanelNorth,BorderLayout.NORTH);

        //Adding Center Panel to Frame
        super.add(PanelCenter);

        //Adding Scroll Pane 
        JScrollPane listScroller = new JScrollPane();
        listScroller.setPreferredSize(new Dimension(400,360));

        PanelCenter.add(listScroller);
        add(PanelCenter, BorderLayout.CENTER);

        //Adding South Panel
        PanelSouth.setBorder(BorderFactory.createEtchedBorder());

        super.add(PanelCenter);

        PanelSouth.add(ButtonLock); 
        PanelSouth.add(ButtonUnlock);
        PanelSouth.add(ButtonPassword);
        add(PanelSouth,BorderLayout.SOUTH);

        //Action Listeners
        ButtonAddFile.addActionListener(this);
        ButtonPassword.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        Object Source = e.getSource();
        int ReturnValue;

        if (Source == ButtonAddFile)
        {
            ReturnValue = chooser.showOpenDialog(LockFrame.this);
            if (ReturnValue == JFileChooser.APPROVE_OPTION()) 
            {
                File file = chooser.getSelectedFile();
                //Add the file to you center panel
            } 
        }

        if (Source == ButtonDeleteFile)
        {

        }

        if (Source == ButtonLock)
        {

        }

        if (Source == ButtonUnlock)
        {

        }

        if (Source == ButtonPassword)
        {

        }
    }
}

You might like to take a read through How to user Lists for more details, but the basic concept is rather simple. 您可能希望阅读如何使用用户列表以获取更多详细信息,但基本概念非常简单。

Create you're self a ListModel . 创建你自己的ListModel In this example, I customised my own, you could just as easily use a DefaultListModel , and add the objects you want to it. 在这个例子中,我自己定制,你可以像使用DefaultListModel一样轻松地添加你想要的对象。

Create you're self a JList and apply your model to it and, well, that's about it... 创建你自己是一个JList并将你的模型应用到它,嗯,这是关于它...

public class FileAdder {

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

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

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FileAdderPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class FileAdderPane extends JPanel {

        private JList fileList;
        private JFileChooser chooser;

        public FileAdderPane() {
            setLayout(new BorderLayout());

            fileList = new JList(new MyFileListModel());
            JButton addMore = new JButton("Add More");
            addMore.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (chooser == null) {
                        chooser = new JFileChooser();
                        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                        chooser.setMultiSelectionEnabled(true);
                    }
                    switch (chooser.showOpenDialog(FileAdderPane.this)) {
                        case JFileChooser.APPROVE_OPTION:
                            File[] files = chooser.getSelectedFiles();
                            if (files != null && files.length > 0) {
                                MyFileListModel model = (MyFileListModel) fileList.getModel();
                                for (File file : files) {
                                    model.add(file);
                                }
                            }
                            break;
                    }
                }
            });

            add(new JScrollPane(fileList));
            add(addMore, BorderLayout.SOUTH);
        }
    }

    public class MyFileListModel extends AbstractListModel {

        private List<File> files = new ArrayList<File>(25);

        @Override
        public int getSize() {
            return files.size();
        }

        @Override
        public Object getElementAt(int index) {
            return files.get(index);
        }

        public void add(File file) {
            files.add(file);
            fireIntervalAdded(this, files.size() - 1, files.size() - 1);
        }

        public void remove(File file) {
            int index = files.indexOf(file);
            files.remove(file);
            fireIntervalRemoved(this, index, index);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM