在 Java Swing 中填充空表

发布于 2024-08-19 02:06:38 字数 99 浏览 4 评论 0 原文

我想要一个 JFrame 窗口,其中包含一个初始空表,例如 10 列。然后,由鼠标单击生成的操作事件应使用 10 个或更少项目的列表填充表,并将表中已使用的行保留为空。这应该怎么做呢?

I would like to have a JFrame window with an initial empty table of say, 10 columns. An action event generated by a mouse click should then populate the table with a list of 10 or less items, the leaving the used rows of the table empty. How should this be done?

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

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

发布评论

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

评论(3

金橙橙 2024-08-26 02:06:38

我建议通过子类化 AbstractTableModel。这样您就可以使用任何合适的集合(例如java.util.List)“支持”您的模型。当 ActionEvent 被触发时,您通常会修改底层集合,然后触发 TableModelEvent 以导致 JTable 被重新绘制。

I'd recommend defining your own TableModel implementation by subclassing AbstractTableModel. That way you can "back" your model with any suitable collection (e.g. java.util.List). When an ActionEvent is fired you would typically amend your underlying collection and then fire a TableModelEvent to cause the JTable to be repainted.

只有一腔孤勇 2024-08-26 02:06:38

为此,您应该使用所需的数据创建一个 DefaultTableModel,并且对于空行,您可以使用 null 值填充对象表。

使用一些代码就更简单了:

因为我不知道你的数据来自哪里,我假设它来自一个少于 10 行的矩阵:

String data[][] = {{"a","b"}, {"c","d"}};

你必须使用以前的数据和创建一个新矩阵>null 单元格用于完成表格。最后你会得到这样的东西。

Object data2[][] = {{"a","b"}, 
{"c","d"}, 
{null,null}, 
{null,null}, 
{null,null}, 
{null,null}, 
{null,null}, 
{null,null}, 
{null,null}, 
{null,null}};

这样你就会有一个 10x2 的矩阵来填充你的表格。现在您可以更新您的 DefaultTableModel

yourTable.setModel(
        new DefaultTableModel(data2, new String [] {"Column1Title", "Cloumn2Title"}) {
        Class[] types = new Class[] {String.class,String.class}; 
        boolean[] canEdit = new boolean[] {true, true};
        @Override
        public Class getColumnClass(int columnIndex){ return types [columnIndex];}
        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex){ return canEdit [columnIndex];}
});

就是这样。我认为您创建对象矩阵没有问题。

For this, you should create a DefaultTableModel with the data you want, and for the blank lines, you fill the object table with null values.

It´s simpler with some code:

As I don't know where you data come from, I'll presume it come from a matrix with less than 10 rows:

String data[][] = {{"a","b"}, {"c","d"}};

you have to create a new matrix with your previous data and the null cells for completion of the table. In the end You'll have something like this.

Object data2[][] = {{"a","b"}, 
{"c","d"}, 
{null,null}, 
{null,null}, 
{null,null}, 
{null,null}, 
{null,null}, 
{null,null}, 
{null,null}, 
{null,null}};

This way you'll have a 10x2 matrix that will fill your table. Now you can update your DefaultTableModel

yourTable.setModel(
        new DefaultTableModel(data2, new String [] {"Column1Title", "Cloumn2Title"}) {
        Class[] types = new Class[] {String.class,String.class}; 
        boolean[] canEdit = new boolean[] {true, true};
        @Override
        public Class getColumnClass(int columnIndex){ return types [columnIndex];}
        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex){ return canEdit [columnIndex];}
});

And that's it. I presume you don't have problems to create your Object matrix.

戈亓 2024-08-26 02:06:38

除了按照 Adamski 的说明创建自己的 TableModel 之外,您还可以使用 javax.swing.table.DefaultTableModel
它有一个构造函数,该构造函数将列数和行数作为参数和管理数据的方法(addRowinsertRowsetDataAt、.. .)。

我更喜欢创建一个自己的 TableModel,除非它用于非常简单的程序/功能。

Besides creating your own TableModel as explained by Adamski, you can use the javax.swing.table.DefaultTableModel directly.
It has a constructor which takes the number of columns and rows as argument and methods to manage the data (addRow, insertRow, setDataAt, ...).

I would prefer creating an own TableModel, unless it's for a very simple program/functionality.

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