开发者

JTable that can save to a file

开发者 https://www.devze.com 2022-12-12 23:07 出处:网络
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 locati

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.


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.


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!


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

JTable that can save to a file

JTable that can save to a file


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号