13 Replies - 7693 Views - Last Post: 01 April 2012 - 05:44 AM Rate Topic: -----

#1 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

arraylist to jtable

Posted 31 March 2012 - 08:24 AM

i have a csv file which i need to read it and put it in a jTable. so its like from "csv-->arrayList--->Jtable" i manage to store the "file.csv" in to the arrayList. so basical my arrayList is populated by studentDetails which is separated by comma, my question is how do you put the arrayList of studentDetails in a jTable?.. my arraList is some what like this..
name,familyName,Id,Addres,phoneNum
te,quiero,1234,hanamatzu,123456
tan,simpson,3456,junjuri,6666234 .. i just wanna know how to store it in a jTable . tnx in advance

Is This A Good Question/Topic? 0
  • +

Replies To: arraylist to jtable

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2689
  • View blog
  • Posts: 11,351
  • Joined: 20-September 08

Re: arraylist to jtable

Posted 31 March 2012 - 09:27 AM

See http://technojeeves....v-to-tablemodel
Was This Post Helpful? 0
  • +
  • -

#3 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: arraylist to jtable

Posted 31 March 2012 - 09:56 AM

thanks for the quick reply. but is there any chance that i can do that with the arrayList?.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: arraylist to jtable

Posted 31 March 2012 - 06:03 PM

If you write your own TableModel it will be a piece of cake :)

http://www.dreaminco...h-a-tablemodel/
Was This Post Helpful? 0
  • +
  • -

#5 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: arraylist to jtable

Posted 31 March 2012 - 07:55 PM

hi pbl thank you for the reply. i have a question how do i convernt my arrayList to Object[][] so that i can put it in a jTable?.. thank you
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: arraylist to jtable

Posted 31 March 2012 - 08:05 PM

If you store you ArrayList in a TableModel then the getValueAt(int row, int col) will be easy:

public Objet getValueAt(int row, int col) {
    // extract the String at row: row
    String line = al.get(row);
    // split by column based on a "," as delimiter
    String[] token = line.split(",");
    // return the String at col
    return token[col];
}



Happy coding
Was This Post Helpful? 1
  • +
  • -

#7 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: arraylist to jtable

Posted 31 March 2012 - 08:47 PM

View Postpbl, on 31 March 2012 - 08:05 PM, said:

If you store you ArrayList in a TableModel then the getValueAt(int row, int col) will be easy:

public Objet getValueAt(int row, int col) {
    // extract the String at row: row
    String line = al.get(row);
    // split by column based on a "," as delimiter
    String[] token = line.split(",");
    // return the String at col
    return token[col];
}



Happy coding


thank you pbl.. i just need to fiddle around more to this so that i can understand clearly thanks
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: arraylist to jtable

Posted 31 March 2012 - 08:52 PM

My pleasure :)
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: arraylist to jtable

Posted 31 March 2012 - 09:37 PM

Just a quick one to give you a hint :)

import javax.swing.*;
import java.util.*;
import javax.swing.table.*;

public class AlToTable extends JFrame {
	
	private final static String[] header = {"Name", "age", "Sex"};
	
	AlToTable(ArrayList<String> al) {
		MyModel mm = new MyModel(al, header);
		JTable table = new JTable(mm);
		add(new JScrollPane(table));
		setSize(200, 200);
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	class MyModel extends AbstractTableModel {
		
		private ArrayList<String> al;
		private String[] header;
		
		MyModel(ArrayList<String> al, String[] header) {
			this.al = al;
			this.header = header;
		}

		public int getColumnCount() {
			return header.length;
		}

		public int getRowCount() {
			return al.size();
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			String[] token = al.get(rowIndex).split(",");
			return token[columnIndex];
		}

		public String getColumnName(int col) {
			return header[col];
		}
		
	}
	
	public static void main(String[] args) {
		ArrayList<String> al = new ArrayList<String>();
		al.add("PBL,59,M");
		al.add("Madona,20,F");
		al.add("teQuiero,???,M");
		new AlToTable(al);
	}
}


Was This Post Helpful? 1
  • +
  • -

#10 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: arraylist to jtable

Posted 01 April 2012 - 04:05 AM

View Postpbl, on 31 March 2012 - 09:37 PM, said:

Just a quick one to give you a hint :)

import javax.swing.*;
import java.util.*;
import javax.swing.table.*;

public class AlToTable extends JFrame {
	
	private final static String[] header = {"Name", "age", "Sex"};
	
	AlToTable(ArrayList<String> al) {
		MyModel mm = new MyModel(al, header);
		JTable table = new JTable(mm);
		add(new JScrollPane(table));
		setSize(200, 200);
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	class MyModel extends AbstractTableModel {
		
		private ArrayList<String> al;
		private String[] header;
		
		MyModel(ArrayList<String> al, String[] header) {
			this.al = al;
			this.header = header;
		}

		public int getColumnCount() {
			return header.length;
		}

		public int getRowCount() {
			return al.size();
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			String[] token = al.get(rowIndex).split(",");
			return token[columnIndex];
		}

		public String getColumnName(int col) {
			return header[col];
		}
		
	}
	
	public static void main(String[] args) {
		ArrayList<String> al = new ArrayList<String>();
		al.add("PBL,59,M");
		al.add("Madona,20,F");
		al.add("teQuiero,???,M");
		new AlToTable(al);
	}
}



OMG! i just did it!. i did pass the arrayList from other class then try to put it in the model and it works awsome!.. thank you so much pbl. im a happy vegemite :)
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2689
  • View blog
  • Posts: 11,351
  • Joined: 20-September 08

Re: arraylist to jtable

Posted 01 April 2012 - 04:57 AM

Quote

but is there any chance that i can do that with the arrayList?.


You don't need to. That method takes you straight from a Reader on the csv file to a TableModel
Was This Post Helpful? 0
  • +
  • -

#12 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: arraylist to jtable

Posted 01 April 2012 - 05:31 AM

i am having trouble with my code here is the class
import javax.swing.*;
import java.util.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.*;
import java.util.ArrayList;


public class AlToTable extends JFrame {
	
	private final static String[] header = {"a","b","c","d","e","f","g"};


	AlToTable(ArrayList<String> fileData ) {
		MyModel mm = new MyModel(fileData , header);
		JTable table = new JTable(mm);
		add(new JScrollPane(table));
		setSize(400, 400);
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	class MyModel extends AbstractTableModel {
		
		private ArrayList<String> fileData ;
		private String[] header;
		
		MyModel(ArrayList<String> fileData , String[] header) {
			this.fileData  = fileData ;
			this.header = header;
		}

		public int getColumnCount() {
			return header.length;
		}

		public int getRowCount() {
			return fileData .size();
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			String[] token = fileData .get(rowIndex).split(",");
			return token[columnIndex];
		}

		public String getColumnName(int col) {
			return header[col];
		}
		
	}
	public static void main(String[] args) {
		ArrayList<String> fileData = new ArrayList<String>();

			fileData.add("22.5,S3,,SJE,,B,SW");
			fileData.add("26,S2,,SJE,,G,FR");
			fileData.add("189.99,CM,,SJE,,G,");
			fileData.add("193.22,C2,DDH001E,RPB,H,,");

		new AlToTable(fileData);
	}
}




maybe you will ask me why is the 3rd and 4th data that i add is like that. well for some reason i am reading it from a csv file so i just get a bit of the datas that i am reading for the example. my problem is how can i read the 3rd and 4th data.. it dosnt give me an error but when i try tu run it it gives me a funny look in the GUI.. thank you in advance
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: arraylist to jtable

Posted 01 April 2012 - 05:37 AM

I'm glad you want me (OK, maybe not), but this really is just a continuation of your previous topic, so I'm going to merge the two together.

Please don't open multiple topics on the same subject.
Was This Post Helpful? 0
  • +
  • -

#14 teQuiero  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 31-March 12

Re: arraylist to jtable

Posted 01 April 2012 - 05:44 AM

View PostJackOfAllTrades, on 01 April 2012 - 05:37 AM, said:

I'm glad you want me (OK, maybe not), but this really is just a continuation of your previous topic, so I'm going to merge the two together.

Please don't open multiple topics on the same subject.


oh! sorry about that i am just new to the group and i didnt read the rules. thanks by the way ..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1