当我添加行时,从 AbtractTableModel 扩展的 JTable 不会更新 GUI
这是我的新 MyJtable
public void addWidget(Book w) {
datalist.add(w);
fireTableRowsInserted(datalist.size()-1, datalist.size()-1);
}
调用类
MyJtable tv = new MyJtable(a);
table = new JTable(tv);
//tv.addWidget(b3);
JScrollPane pane2 = new JScrollPane(table);
按钮 CLick 函数,
public void actionPerformed(ActionEvent e)
{
MyJtable tv1 = new MyJtable();
Book b3 = new Book ("Java nutshell-299", "Ajfdfdfdingya2") ;
if("Add".equals(e.getActionCommand()))
{
JOptionPane.showMessageDialog(null,"Add button is clicked");
tv1.addWidget(b3);
}
当我单击按钮时,我看不到任何 GUI chnage,但如果
tv1.addWidget(b3);
}
在之前调用,我的意思是在加载时我可以看到新书,但在按钮单击时看不到
This my new MyJtable
public void addWidget(Book w) {
datalist.add(w);
fireTableRowsInserted(datalist.size()-1, datalist.size()-1);
}
calling class
MyJtable tv = new MyJtable(a);
table = new JTable(tv);
//tv.addWidget(b3);
JScrollPane pane2 = new JScrollPane(table);
button CLick function
public void actionPerformed(ActionEvent e)
{
MyJtable tv1 = new MyJtable();
Book b3 = new Book ("Java nutshell-299", "Ajfdfdfdingya2") ;
if("Add".equals(e.getActionCommand()))
{
JOptionPane.showMessageDialog(null,"Add button is clicked");
tv1.addWidget(b3);
}
when i click button then i don't see any GUI chnage but if call
tv1.addWidget(b3);
}
before , i mean on load then i can see the new book but not on button click
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我看到您将该行添加到刚刚在
actionPerformed
方法内创建的新表中。通常,我们使用操作来更改/改变现有的 GUI 组件。这可能是您在 GUI 上看不到任何变化的原因。我猜想,滚动窗格中显示的表格是通过
尝试将行添加到
table
(通过tv
必须首先将其设为实例变量)来创建的,而不是新创建的表。I see that you add the row to a new table that you've just created inside the
actionPerformed
method. Usually, we use actions to change/alter already existing GUI components. This might be a reason why you don't see any change on the GUI.I guess, the table that is displayed in the scroll pane is created with
Try adding the row to
table
(viatv
which has to be made an instance variable first) instead of the newly created table.检查您是否覆盖
setValueAt
在AbstractTableModel
。默认实现是空的。Check that you override
setValueAt
in your concrete implementation ofAbstractTableModel
. The default implementation is empty.我认为 Andreas_D 已经确定了一个核心问题。您还可能会遇到问题,因为您正在重写
AbstractTableModel
而不是DefaultTableModel
。DefaultTableModel
有许多已经完成的事件触发方法。I think Andreas_D has identified a core issue. You might also have problems because you are overriding
AbstractTableModel
instead ofDefaultTableModel
. TheDefaultTableModel
has many of the event firing methods already completed.那是错误的。该表不应有 addWidget() 方法。
所有数据更新都应直接通过 TableModel 完成。然后TableModel会在数据发生变化时通知表重新绘制自身。
您永远不应该在 TableModel 本身之外访问 TableModel 使用的数据存储。当你昨天问另一个问题时,我给了你一个完整的解决方案。
That is wrong. The table should NOT have an addWidget() method.
All updates to the data should be done directly through the TableModel. Then the TableModel will notify the table to repaint itself when the data changes.
You should never access the data storage used by the TableModel outside of the TableModel itself. I gave you a complete solution when you asked your other question yesterday.