Results 1 to 3 of 3
  1. #1
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Default [SOLVED] Multiple selection on JTable & store datas into an array

    Hello friends!

    Can anybody show me any method for selecting multiple values (rows) from a JTable?

    I mean, i have a JTable; the user selects 2 or more rows; how can i pass these values into an array, for example?
    I know that getSelectedRows returns the index of the selected rows, and that's fine, but i need to store the datas which are in the rows into an array.

    Thanks so much!
    Last edited by hendrix79; 01-30-2009 at 05:11 PM. Reason: Solved, thanks alot.

  2. #2
    hendrix79 is offline Member
    Join Date
    Dec 2008
    Posts
    28
    Rep Power
    0

    Lightbulb

    Hopefully i solved it.

    This is how:

    Java Code:
    private void tabellaProgrammiMouseClicked(MouseEvent evt) {
    		r = tabellaProgrammi.getSelectedRow();
    		if (r==-1) {
    		} else 
    		{
    			java.util.List<String> elenco = new ArrayList<String>();
    			ind = tabellaProgrammi.getSelectedRows();
    			int z =ind.length;
    			String[] listaId = new String[z+1];
    			
    			for (int i=0; i<z; i++) {
    				elenco.add((String) tabellaProgrammi.getValueAt(ind[i], 0));
    			}
    			listaId = (String[])elenco.toArray(new String[elenco.size()]);
    				
    			
    		}
    	}
    I hope this could help someone with my same problem.
    Thanks :)

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    My only comment would be:
    Java Code:
    if ([B][COLOR="Blue"]!(r==-1)[/COLOR][/B]) 
    		{
    			java.util.List<String> elenco = new ArrayList<String>();
    			ind = tabellaProgrammi.getSelectedRows();
    			int z =ind.length;
    			String[] listaId = new String[z+1];
    			
    			for (int i=0; i<z; i++) {
    				elenco.add((String) tabellaProgrammi.getValueAt(ind[i], 0));
    			}
    			listaId = (String[])elenco.toArray(new String[elenco.size()]);
    				
    			
    		}
    It's not a good programming practice to leave empty "then" statements.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. How to sort a JTable for multiple columns?
    By BLR in forum Advanced Java
    Replies: 2
    Last Post: 03-16-2009, 10:41 AM
  2. JTable Combo Box Row Selection Problem
    By hemanthjava in forum AWT / Swing
    Replies: 2
    Last Post: 12-18-2008, 07:31 AM
  3. List Selection
    By Sarinam in forum AWT / Swing
    Replies: 2
    Last Post: 07-14-2008, 06:16 AM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. Code for selection
    By kneekow in forum Eclipse
    Replies: 0
    Last Post: 02-01-2008, 03:10 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
  •