Java 为什么我的ArrayList添加方法冲突,如何编辑我的自定义添加方法
我已经找到了为什么我的arraylist总是返回0的大小,但我无法解决这个问题。我有一个带有“addHuman(Human h)方法的自定义模型,该方法旨在添加到数组中。唯一的问题是它没有。现在,如果我使用常规方法,比如model.add(index,object o)它实际上会起作用并增加我的arraylist的大小,但不会显示在我的jtable上。我的问题是,如何使我的自定义addHuman方法起作用?非常感谢任何帮助 下面是使用该方法的主要类。当我单击addIndividual按钮时,它应该将human添加到我的HumanListModel中:Java 为什么我的ArrayList添加方法冲突,如何编辑我的自定义添加方法,java,Java,我已经找到了为什么我的arraylist总是返回0的大小,但我无法解决这个问题。我有一个带有“addHuman(Human h)方法的自定义模型,该方法旨在添加到数组中。唯一的问题是它没有。现在,如果我使用常规方法,比如model.add(index,object o)它实际上会起作用并增加我的arraylist的大小,但不会显示在我的jtable上。我的问题是,如何使我的自定义addHuman方法起作用?非常感谢任何帮助 下面是使用该方法的主要类。当我单击addIndividual按钮时,它应
addIndividual.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Human temp;
try {
temp = new Human();
modelx.addHuman(indexPoint, temp);
///the addHuman method does display on jtable but doesn't increment arraylist, meaning that the size is always 0 which creates many problems/////
//modelx.add(indexPoint, temp); does indeed increment the arraysize but then it doesn't display the values on the jtable////
indexPoint++;
System.out.println(modelx.size());
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
newbiex.revalidate(); ////is the jtable////
}
});
public class HumanListModel extends DefaultListModel implements TableModel
{
private ArrayList<Human> data;
public HumanListModel()
{
super();
data = new ArrayList<Human>();
}
public void addHuman(int k, Human h)
{
data.add(k, h);
fireIntervalAdded(this, data.size(), data.size());
}
public Human getHuman(int o)
{
return data.get(o);
}
public void removeHuman(Human h)
{
data.remove(h);
}
public int getColumnCount()
{
// the number of columns you want to display
return 1;
}
public int getRowCount()
{
return data.size();
}
public Object getValueAt(int row, int col)
{
return (row < data.size()) ? data.get(row) : null;
}
public String getColumnName(int col)
{
return "Human";
}
public Class getColumnClass(int col)
{
return Human.class;
}
public void addTableModelListener(TableModelListener arg0) {
}
@Override
public boolean isCellEditable(int arg0, int arg1) {
return false;
}
public void removeTableModelListener(TableModelListener arg0) {
}
public void setValueAt(Object arg0, int arg1, int arg2) {
}
}
以下是我的自定义HumanListModel:
addIndividual.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Human temp;
try {
temp = new Human();
modelx.addHuman(indexPoint, temp);
///the addHuman method does display on jtable but doesn't increment arraylist, meaning that the size is always 0 which creates many problems/////
//modelx.add(indexPoint, temp); does indeed increment the arraysize but then it doesn't display the values on the jtable////
indexPoint++;
System.out.println(modelx.size());
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
newbiex.revalidate(); ////is the jtable////
}
});
public class HumanListModel extends DefaultListModel implements TableModel
{
private ArrayList<Human> data;
public HumanListModel()
{
super();
data = new ArrayList<Human>();
}
public void addHuman(int k, Human h)
{
data.add(k, h);
fireIntervalAdded(this, data.size(), data.size());
}
public Human getHuman(int o)
{
return data.get(o);
}
public void removeHuman(Human h)
{
data.remove(h);
}
public int getColumnCount()
{
// the number of columns you want to display
return 1;
}
public int getRowCount()
{
return data.size();
}
public Object getValueAt(int row, int col)
{
return (row < data.size()) ? data.get(row) : null;
}
public String getColumnName(int col)
{
return "Human";
}
public Class getColumnClass(int col)
{
return Human.class;
}
public void addTableModelListener(TableModelListener arg0) {
}
@Override
public boolean isCellEditable(int arg0, int arg1) {
return false;
}
public void removeTableModelListener(TableModelListener arg0) {
}
public void setValueAt(Object arg0, int arg1, int arg2) {
}
}
公共类HumanListModel扩展DefaultListModel实现TableModel
{
私有数组列表数据;
公共HumanListModel()
{
超级();
数据=新的ArrayList();
}
公共无效添加人(整数k,人h)
{
数据。添加(k,h);
fireIntervalAdded(this,data.size(),data.size());
}
公共人员获取人员(INTO)
{
返回数据get(o);
}
公共无效删除人(人h)
{
数据删除(h);
}
public int getColumnCount()
{
//要显示的列数
返回1;
}
public int getRowCount()
{
返回data.size();
}
公共对象getValueAt(整数行,整数列)
{
return(row
当您更改基础数据时,必须触发模型更改
public void addHuman(Human h)
{
data.add(h);
fireIntervalAdded(this, data.size(), data.size());
}
每当您更改基础数据时,需要调用具有类似事件的类似方法来告诉列表它必须更新屏幕图像
例如,removeHuman()
将需要一个类似的调用。有关执行该调用的方法,请参阅上的javadoc。(在本例中,fireIntervalRemoved()
事件将需要包含已删除行的索引。)
您还需要一个返回该行数据元素的getElementAt()
方法。在您的情况下,在该行返回Human
,但它需要一个toString()
方法。或者,您可以从Human
中格式化字符串
然后把它还给我
注意-这个答案的前一个版本是基于我的困惑,认为这是一个TableModel而不是ListModel。它已被修复。我在哪里添加它?我如何使我的addHuman方法工作?:(以及addTableModelListener()和removeTableModelListener())不应该用空方法覆盖我还不确定在这些方法中放什么,我还没有做到:/n在父类中使用与侦听器相关的方法的版本。你可以把你的方法去掉。嘿,Lee,我添加了那行,但是它告诉我创建变量模型,但是我的Humanlistmodel是在我的主类中创建的。Whaat变量我真的放在那里了吗???