Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By pbrockway2

Thread: Can't add vector to JList

  1. #1
    rabbitfarmer is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Unhappy Can't add vector to JList

    I have a JList set up, and I'm trying to make the Jlist change when a different selection is made on another list. I'm completely fine except that when i try to use JList.setListData(), eclipse keeps telling me to enter a Vector<String>[]. when i do this i just get errors. I've tried passing Vector<String> and even Object[] but eclips won't allow them. any ideas?

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

  3. #3
    rabbitfarmer is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Can't add vector to JList

    Here is the initialization

    Java Code:
    Handler handler;
    	JButton add,add2, add3,remove,remove2,remove3, defaults, done;
    	JLabel parent, child;
    	JList<Vector<String>> categories, exts, chars;
    	JTextField catin, exin, charin;
    	JScrollPane ta1p, ta2p, ta3p;
    	Vector<String> catArray, charArray;
    	Vector<Vector<String>> catcat;
    	Scanner in;
    	GridBagConstraints ta2;
    	
    	
    	
    	//Constructor
    	public Preferences() {
    		super("Preferences");
    		final Container container = getContentPane();
    		container.setLayout(new GridBagLayout());
    		container.setBackground(Color.black);
    		container.setForeground(Color.cyan);
    		
    		
    		handler = new Handler();
    		catArray = new Vector<String>();
    		catcat = new Vector<Vector<String>>();
    		charArray = new Vector<String>();
    here is my actual problem

    Java Code:
    categories.addListSelectionListener(new ListSelectionListener() {
    
    			@Override
    			public void valueChanged(ListSelectionEvent e) {
    				// TODO Auto-generated method stub
    				
    				try
    				{
    					
    					exts.setListData(catcat.get(categories.getSelectedIndex()));
    					exts.updateUI();
    				}catch(Exception e4){};
    				
    			}
    			
    			
    		});
    as you can tell, I'm trying to get one list to display differently based on another list's choices

    exts.setListData(catcat.get(categories.getSelected Index())); is my problem. Eclipse has it red underlined and says "The method setListData(Vector<String>[]) in the type JList<Vector<String>> is not applicable for the arguments (Vector<String>)"
    Last edited by rabbitfarmer; 06-11-2012 at 03:26 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Can't add vector to JList

    You declare exts to be JList<Vector<String>> ie a list each of whose entries will be a vector of strings.

    So when you call setListData() you must pass a vector of these items - ie a vector of vectors of strings. AFAIK you can't construct a Vector<String>[] as Eclipse suggests: with generics you pass a Vector<Vector<String>>. And what you are actually passing is an instance of Vector<String>, hence the error.

    I'm trying to get one list to display differently based on another list's choices
    Possibly you made a wrong turn when you declared exts the way you did. Do you mean it to be a list whose entries are strings? In that case declare it as JList<String> and setListData() will be good as you have it.
    rabbitfarmer likes this.

  5. #5
    rabbitfarmer is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: Can't add vector to JList

    oh wow i completely missed that. Yes i changed it like you said and it worked perfectly, thanks a ton

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Can't add vector to JList

    You're welcome.

    You're always learning stuff here. I had missed the memo about JList becoming generified... Oracle have still left it with the prehistoric Vector vestige (I guess there's some reason for that).

Similar Threads

  1. Replies: 4
    Last Post: 01-10-2012, 02:05 PM
  2. JList autocomplete with Vector
    By nik_meback in forum AWT / Swing
    Replies: 0
    Last Post: 01-01-2011, 01:31 PM
  3. Link one JList to another JList
    By mib1bee in forum New To Java
    Replies: 1
    Last Post: 12-30-2010, 07:24 PM
  4. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 03:17 PM
  5. [SOLVED] JList( Vector ) Display problem
    By logicbug in forum AWT / Swing
    Replies: 4
    Last Post: 03-21-2009, 03:28 PM

Tags for this Thread

Posting Permissions

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