可以保存到文件的JTable

发布于 2024-08-11 23:42:43 字数 167 浏览 3 评论 0原文

有谁知道基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若沐 2024-08-18 23:42:43

写你自己的。您要做的就是使用 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.

維他命╮ 2024-08-18 23:42:43

我使用以下方法实现了这一点:

  • 创建一个 Action 实现:DelimitedExportAction。我通常将此操作添加到 JToolBarJMenuBarJPopupMenu 中。
  • 公开方法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()
  • 我允许在不同模式下配置操作(这也控制操作的图标):
    • 导出到文件:启动文件选择器对话框,允许用户指定保存目的地。
    • 导出到 Excel:将导出的数据保存为临时文件并在 Excel 中打开。
    • 可配置导出:启动一个对话框,用户可以在其中指定:行分隔符、字段分隔符、要导出的列、要导出的行(全部、可见、选定)、保存目标(Excel/文件)。

恐怕我无法提供代码,因为它是专有的,但希望这能让您走上正轨。祝你好运!

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!

落叶缤纷 2024-08-18 23:42:43

我最近创建了一个非常简单的教程,使用制表符分隔值(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

enter image description here

enter image description here

傻比既视感 2024-08-18 23:42:43

我不知道有任何类似 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文