tags:

views:

36

answers:

1

Hi all,

I am just having problems starting a bit of code to extract values from JTable so that I can eventually say them as a CSV file for viewing on Excel. Currently I have a JTable created using the following code:

package com.alpha;

import javax.swing.*;
import java.awt.*;

public class JTableComponent{
  public static void main(String[] args) 
{
    new JTableComponent();
  }

  public JTableComponent(){
    JFrame frame = new JFrame("Whiteboard Test");
    JPanel panel = new JPanel();
    String data[][] = {{"Company A","1000","1"},{"Company B","2000","2"},
     {"Company C","3000","3"},{"Company D","4000","4"}};

      String col[] = {"Company Name","Shares","Price"};
    JTable table = new JTable(data,col);
    panel.add(table,BorderLayout.CENTER);

    frame.add(panel);
    frame.setSize(300,200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
} 

I have started a new class that will be called whenever the "Export to CSV" button is pressed. I will implement the button listeners etc at a later stage, right now I would like a few pointers on how to create the for look that will go through the columns and rows looking for the values contained in them. Just a note, the JTable will be scalable, the current JTable is just for test purposes. I know there are APIs available such as the Apache one however I would prefer not to use them.

package com.alpha;

public class Exporter extends JTableComponent
{

 public changeToCSV(){

 }


 public changeToCSV()
 {
   for(int j = 0; j < table.getColumnCount(); j++) {
 }
}

I am having trouble deciding what the constructor should expect to receive. Many thanks for your help in advance!

A: 

The Exporter class method definitions are invalid. You need to include return types in order for this code to compile (I presume this is what you mean when you mention about what the constructor should return).

For a simple implementation you could consider returning a String[][] which you could then translate into csv data in a doubly nested loop. I have implemented something in the past where I had a structure similar to:

ExportAction
 |
 |--> ExportDestination
 |--> ExportFormatter

The ExportAction is an implementation of javax.swing.Action and is associated with the "Export to csv" button. It references two classes:

  • ExportDestination: The logical destination for the csv data. This interface offers a method createWriter() that is called by the action to create a "sink" which to squirt the csv data down. A concrete implementation of ExportDestination is FileDestination, but I also implemented others such as ClipboardDestination.
  • ExportFormatter: Responsible for translating the table rows into csv data. I chose to do this one row at a time: The ExportAction would iterate over each row to export calling exportFormatter.writeTableRow and passing in the writer created from the export destination. This gave me the flexibility to decide which rows to export (e.g. those currently visible / currently selected / all rows).

So in answer to your original question I didn't return anything from my various export methods. Instead, when the action was invoked I would prompt the user to select parameters (e.g. file name) which I would then set before invoking my export routine.

Hope that helps.

Adamski
Thanks, I understand I need to return a type, I only pasted my Exporter class so people had an idea of what I wanted to achieve. As I said, I'd really just like some advice / pointers on how to continue with the For loop and what the Exporter class would contain (as a String would not be correct).
Joey jie