Reputation: 8807
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.
Upvotes: 1
Views: 5307
Reputation: 1053
I recently created a very simple tutorial that exports data from JTable into excel file, using Tab-Separated Values(TSV) format. The application provides an Export button, which then triggers a dialog box (JFileChooser) to assist the user in specifying the file location/destination. I hope this helps somehow.
https://sites.google.com/site/teachmemrxymon/java/export-records-from-jtable-to-ms-excel
Upvotes: 2
Reputation: 54697
I have implemented this using the following approach:
Action
implementation: DelimitedExportAction
. I typically add this action to a JToolBar
, JMenuBar
or JPopupMenu
.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.actionPerformed(ActionEvent)
method is called, invoke your export logic if tableToExport != null
.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.)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'm afraid I can't provide the code as it is proprietary but hopefully this will put you on the right track. Good luck!
Upvotes: 2
Reputation: 1265
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.
Upvotes: 1
Reputation: 324088
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.
Upvotes: 6