JTable:如何添加数据而不显示?

发布于 2024-09-26 10:13:13 字数 125 浏览 3 评论 0原文

我有一个图像专栏。我想附加一些只能阅读但不能显示的附加文本信息。

我怎样才能做到这一点?我正在寻找一些“幽灵”专栏。

寻找某种方法来存储临时存储在客户端java应用程序上的持久数据,然后将其上传到服务器。

I have a column for images. I would like to attach some additional text information that will only be read but not for display.

How can I achieve this ? I am looking for some "ghost" column.

Looking for some way to store persistent data that stores temporarily on client java application, and later uploads it to the server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

残月升风 2024-10-03 10:13:13
  1. 通常使用两列数据创建 TableModel
  2. 使用 TableModel 创建 JTable
  3. 从 JTable 中获取 TableColumnModel
  4. 删除您不希望在表中显示的 TableColumn(从 TableColumnModel 中)。这不会删除模型中的数据,只是不会在表视图中绘制列。

现在,当您想要引用隐藏列中的数据时,您需要从 TableModel 而不是 JTable 获取数据:

Object cellData = table.getModel().getValueAt(...);
  1. Create the TableModel normally with two columns of data
  2. Create the JTable using the TableModel
  3. Get the TableColumnModel from the JTable
  4. Remove the TableColumn (from the TableColumnModel) that you don't want displayed in the table. This will NOT delete the data in the model, it just won't paint the column in the view of the table.

Now when you want to reference the data in the hidden column you need to get the data from the TableModel, not the JTable:

Object cellData = table.getModel().getValueAt(...);
冷弦 2024-10-03 10:13:13

了解您的 TableModel 的外观会很有帮助,因为我们可以为您提供对当前设计进行最小更改的想法。但是,一种解决方案是设计一个自定义数据对象来表示表中的一行,并让您的 TableModel 使用它为每列提供正确的数据,包括您当前显示的图像。

编辑:

基本上我有一个单列表,并且 DefaultTableModel 设置有 2 列。我只想显示模型的第一列。

我建议您通过扩展 AbstractTableModel 创建自己的 TableModel。为此,您只需要实现三个方法:

public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);

然后您可以提供一个像 List 这样的后备集合来保存行数据。您的 getRowCount() 可以返回列表的大小,您的 getColumnCount() 可以为图片列返回 1。然后,getValueAt() 将从上面提到的自定义数据对象中返回图片。

It would be helpful to see what your TableModel looks like, as we could give you ideas that would offer minimal changes to your current design. However, one solution would be to design a custom data object that would represent a row in your table, and have your TableModel use it to supply the correct data for each column, including the image that you currently display.

edit:

basically I have a single column table and DefaultTableModel set up with 2 columns. I would like only the first column of the Model to display.

I would suggest that you create your own TableModel by extending AbstractTableModel. For that you just need to implement three methods:

public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);

You then could provide a backing collection like a List to hold your row data. Your getRowCount() could return the size of the list, your getColumnCount() could return 1 for your picture column. getValueAt() would then return the picture from the custom data object that I mentioned above.

佞臣 2024-10-03 10:13:13

您需要扩展 AbstractTableModel 或 DefaultTableModel 类并重写 getValueAt(row,column) 和 setValueAt 方法。

You need to extend AbstractTableModel or DefaultTableModel class and override getValueAt(row,column) and setValueAt methods.

苍景流年 2024-10-03 10:13:13

您使用的是自定义 TableModel 吗?您可以像这样覆盖 getColumnCount()

@Override
public int getColumnCount() {
    return super.getColumnCount() - 1;
}

这样您的模型将隐藏最后一列,但您仍然可以使用 getValueAt() 读取它

Are you using a custom TableModel? You can override getColumnCount() like this:

@Override
public int getColumnCount() {
    return super.getColumnCount() - 1;
}

This way your model will have the last column hidden, but you'll still be able to read it with getValueAt()

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