Ok so I think the title is all I've got to ask here.
好的,我认为标题就是我在这里要问的全部内容。
My AbstractTableModel
works fine, when I want to add an empty row, I use the following code to do so:
我的AbstractTableModel工作正常,当我想添加一个空行时,我使用以下代码来执行此操作:
public void addRow() {
Object[][] oldData = data;
data = new Object[oldData.length + 1][3];
// Copy old data to new data
for (int x = 0; x < oldData.length; x++) {
data[x] = oldData[x];
}
// Append new row
data[oldData.length] = new Object[] {"", "", false};
fireTableRowsInserted(data.length - 2, data.length - 1);
}
Now, since I'm showing an empty row, I want the user to edit it, and I assume that the user will. Now, how do I make sure that the data is saved in my array as the user makes changes? Or, if that's not possible, what better alternative is there?
现在,因为我显示一个空行,我希望用户编辑它,我假设用户会。现在,当用户进行更改时,如何确保数据保存在我的阵列中?或者,如果那是不可能的,有什么更好的选择呢?
Ok so I should probably explain what I want to do:
好的,我应该解释一下我想做什么:
I'm loading contents from a file, and displaying as a table. Now, the user might add new rows to the table on clicking the Add Row
button. This will add 1 empty row per click. (In this image above is an instance when the button is pressed twice.
我正在从文件加载内容,并显示为表格。现在,用户可以在单击“添加行”按钮时向表中添加新行。这将为每次点击添加1个空行。 (在上图中是按下按钮两次的实例。
Now, I want that the user can edit the cells, and then maybe delete some rows (maybe) but on clicking the Save Database
button, the updated data in the table is stores.
现在,我希望用户可以编辑单元格,然后可能删除一些行(可能)但是在单击“保存数据库”按钮时,表中的更新数据是存储。
1 个解决方案
#1
2
You may get a better understanding by comparing several approaches:
通过比较几种方法,您可以更好地理解:
-
In this example, either a background thread or a button can add a new row to a table. The background thread sequences instances of
Runnable
on the EDT viainvokeLater()
. It is conceptually easier to understand, but it is also easier to get wrong.在此示例中,后台线程或按钮可以向表中添加新行。后台线程通过invokeLater()在EDT上对Runnable的实例进行排序。它在概念上更容易理解,但也更容易出错。
-
SwingWorker
encapsulates sequencing access to shared data. The API and tutorial show the basic approach of updating a component's model usingpublish()
/process()
. This preferred mechanism is more robust and scalable.SwingWorker封装了对共享数据的排序访问。 API和教程展示了使用publish()/ process()更新组件模型的基本方法。这种优选机制更加健壮和可扩展。
-
In this more advanced example, a Swing
Timer
paces anExecutor
that controls a series ofSwingWorker
instances.在这个更高级的示例中,Swing Timer控制一个控制一系列SwingWorker实例的Executor。
In all cases, the tables remain functional. You might use any as a basis for an sscce.
在所有情况下,表格仍然有效。您可以使用any作为sscce的基础。
Addendum: Could you explain a little more about the strategies you suggested?
附录:你能解释一下你建议的策略吗?
I've updated the list of examples and suggested some things to look for in context.
我已经更新了示例列表,并在上下文中提出了一些要查找的内容。
#1
2
You may get a better understanding by comparing several approaches:
通过比较几种方法,您可以更好地理解:
-
In this example, either a background thread or a button can add a new row to a table. The background thread sequences instances of
Runnable
on the EDT viainvokeLater()
. It is conceptually easier to understand, but it is also easier to get wrong.在此示例中,后台线程或按钮可以向表中添加新行。后台线程通过invokeLater()在EDT上对Runnable的实例进行排序。它在概念上更容易理解,但也更容易出错。
-
SwingWorker
encapsulates sequencing access to shared data. The API and tutorial show the basic approach of updating a component's model usingpublish()
/process()
. This preferred mechanism is more robust and scalable.SwingWorker封装了对共享数据的排序访问。 API和教程展示了使用publish()/ process()更新组件模型的基本方法。这种优选机制更加健壮和可扩展。
-
In this more advanced example, a Swing
Timer
paces anExecutor
that controls a series ofSwingWorker
instances.在这个更高级的示例中,Swing Timer控制一个控制一系列SwingWorker实例的Executor。
In all cases, the tables remain functional. You might use any as a basis for an sscce.
在所有情况下,表格仍然有效。您可以使用any作为sscce的基础。
Addendum: Could you explain a little more about the strategies you suggested?
附录:你能解释一下你建议的策略吗?
I've updated the list of examples and suggested some things to look for in context.
我已经更新了示例列表,并在上下文中提出了一些要查找的内容。