1

As i am new to java swing, i am finding a little difficulty in integrating the JFileChooser with JList. My goal is to select a file from the dialog-box(JFileChooser) and click 'add' so that it gets added to the JList automatically and the same mechanism with 'remove'. I tried going through a few tutorials and a few hints but it dint work. It would be really great if any of you could help me with this step. Thanks in advance..!!

    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);     
            }
        }  
dmurali
  • 211
  • 1
  • 6
  • 14

2 Answers2

2
  1. Read the JFileChooser tutorial
  2. If the above step is not sufficient, take a look at the class javadoc of JFileChooser and note the APPROVE_OPTION and the getSelectedFile method. This should allow you to obtain the file
  3. Read the JList tutorial
  4. If the above step is not sufficient, take a look at the available API of JList and ListModel, and more in particular the default implementation DefaultListModel which contains add and remove methods
Robin
  • 36,233
  • 5
  • 47
  • 99
  • Thanks for your quick response! I have added my code to my initial query. I request you to kindly have a look at it and let me know how do i go about with it. thanks in advance! – dmurali Mar 04 '12 at 18:02
2

When you get a new file name in your chooser's action listener, shown here, add it to (or remove it from) the list's models, as shown in this example.

Addendum: To display the file's content in the JList, you'll need to create a suitable renderer using one of the text components.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for your reply..! May be i was not clear enough in my query, i actually want the file itself to be copied in the JList so that it would be easy for adding more and removing the added ones. For example, when i choose a file named random.txt, i want this file copied in the JList. Do you think, this is possible? If yes, can you send me a link which has some example? – dmurali Mar 04 '12 at 23:19
  • Yes, I've linked to the relevant tutorials above. You can update your question's [sscce](http://sscce.org/) as it evolves. – trashgod Mar 04 '12 at 23:46
  • Thank you :) It worked!! I am able to now add the files to JList. – dmurali Mar 05 '12 at 18:53
  • But I have another doubt. I am unable remove a particular file which is already added to the JList. Can you tell me how to remove one particular file? My code removes all files and not just selected files. for (File file : fileChooser.getSelectedFiles()) { field.setText(file.getAbsolutePath()); vector.remove(file); } – dmurali Mar 06 '12 at 09:35
  • The critical thing is to `remove()` the file(s) from the list's _model_. `DefaultListModel` notifies the list when changes occur. – trashgod Mar 06 '12 at 15:19
  • DefaultListModel dint work in my case. Dint get any error msg but it just dint work. But i tried an other way and yes, it works now.! Anyways thanks a lot for your hint! – dmurali Mar 07 '12 at 11:10