Results 1 to 11 of 11
  1. #1
    Desperado is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Adding \ removing items from Jlist

    Hello! Could anybody help me with a function of adding(populating) \ removing items from Jlist?
    I am selecting some files with Swing File chooser, and setting them to Jlist.

    Java Code:
    File[] sf;
    sf = chooser.getSelectedFiles();
    checkBoxList.setListData(sf);
    As known, setListData
    Constructs a read-only ListModel from an array of objects, and calls setModel with this model.
    But I need multiple adding of these files, and for now have no idea how to add more items to list.

    Also It would be good to know how the good method to remove items from there...

    Thanks for further help...

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Desperado is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    Use a ListModel
    Thanks for quick help, i will try to do that

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Desperado is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Seems that it is a problem with creating another list model, since I am using
    3rd party Jidesoft swing library for creating
    CheckBoxList
    http: // bit<dot>ly/ 53LuX6
    And I really don't know how define different list model there. However, it is possible to download source code of jidesoft swing (but how to compile it in jar later?)

    Here is the code of my app where I am working with the list( sorry, it is way too dirty)

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    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.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.Vector;
    
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JOptionPane;
    import javax.swing.AbstractListModel;
    import com.jidesoft.swing.CheckBoxList;
    
    
    public class ListBoxDemo2 extends JFrame implements Serializable {
        final JLabel statusbar = new JLabel(("Ready"), JLabel.CENTER);
        final Vector listContents = new Vector();
        final CheckBoxList checkBoxList = new CheckBoxList();
        // final ListModel listModel = new ListModel();
        File[] sf;
    
        FileOutputStream fOut = null;
        ObjectOutputStream oOut = null;
        FileInputStream fIn = null;
        ObjectInputStream oIn = null;
    
        public ListBoxDemo2() {
    	super("ListBoxDemo");
    	setSize(500, 300);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    	final Container c = getContentPane();
    	c.setLayout(new BorderLayout());
    	c.add(createPanel1(), BorderLayout.CENTER);
    	c.add(createPanel2(), BorderLayout.EAST);
    	c.add(createPanel3(), BorderLayout.SOUTH);
    	sf = readData();
        }
    
        private JPanel createPanel1() {
    	final JPanel panel1 = new JPanel(new BorderLayout());
    	final JScrollPane scroll = new JScrollPane(this.checkBoxList);
    	scroll.setPreferredSize(new Dimension(350, 300));
    	panel1.add(scroll);
    	return panel1;
    
        }
        
        private JPanel createPanel2() {
    	final JPanel panel2 = new JPanel(new GridLayout(3, 1));
    	final JButton addButton = new JButton("Add");
    	final JButton deleteButton = new JButton("Remove");
    	final JButton openButton = new JButton("Open");
    	panel2.add(addButton);
    	panel2.add(deleteButton);
    	panel2.add(openButton);
    	final JPanel p = new JPanel(new BorderLayout());
    	p.add(panel2, BorderLayout.NORTH);
    
    	addButton.addActionListener(new ActionListener() {
    	    public void actionPerformed(final ActionEvent ae) {
    		
    		final JFileChooser chooser = new JFileChooser();
    		chooser.setMultiSelectionEnabled(true);
    
    		chooser.addChoosableFileFilter(new SimpleFileFilter(".txt"));
    		chooser.setDialogTitle("Please select files to add");
    		chooser.setAcceptAllFileFilterUsed(false);
    		final File f = new File("Test1");
    		//if (f.exists())
    			//{JOptionPane.showMessageDialog(null, "File does not exist", "Error", JOptionPane.ERROR_MESSAGE);}
    		chooser.setSelectedFile(f);
    		
    		
    
    		final int option = chooser.showOpenDialog(ListBoxDemo2.this);
    		if (option == JFileChooser.APPROVE_OPTION) {
    		    sf = chooser.getSelectedFiles();
    		    checkBoxList.setListData(sf);
    		    
    		    statusbar.setText("Added " + sf.length + " items");
    		} else {
    		    statusbar.setText("You canceled.");
    		}
    		writeData(sf);
    
    	    }
    	});
    	deleteButton.addActionListener(new ActionListener() {
    	    public void actionPerformed(final ActionEvent ae) {
    		// get the indices of selected items
    		final int select[] = checkBoxList.getCheckBoxListSelectedIndices();
    		
    		int size = checkBoxList.getModel().getSize();
    		// copy these into a temporary vector
    		final Vector v = new Vector();
    		for (int i = 0; i < size; i++) {
    		    v.add((checkBoxList.getModel().getElementAt(i)));
    		}
    		// remove from original vector
    		int count = 0;
    		for (int i = select.length-1; i >= 0; i--)
    		    {v.remove(select[i]);
    		    checkBoxList.removeCheckBoxListSelectedIndex(select[i]);
    		    count = count + 1;
    		    }
    		
    		checkBoxList.setListData(v);
    		statusbar.setText("Removed "+count+" items");
    		//sf = null;
    		//sf[2].get;
    		//String fpath = sf[1].getAbsolutePath();
    		//writeData(sf);
    		
    		deleteFile();
    		
    	    }
    	});
    
    	checkBoxList.addMouseListener(new MouseAdapter()
    	{
    	    public void mouseClicked(MouseEvent event)
    	    {
    	    System.out.println("Mouse event, clickCount = " +
    	    event.getClickCount());
    	    if (event.getClickCount() == 2) {
    		if (!checkBoxList.getCheckBoxListSelectionModel().isSelectedIndex(checkBoxList.getSelectedIndex())) {
    		    
    		checkBoxList.addCheckBoxListSelectedIndex(checkBoxList.getSelectedIndex());
    		}
    		else 
    			checkBoxList.removeCheckBoxListSelectedIndex(checkBoxList.getSelectedIndex());
    		}
    	    }
    	});
    
    	openButton.addActionListener(new ActionListener() {
    	    public void actionPerformed(final ActionEvent ae) {
    		final int select[] = checkBoxList.getCheckBoxListSelectedIndices();
    		if (select.length == 0) {
    		    JOptionPane.showMessageDialog(null, "No items selected!", "Error", JOptionPane.ERROR_MESSAGE);
    		}
    		
    		String program = "C:\\WINDOWS\\NOTEPAD.exe";
    		
    		for (int i = 0; i < select.length; i++) {
    		    Runtime load = Runtime.getRuntime();
    		    try {
    			load.exec(program + " " + sf[select[i]]);
    		    } catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		    }
    		    statusbar.setText("Using notepad.exe to open files");
    		}
    	    }
    	});
    	return p;
        }
        
        
        private JPanel createPanel3() {
    	final JPanel panel3 = new JPanel();
    	panel3.setLayout(new BorderLayout());
    	panel3.setPreferredSize(new Dimension(400, 30));
    	panel3.setMinimumSize(new Dimension(100, 30));
    
    	panel3.add(statusbar, BorderLayout.WEST);
    	return panel3;
        }
        private void writeData(File[] sf){
        try {
    	    fOut = new FileOutputStream("c:\\NewFileList.ser");
    	    oOut = new ObjectOutputStream(fOut);
    	    oOut.writeObject(sf); // serializing selected files
    	    System.out.println("The file list is serialized into c:\\NewFileList.ser");
    	} catch (IOException e) {
    	    e.printStackTrace();
    	} finally {
    	    try {
    		oOut.flush();
    		oOut.close();
    		fOut.close();
    	    } catch (IOException e1) {
    		e1.printStackTrace();
    	    }
    	}}
        private File[] readData(){
    	boolean exists = (new File("c:\\NewFileList.ser")).exists();
    	    if (exists) {
    		try {
    		    fIn = new FileInputStream("c:\\NewFileList.ser");
    		    oIn = new ObjectInputStream(fIn);
    
    		    sf = (File[]) oIn.readObject();
    		    
    		    this.checkBoxList.setListData(sf);
    
    		} catch (IOException e) {
    		    e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    		    e.printStackTrace();
    		} finally {
    		    try {
    			oIn.close();
    			fIn.close();
    		    } catch (IOException e1) {
    			e1.printStackTrace();
    		    }
    		}
    	    } 
    	    else sf = null;
    
    	return sf;
        } 
        private void deleteFile(){
    	String fileName = "c:\\NewFileList.ser";
    	    // A File object to represent the filename
    	    File f = new File(fileName);
    
    	    /
    	    // If it is a directory, make sure it is empty
    	    if (f.isDirectory()) {
    	      String[] files = f.list();
    	      if (files.length > 0)
    	        throw new IllegalArgumentException(
    	            "Delete: directory not empty: " + fileName);
    	    }
    
    	    // Attempt to delete it
    	    boolean success = f.delete();
    
    	  }
        
        public static void main(final String args[]) {
    	final ListBoxDemo2 sfc = new ListBoxDemo2();
    	sfc.setVisible(true);
    	
        }
    
    
    }
    Last edited by Desperado; 12-16-2009 at 03:45 PM.

  6. #6
    Desperado is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Can anybody actually give a clue about that?
    I would appreciate it very much!

  7. #7
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    From the "Members" tab of page CheckBoxList we can see that CheckBoxList has a constructor that takes a ListModel as an argument. Your application can create the CheckBoxList with a DefaultListModel as its model. and then modify the model via addElement and removeElement. (The elements are the String file names.)

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Seems that it is a problem with creating another list model, since I am using 3rd party Jidesoft swing library for creating CheckBoxList
    Yes, that would be the problem. You should be using a JTable, not a JList to show checkboxes. A JList was not designed to be editable.

  9. #9
    Desperado is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by zweibieren View Post
    Your application can create the CheckBoxList with a DefaultListModel as its model. and then modify the model via addElement and removeElement. (The elements are the String file names.)
    Java Code:
     final CheckBoxList checkBoxList = new CheckBoxList(new DefaultListModel() );
    You mean like this? If so, I could not access to addElement or removeElement there...
    What am I doing wrong?

  10. #10
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    To give you a chance to work it out for yourself, I've hidden the answer below.
    To see it, type control-A to select all the text.

    Java Code:
    [COLOR="White"]DefaultListModel cbmodel = new DefaultListModel();
    final CheckBoxList checkBoxList = new CheckBoxList(cbmodel);
    cbmodel.addElement(...);
    [/COLOR]

  11. #11
    Desperado is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by zweibieren View Post
    To give you a chance to work it out for yourself, I've hidden the answer below.
    To see it, type control-A to select all the text.

    Java Code:
    [COLOR="White"]DefaultListModel cbmodel = new DefaultListModel();
    final CheckBoxList checkBoxList = new CheckBoxList(cbmodel);
    cbmodel.addElement(...);
    [/COLOR]
    Thank you, I guess this works for me at last.:)

Similar Threads

  1. Clicking Items inside of a JList
    By hunterbdb in forum AWT / Swing
    Replies: 9
    Last Post: 10-21-2009, 06:50 PM
  2. How can I save items in my JList?
    By yoodidoo in forum New To Java
    Replies: 2
    Last Post: 08-02-2009, 06:36 AM
  3. jlist items
    By bullstreetboy in forum New To Java
    Replies: 6
    Last Post: 04-12-2009, 05:30 PM
  4. CheckBox Jlist items
    By dropez in forum AWT / Swing
    Replies: 1
    Last Post: 03-26-2009, 07:39 PM
  5. Adding and removing panels dynamically
    By kbyrne in forum AWT / Swing
    Replies: 1
    Last Post: 04-12-2008, 08:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •