Log in
updated 11:29 AM UTC, May 4, 2016

How to read from a CSV file, Sort the contents and populate a JTable

  • Written by Madhu V Rao
  • Published in Core Java

Here is a small example to read a CSV file and sort based on a column and then populate the JTable (Swing component).

I feel the code is self explanatory , if not please let me know in the comments section will try to add the comments where required.

 

Main Program:

package test;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class CSVTable extends JFrame {

	private static final int NUMBER_OF_COLUMNS = 4;
	private static final String AEKOS_LINK = "Link";
	private static final String LONG = "Long";
	private static final String LAT = "Lat";
	private static final String CRS = "CRS";

	private Logger logger = Logger.getLogger(this.getName());
	private List<TableDataModel> tableList = new ArrayList<TableDataModel>();
	/**
	 * Default Serialization Version
	 */
	private static final long serialVersionUID = 1L;

	public CSVTable() {
		super();
		InputStream stream = this.getClass().getResourceAsStream("CSV_1.csv");
		readAndUpdateTableDataModel(stream);

		Object[] columnNames = new Object[NUMBER_OF_COLUMNS];
		columnNames[0] = AEKOS_LINK;
		columnNames[1] = LONG;
		columnNames[2] = LAT;
		columnNames[3] = CRS;

		DefaultTableModel model = new DefaultTableModel(columnNames,0);

		populateModel(model);

		JTable table = new JTable(model);
			
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(new Rectangle(new Dimension(400, 300)));
		
		this.getContentPane().add(scrollPane);

	}

	private void populateModel(DefaultTableModel model) {
		for (int i = 0; i < tableList.size(); i++) {
			model.addRow(this.tableList.get(i).getDataAsObjectList());
		}
	}

	public static void main(String args[]) {
		final CSVTable table = new CSVTable();
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				table.setTitle("CSV Table");
				table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				table.pack();
				table.setVisible(true);
			}
		});

	}

	public void readAndUpdateTableDataModel(InputStream in) {

		Scanner scan = new Scanner(in);

		while (scan.hasNext()) {
			String rowValue = scan.nextLine();
			seperateAndPopulate(tableList, rowValue);
		}
		Collections.sort(tableList);

	}

	private void seperateAndPopulate(List<TableDataModel> tableList,
			String rowValue) {
		String splitValue[] = rowValue.split(",");
		if (splitValue.length == NUMBER_OF_COLUMNS) {
			if (splitValue[0].equals(AEKOS_LINK)) {
				return;
			}
			TableDataModel model = TableDataModel.newInstance(splitValue[0],
					splitValue[1], splitValue[2], splitValue[3]);
			tableList.add(model);
		} else {
			logger.log(Level.SEVERE, "Issue with data");
		}
	}

}

 

TableDataModel:

package test;

public class TableDataModel implements Comparable<TableDataModel> {
	
	private String aekoLink;
	private String longData;
	private String latData;
	private String cRS;

	private TableDataModel() {
		//dont allow creation of Object without elements
	}
	
	public String getAekoLink() {
		return aekoLink;
	}

	public void setAekoLink(String aekoLink) {
		this.aekoLink = aekoLink;
	}

	public String getLongData() {
		return longData;
	}

	public void setLongData(String longData) {
		this.longData = longData;
	}

	public String getLatData() {
		return latData;
	}

	public void setLatData(String latData) {
		this.latData = latData;
	}

	public String getcRS() {
		return cRS;
	}

	public void setcRS(String cRS) {
		this.cRS = cRS;
	}

	public static TableDataModel newInstance(String aekoLink,String longData, String latData, String cRs ) {
		TableDataModel model = new TableDataModel();
		model.setAekoLink(aekoLink);
		model.setLongData(longData);
		model.setLatData(latData);
		model.setcRS(cRs);
		return model;
	}

	@Override
	public int compareTo(TableDataModel o) {
		return this.getAekoLink().compareTo(o.getAekoLink());
	}
	
	public Object[] getDataAsObjectList() {
		Object[] columnVals = new Object[4];
		columnVals[0] = this.getAekoLink();
		columnVals[1] = this.getLongData();
		columnVals[2] = this.getLatData();
		columnVals[3] = this.getcRS();
		return columnVals;
	}
	
}

 

CSV File:

LINK TO CSV File


Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest