Results 1 to 4 of 4

Thread: JList problems

  1. #1
    grimx is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default JList problems

    When i run the following code;
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    import java.io.*;
    
    public class TestJList extends JFrame {
    	private JList jList;
    	private String[] files;
    	private File file;
    	private JTextArea textArea;
    	
    	public TestJList(String fname) {
    		setSize(650, 500);
    		setTitle("TestJList Demo");
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		
    		
    		file = new File(fname);
    		files = file.list();
    		jList = new JList(files);
    		jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		jList.addListSelectionListener(new ListSelectionListener() {
    
    			@Override
    			public void valueChanged(ListSelectionEvent e) {
    				String line = (String)jList.getSelectedValue();
    				textArea.append(new File(line).getPath());
    				
    			}
    			
    		});
    		JScrollPane JList_sp = new JScrollPane(jList);
    		add(JList_sp, BorderLayout.WEST);
    		
    		textArea = new JTextArea(10, 80);
    		
    		JScrollPane textArea_sp = new JScrollPane(textArea);
    		add(textArea_sp, BorderLayout.CENTER);
    		
    		setVisible(true);
    	}
    	
    	public static void main(String[] args) {
    		TestJList frame = new TestJList()); //input some directory
    	}
    
    }
    When i select a file/directory from the list it is printed twice in the JTextArea.

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

    Default Re: JList problems

    Only respond if the selected value isn't adjusting. For example:

    Java Code:
          jList.addListSelectionListener(new ListSelectionListener() {
    
             @Override
             public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                   // String line = files[jList.getSelectedIndex()] + "\n";
                   String line = (String) jList.getSelectedValue();
                   // textArea.append(new File(line).getPath());
                   textArea.append("i\n");
                }
             }
          });

  3. #3
    grimx is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default Re: JList problems

    thanx.

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

Similar Threads

  1. Replies: 4
    Last Post: 01-10-2012, 02:05 PM
  2. performance problems with JList/default list model
    By prescott in forum AWT / Swing
    Replies: 3
    Last Post: 11-16-2011, 05:06 AM
  3. Link one JList to another JList
    By mib1bee in forum Advanced Java
    Replies: 1
    Last Post: 12-30-2010, 07:35 PM
  4. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 07:24 PM
  5. JList displaying problems
    By D.Calladine in forum New To Java
    Replies: 0
    Last Post: 12-03-2010, 12:27 AM

Posting Permissions

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