在模型中使用JList? [英] Using JList with a model?

查看:165
本文介绍了在模型中使用JList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,让您添加文件,然后压缩它们,但如何从我的硬盘驱动器或任何硬盘上的文件到我的应用程序的文件?我可以通过filereader获取文件,但如何把它放到我的GUI?



我读取defaultListModel是要走的路,但我不确定。 b
$ b

  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
{
//声明MenuBar和组件
JMenuBar menuBar = new JMenuBar() ;
JMenu menu = new JMenu(File);
JMenuItem MenuItemClose = new JMenuItem(Close);

//声明面板
JPanel PanelNorth = new JPanel();
JPanel PanelCenter = new JPanel();
JPanel PanelSouth = new JPanel();

//声明按钮
JButton ButtonAddFile = new JButton(Add File);
JButton ButtonDeleteFile =新的JButton(删除文件);
JButton ButtonLock = new JButton(Lock);
JButton ButtonUnlock = new JButton(Unlock);

//声明FileChooser
JFileChooser chooser = new JFileChooser();
$ b $ public LockFrame()
{
//框架的标题
super(Lock and Zip);

//创建菜单栏
super.setJMenuBar(menuBar);

//创建菜单标签
menuBar.add(menu);

//创建菜单项
menu.add(MenuItemClose);

//添加北面板
PanelNorth.setBorder(BorderFactory.createEtchedBorder());

super.add(PanelNorth);

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

//将中心面板添加到框架
super.add(PanelCenter);

//添加滚动窗格
JScrollPane listScroller = new JScrollPane();
listScroller.setPreferredSize(new Dimension(400,360));

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

//添加南面板
PanelSouth.setBorder(BorderFactory.createEtchedBorder());

super.add(PanelCenter);

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

//动作监听器
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();
//将文件添加到您的中心面板


$ b $ if(Source == ButtonDeleteFile)
{
$ b $如果(Source == ButtonLock)
{

}

如果(Source == ButtonUnlock)
{

}

if(Source == ButtonPassword)
{





解决方案

您可能需要阅读如何使用用户列表了解更多详细信息,但基本概念相当简单。 / p>

创建您自己的 ListModel 。在这个例子中,我定制了我自己的,你可以简单地使用 DefaultListModel ,并添加你想要的对象。



创建你自己的 JList 并将你的模型应用到它上面,好吧,就是这个了...

  public class FileAdder {

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

$ b public FileAdder(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
尝试{
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); $ $ b $ frame.add(new FileAdderPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class FileAdderPane extends JPanel {
$ b $ private JList fileList;
私有JFileChooser选择器;

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&& file.length> 0){
MyFileListModel (FileFileListModel)fileList.getModel();
for(File file:files){
model.add(file);
}
}
break;
}
}
});

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


$ b $ public class MyFileListModel extends AbstractListModel {

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

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

$ b @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);
}
}
}


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?

I read that defaultListModel is the way to go but am unsure.

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. In this example, I customised my own, you could just as easily use a DefaultListModel, and add the objects you want to it.

Create you're self a JList and apply your model to it and, well, that's about it...

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);
        }
    }
}

这篇关于在模型中使用JList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆