可以保存到文件的JTable
有谁知道基于 JTable 的 Swing 组件或可以保存到文件的代码示例? ie:提供一个菜单项或按钮,单击该菜单项或按钮时会提示用户输入文件位置并将表的内容保存到文件(CSV、XLS、TXT 或其他文件)。
最简单的部分是循环遍历行并保存到文件中。但表本身还需要有一个 UI 组件,允许用户启动保存。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
写你自己的。您要做的就是使用 table.getModel().getValueAt(...) 方法并循环遍历行和列,然后将数据写入文件。
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.
我使用以下方法实现了这一点:
Action
实现:DelimitedExportAction
。我通常将此操作添加到JToolBar
、JMenuBar
或JPopupMenu
中。void registerTable(JTable tbl)
。在内部,此方法将FocusListener
添加到JTable
。当给定的JTable
获得焦点时,设置tableToExport
实例变量并启用该操作。actionPerformed(ActionEvent)
方法时,如果tableToExport != null
,则调用导出逻辑。TableModel
,而是迭代JTable
,并为每一行调用底层getValueAt(int, int)
>TableModel,记住在视图和模型行/列索引之间进行转换。这对于最终用户来说更加直观,因为这意味着应用于JTable
的任何排序和过滤都会在导出过程中保留。 (在我的实现中,我实际上为用户提供了导出所有数据或可见数据的选择。)DelimitedExportFormatter
,其作用是转换getValueAt(int, int)
返回的每个对象> 到字符串
。与向JTable
提供渲染器类似,此类应该允许您为给定的Class
或特定列提供格式,其中特定于列的格式优先。应用于所有其他值的默认格式应为:value == null ? “”:value.toString()
。恐怕我无法提供代码,因为它是专有的,但希望这能让您走上正轨。祝你好运!
I have implemented this using the following approach:
Action
implementation:DelimitedExportAction
. I typically add this action to aJToolBar
,JMenuBar
orJPopupMenu
.void registerTable(JTable tbl)
. Internally this method adds aFocusListener
to theJTable
. When a givenJTable
gains focus set thetableToExport
instance variable and enable the action.actionPerformed(ActionEvent)
method is called, invoke your export logic iftableToExport != null
.TableModel
I recommend iterating over theJTable
, and for each row callinggetValueAt(int, int)
on the underlyingTableModel
, 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 theJTable
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 bygetValueAt(int, int)
to aString
. Similar to when providing renderers to aJTable
this class should permit you to provide a Format for a givenClass
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!
我最近创建了一个非常简单的教程,使用制表符分隔值(TSV)格式将数据从 JTable 导出到 Excel 文件中。该应用程序提供了一个“导出”按钮,然后该按钮会触发一个对话框(JFileChooser)以帮助用户指定文件位置/目标。我希望这会有所帮助。
https://sites.google。 com/site/teachmemrxymon/java/export-records-from-jtable-to-ms-excel
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 的 Swing 组件可以满足这种确切的需求。
但是,您希望按钮放置在桌子上的什么位置?在我看来,最好将 JTable 添加到 JScrollPane 并将“保存”按钮放在 JScrollPane 上,或者将 JScrollPane 添加到 JPanel 并将“保存”按钮放在 JPanel 上。我没有看到 JTable 本身上有一个按钮背后的逻辑。
如果您想要一个菜单项,您可能需要创建菜单栏并将 JTable 添加到保存菜单栏的任何容器中。请注意,表格本身仍然没有添加按钮,但视觉上是一样的。
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.