通过jFileChoser将文件添加到jList

时间:2012-03-04 16:51:17

标签: java swing jlist jfilechooser

由于我是java swing的新手,我发现将JFileChooser与JList集成有点困难。我的目标是从对话框中选择一个文件(JFileChooser)并单击“添加”,以便它自动添加到JList,并使用“删除”相同的机制。我尝试了一些教程和一些提示,但它有用。如果你们中的任何人能够帮助我完成这一步,那真的很棒。 在此先感谢.. !!

    package examples;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JSplitPane;
//import javax.swing.SwingConstants;
class SplitPane extends JFrame 
{     

    private static final long serialVersionUID = 1L;     
    private JSplitPane splitPaneV;     
    private JSplitPane splitPaneH;      
    private JLayeredPane panel1;      
    private JPanel panel2;      
    private JPanel panel3;      
    private JButton add;
    private JButton remove;
    private JScrollBar scrollBar;
    private JList list;


    public SplitPane() 
    {
        setTitle("AdditionalLoaderInformation");         
        setBackground(Color.blue);            
        JPanel topPanel = new JPanel();           
        topPanel.setLayout(new BorderLayout());         
        topPanel.setPreferredSize(new Dimension(700, 500));         
        getContentPane().add(topPanel); 

        // Create the panels        
        createPanel1();          
        createPanel2();         
        createPanel3();      

        // Create a splitter pane         
        splitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT);         
        topPanel.add(splitPaneV, BorderLayout.CENTER);           
        splitPaneH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);          
        splitPaneH.setLeftComponent(panel1);         
        splitPaneH.setRightComponent(panel2);           
        splitPaneV.setLeftComponent(splitPaneH);         
        splitPaneV.setRightComponent(panel3);

        scrollBar = new JScrollBar();
        scrollBar.setOrientation(JScrollBar.HORIZONTAL);
        panel3.add(scrollBar, BorderLayout.SOUTH);


        list = new JList();
        panel3.add(list, BorderLayout.CENTER);
        }       
    public void createPanel1() 
    {         
        panel1 = new JLayeredPane();         
        panel1.setLayout(new BorderLayout()); 

    }


    public void createPanel2() 
    {          
        panel2 = new JPanel();

        add = new JButton("ADD");
        final JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

        add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                   ************************************

                  }
              });   


        panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        panel2.add(add);

        remove = new JButton("REMOVE");
        remove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            removeActionPerformed(e);   
            }

            private void removeActionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });
        panel2.add(remove);        



        }            

           public void createPanel3()
           {      
               panel3 = new JPanel();       
               panel3.setLayout(new BorderLayout());       
               panel3.setPreferredSize(new Dimension(400, 100));      
               panel3.setMinimumSize(new Dimension(100, 50));        
               final JFileChooser fileChooser = new JFileChooser();   
               fileChooser.setMultiSelectionEnabled(true);
               //fileChooser.showOpenDialog(fileChooser);
               fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
               fileChooser .setDialogTitle("OPEN"); 
               panel3.add(fileChooser, BorderLayout.NORTH);  

               //fileChooser.addActionListener(new ActionListener()
              // {          
                  // public void actionPerformed(ActionEvent e)
                   //{             




                     // }       
                   //});    
               }  

        public static void main(String args[]) {        
            // Create an instance of the test application         
            SplitPane mainFrame = new SplitPane();        
            mainFrame.pack();         
            mainFrame.setVisible(true);     
            }
        }  

2 个答案:

答案 0 :(得分:2)

  1. 阅读the JFileChooser tutorial
  2. 如果上述步骤不充分,请查看JFileChooser的类javadoc,并注意APPROVE_OPTIONgetSelectedFile方法。这应该允许您获取文件
  3. 阅读the JList tutorial
  4. 如果上述步骤不充分,请查看JListListModel的可用API,尤其是包含DefaultListModel的默认实施addremove方法

答案 1 :(得分:2)

当你在选择器的动作监听器中显示一个新的文件名,显示为here时,将其添加到列表的模型中(或从中移除),如example所示。

附录:要在JList中显示文件的内容,您需要使用renderer之一创建合适的text components