Results 1 to 11 of 11
- 12-15-2009, 04:15 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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.
As known, setListDataJava Code:File[] sf; sf = chooser.getSelectedFiles(); checkBoxList.setListData(sf);
But I need multiple adding of these files, and for now have no idea how to add more items to list.Constructs a read-only ListModel from an array of objects, and calls setModel with this model.
Also It would be good to know how the good method to remove items from there...
Thanks for further help...
- 12-15-2009, 04:19 PM #2
Use a ListModel, you can change the contents as you like:
How to Use Lists (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 12-15-2009, 04:23 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
-
More specifically, I would use a DefaultListModel object, since it is already wired to do what you want.
- 12-16-2009, 12:23 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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.
- 12-16-2009, 04:09 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
Can anybody actually give a clue about that?
I would appreciate it very much!
- 12-16-2009, 05:40 PM #7
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
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.)
- 12-16-2009, 05:48 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.Seems that it is a problem with creating another list model, since I am using 3rd party Jidesoft swing library for creating CheckBoxList
- 12-17-2009, 10:58 AM #9
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
- 12-17-2009, 12:30 PM #10
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
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]
- 12-17-2009, 12:48 PM #11
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Clicking Items inside of a JList
By hunterbdb in forum AWT / SwingReplies: 9Last Post: 10-21-2009, 06:50 PM -
How can I save items in my JList?
By yoodidoo in forum New To JavaReplies: 2Last Post: 08-02-2009, 06:36 AM -
jlist items
By bullstreetboy in forum New To JavaReplies: 6Last Post: 04-12-2009, 05:30 PM -
CheckBox Jlist items
By dropez in forum AWT / SwingReplies: 1Last Post: 03-26-2009, 07:39 PM -
Adding and removing panels dynamically
By kbyrne in forum AWT / SwingReplies: 1Last Post: 04-12-2008, 08:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks