关于如何调试 ComboBoxModel 的建议
在我的项目中,我有几个 JComboBox 及其自定义模型。基本上它们用于显示某些 JTables 列的值(因此我决定在相对的 扩展 AbstractTableModel 的类)。
public class MyTableModel1 extends AbstractTableModel{
protected class MyTableComboBoxModel1 extends AbstractListModel implements ComboBoxModel{
private Object selected;
@Override
public Object getElementAt(int index) {
return getValueAt(index, 1);
}
@Override
public int getSize() {
return getRowCount();
}
@Override
public Object getSelectedItem() {
return this.selected;
}
@Override
public void setSelectedItem(Object anItem) {
this.selected = anItem;
}
}
}
我有几个模型:MyTableModel2 和 MyTableComboBoxModel2。这些模型都做几乎相同的事情,除了一些与组合框和表本身都不相关的附加操作。
所有这些东西的目的应该是根据相对 JTable 发生的修改来更新 JComboBox 的显示值。
对于我实现的模型来说,除了一种情况之外,一切都工作正常,经过几个小时的调试我仍然无法解决它。有缺陷的模型的代码几乎与其他模型相同。可能这是我代码中其他地方的错误,但我不知道在哪里。
错误的情况具有以下行为:当我最初创建一个包含某些值的表时,即使在 JComboBox 中,这些值也能正确显示,但是当我添加新行时,显示的值全部变为空白(显示的空白菜单的大小是正确的) 。 我发现:
- jtable的新行添加正确。
- 单击 JComboBox 时会多次调用 getElementAt 方法并返回所有可用值(包括新值)。
- 当我单击 JComboBox 并返回正确的更新值时,将调用 getSize() 方法。
- 如果 JComboBox 具有焦点,我可以使用箭头来选择可用项目,并且所有项目都是可选的(甚至是添加的项目)。
- 如果我用鼠标在显示的空白菜单上随机单击一点,则可以选择所有最后可用的条目。
有人有什么想法吗?您能建议我在这种情况下如何调试吗?不幸的是我无法发布 SSCCE..我希望有人能提供帮助..
in my project I've several JComboBox with their custom models. Basically they are used to show values of some JTables column (therefore I decided to implement them on the relative
class extending AbstractTableModel).
public class MyTableModel1 extends AbstractTableModel{
protected class MyTableComboBoxModel1 extends AbstractListModel implements ComboBoxModel{
private Object selected;
@Override
public Object getElementAt(int index) {
return getValueAt(index, 1);
}
@Override
public int getSize() {
return getRowCount();
}
@Override
public Object getSelectedItem() {
return this.selected;
}
@Override
public void setSelectedItem(Object anItem) {
this.selected = anItem;
}
}
}
And I have several models : MyTableModel2 with MyTableComboBoxModel2. These models all do pretty the same thing except some additional operations not related neither with the combobox nor with the table itself.
The purpose of all this stuff should be to update JComboBox's displayed values accordingly to modifications occured to the relative JTable.
All works fine for models I've implemented except in one case , and after several hours of debug I still can't solve it. The code of the bugged model is almost identical to the others. Probably it's a bug somewhere else in my code, but I can't figure out where.
The wrong case has the following behavior: when I initially created a table with some values these are correctly displayed even in the JComboBox, but when I add a new row the displayed values become all blank (the size of the displayed blank menu is right).
I found out that:
- the new row of the jtable is added correctly.
- the getElementAt method is called several times when clicked on the JComboBox and return all the available values (including the new ones).
- the method getSize() is called when I click the JComboBox and return the right updated value
- If JComboBox has focus I can use arrows to select the available items and all items are selectable (even the added ones).
- If I use the mouse clicking one point at random on the displayed blank menu, all the last available entry could be chosen.
Has someone any idea? Could you suggest me how could I debug in such a situation? Unfortunately I can't post an SSCCE.. I hope someone could help anyway..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是为什么需要 SSCCE。
我不明白为什么您需要自定义模型。我猜你只需要使用 TabelModelListener 。
每当添加/删除值时,您都会更新组合框。
Which is why a SSCCE is required.
I don't understand why you need a custom model. I would guess you just need to use a TabelModelListener.
Whenever a value is added/removed you updated the combo box.