Write to a JTable

Community help forum for Data Pipeline Toolkit

Write to a JTable

Postby jeffg » Fri Jun 04, 2010 11:26 am

Swing's JTable uses a format like this...
final String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };

final Object[][] data = {
{ "Mary", "Campione", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
{ "Kathy", "Walrath", "Knitting", new Integer(2),
new Boolean(false) },
{ "Sharon", "Zakhour", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Philip", "Milne", "Pool", new Integer(10),
new Boolean(false) } };

final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

Is there an easy way to output DataPipeline into a column name array (String or Object) and a data multiarray for easy input into a JTable? Thanks
jeffg
 
Posts: 3
Joined: Fri Jun 04, 2010 3:54 am

Re: Write to a JTable

Postby dele.taylor » Fri Jun 04, 2010 2:57 pm

jeffg,

The easiest thing would be to adapt the records to a Swing TableModel.

Let's say you have the following CSV file:

Code: Select all
"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"
"Mary", "Campione", "Snowboarding", 5,false
"Alison", "Huml", "Rowing", 3, true
"Kathy", "Walrath", "Knitting", 2,  false
"Sharon", "Zakhour", "Speed reading", 20,true
"Philip", "Milne", "Pool", 10,false


You could use the following code to display it in a JTable:

Code: Select all
import java.awt.Dimension;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.RecordList;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.job.JobTemplate;
import com.northconcepts.datapipeline.memory.MemoryWriter;

public class DisplayRecordsInAJTable {

   public static void main(String[] args) {

      // -------------------------------------------------------------------
      // Read file "jtable-input.csv" into memory as a RecordList
      // -------------------------------------------------------------------
      DataReader reader = new CSVReader(new File("scratch/data/input/jeffg/jtable-input.csv")).setFieldNamesInFirstRow(true);
      MemoryWriter writer = new MemoryWriter();
      JobTemplate.DEFAULT.transfer(reader, writer);
      RecordList records = writer.getRecordList();

      // -------------------------------------------------------------------
      // Create a JTable by adapting RecordList to the TableModel interface
      // -------------------------------------------------------------------
      final JTable table = new JTable(new DataPipelineTableModelAdapter(records));
      table.setPreferredScrollableViewportSize(new Dimension(500, 70));
      table.setFillsViewportHeight(true);

      // -------------------------------------------------------------------
      // Show JTable in a frame
      // -------------------------------------------------------------------
      JFrame frame = new JFrame("Data Pipeline - JTable Bridge");
      frame.setSize(640, 480);
      frame.setVisible(true);
      frame.getContentPane().add(new JScrollPane(table));

   }

   public static class DataPipelineTableModelAdapter extends AbstractTableModel {

      private final RecordList records;

      public DataPipelineTableModelAdapter(RecordList records) {
         this.records = records;
      }

      public int getColumnCount() {
         if (records.getRecordCount() == 0) {
            return 0;
         }
         return records.get(0).getFieldCount();
      }

      public String getColumnName(int columnIndex) {
         return records.get(0).getField(columnIndex).getName();
      }

      public int getRowCount() {
         return records.getRecordCount();
      }

      public Object getValueAt(int rowIndex, int columnIndex) {
         return records.get(rowIndex).getField(columnIndex).getValue();
      }

      public boolean isCellEditable(int rowIndex, int columnIndex) {
         return false;
      }

      public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
         records.get(rowIndex).getField(columnIndex).setValue(aValue);
      }

   }

}


Hope this helps.

D
dele.taylor
Site Admin
 
Posts: 10
Joined: Mon Mar 31, 2008 10:03 pm

Re: Write to a JTable

Postby jeffg » Thu Jun 10, 2010 2:33 pm

Excellent! Thank you :-)
I've been playing with it and it works great. At one point early in my data process, I'm stepping through importing an unknown text format. When making a selection like, changing the delineation to a semi-colon or setting the first row as a header, I have to reread the entire file from disk with the appropriate reader options. Is there any way to read only the first 100 lines (if the file is that long) or somehow placing part of the file into memory before running the reader? Just trying to think how I could do this without rereading the entire file from disk several times as options are selected. Not a big deal if it's a small file, but might be if it's a large one.
jeffg
 
Posts: 3
Joined: Fri Jun 04, 2010 3:54 am

Re: Write to a JTable

Postby dele.taylor » Sat Jun 12, 2010 8:48 pm

Jeffg, you've got at least two options here.

1. If your DataReader extends AbstractReader you can change the declaration and set the last row:

Code: Select all
      AbstractReader reader = new CSVReader(new File("scratch/data/input/jeffg/jtable-input.csv")).setFieldNamesInFirstRow(true);
      reader.setLastRow(100);


2. A more general approach would be to decorate the original reader:

Code: Select all
      DataReader reader = new CSVReader(new File("scratch/data/input/jeffg/jtable-input.csv")).setFieldNamesInFirstRow(true);
      reader = new ProxyReader(reader) {
         @Override
         protected Record readImpl() throws Throwable {
            if (getRecordCount() == 100) {
               return null;
            }
            return super.readImpl();
         }
      };


Cheers,
Dele
dele.taylor
Site Admin
 
Posts: 10
Joined: Mon Mar 31, 2008 10:03 pm


Return to Data Pipeline Toolkit - Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron