tags:

views:

268

answers:

3

Does anyone know of a JTable based Swing component OR code example that can save to a file? i.e: provides a menu item or button that when clicked on prompts the user for a file location and saves the table's contents to a file (CSV, XLS, TXT, or whatever).

The easy part is looping through the rows and saving to a file. But there also needs to be a UI component ON the table itself that allows the user to initiate the save.

+4  A: 

Write your own. All you do is use the table.getModel().getValueAt(...) method and loop through the rows and columns and then write the data to a file.

camickr
Just updated the question to mention that there also needs to be a UI component on the table itself that allows the user to initiate the save.
tukushan
But that's just a button, labelled 'Save' and an action behind that calls your save method.
Andreas_D
A: 

I don't know of any JTable-like Swing component that fulfills this exact need.

However, where would you expect the button to be placed on the table? In my opinion you would be better served by either adding the JTable to a JScrollPane and putting your "save" button on the JScrollPane or adding the JScrollPane to a JPanel and putting the "save" button on the JPanel. I don't see the logic behind having a button on the JTable itself.

If you want a menu item, you'd probably want to create the menubar and add the JTable to whatever container is holding the menubar. There's still no adding of a button to the table itself, mind you, but it would be the same thing visually.

spork
+1  A: 

I have implemented this using the following approach:

  • Create an Action implementation: DelimitedExportAction. I typically add this action to a JToolBar, JMenuBar or JPopupMenu.
  • Expose a method void registerTable(JTable tbl). Internally this method adds a FocusListener to the JTable. When a given JTable gains focus set the tableToExport instance variable and enable the action.
  • When the actionPerformed(ActionEvent) method is called, invoke your export logic if tableToExport != null.
  • Rather than iterate over the TableModel I recommend iterating over the JTable, and for each row calling getValueAt(int, int) on the underlying TableModel, remembering to convert between view and model row / column indices. This is more intuitive to the end user as it means any sorting and filtering applied to the JTable is preserved during the export. (In my implementation I actually offer users the choice of exporting all data or visible data.)
  • Define a DelimitedExportFormatter whose role is to convert each object returned by getValueAt(int, int) to a String. Similar to when providing renderers to a JTable this class should permit you to provide a Format for a given Class or specific column, whereby a column specific format takes precedence. The default format applied to all other values should be: value == null ? "" : value.toString().
  • I allow the action to be configured in different modes (which also governs the action's icon):
    • Export to file: Launches a file chooser dialog allowing user to specify save destination.
    • Export to Excel: Saves the exported data as a temporary file and opens it within Excel.
    • Configurable export: Launches a dialog whereby the user can specify: row delimiter, field delimiter, columns to export, rows to export (all, visible, selected), save destination (Excel / File).

I'm afraid I can't provide the code as it is proprietary but hopefully this will put you on the right track. Good luck!

Adamski