Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA摆动JTable。更改JTable中的数据并调用表模型中的自定义方法_Java_Swing_Jtable_Listener - Fatal编程技术网

JAVA摆动JTable。更改JTable中的数据并调用表模型中的自定义方法

JAVA摆动JTable。更改JTable中的数据并调用表模型中的自定义方法,java,swing,jtable,listener,Java,Swing,Jtable,Listener,我已经学会了如何在帧中显示JTable,但我不知道如何实际更改数据。我已经阅读了大量关于这个主题的教程,但似乎没有任何东西是合适的。你能回答一些关于下面代码的问题吗 1) 在actionListener中,我调用tab.getModel().getValueAt(1,1)和tab.getValueAt(1,1)。我得到相同的数据,“Petty.”如果它提供相同的数据,“getModel()”是否必要 我认为“getModel()”允许我访问在CustomTable.java类中编写的任何自定义

我已经学会了如何在帧中显示JTable,但我不知道如何实际更改数据。我已经阅读了大量关于这个主题的教程,但似乎没有任何东西是合适的。你能回答一些关于下面代码的问题吗

1) 在actionListener中,我调用tab.getModel().getValueAt(1,1)和tab.getValueAt(1,1)。我得到相同的数据,“Petty.”如果它提供相同的数据,“getModel()”是否必要


我认为“getModel()”允许我访问在CustomTable.java类中编写的任何自定义方法,但这似乎不是真的

命令选项卡.getModel().setValueAt(窗格1,2);什么也不做。它甚至没有在方法中运行System.out.println命令。所以这个方法甚至没有被调用

2) 为什么我可以叫“getValueAt”而不是“setValueAt”


我编写了一个名为“test()”的新方法。如果我在actionPerformed方法中调用它,它将生成一个编译错误。就好像这个方法不存在似的

3) 那么,我如何在AbstractTable模型中调用这些自定义方法呢

在我现在使用的程序中,该表由SQL查询的结果填充。我在服务类中有一个方法,该方法使用从查询生成的自定义对象填充下面的ArrayList。它可以很好地显示数据

我在AbstractTableModel类中编写了一个方法“reQuery”,该方法调用服务中的一个方法,并使用新查询中的数据重新填充ArrayList。这个方法工作正常,但我不能调用它(更不用说更新表数据了)

我错过了什么


主要方法是“newmainframe();”,下面是MainFrame和CustomTable类

package scratchpad3;
import javax.swing.*;
import java.awt.event.*;
public class MainFrame extends JFrame implements ActionListener {

JPanel pane = new JPanel();
JButton butt = new JButton("Push Me To Update The Table");
JTable tab = new JTable(new CustomTable());

MainFrame(){
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(1000,200,1000,1000);
    pane.setLayout(null);
    add(pane);   
    butt.setBounds(20,10,200,100);
    butt.addActionListener(this);
    pane.add(butt);     
    tab.setBounds(20,125,500,500);
    pane.add(tab);
        tab.setValueAt(pane, 1, 2);
    }    
public void actionPerformed(ActionEvent e){
    System.out.println("With getModel() " + tab.getModel().getValueAt(1, 1) );
    System.out.println("Without getModel() " + tab.getValueAt(1, 1) );
    tab.getModel().setValueAt("Tampa", 1, 2); 
    tab.getModel().test();
    }    
}
CustomTable.java

package scratchpad3;
import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;

public class CustomTable extends AbstractTableModel {
    String[] colName = {"First Name", "Last Name", "City", "State"};
    ArrayList<String[]> rows = new ArrayList<String[]>();
    public String getColumnName(int col){
        return colName[col];
    }

    public int getColumnCount(){
        return colName.length;
    }

    public int getRowCount(){
        return rows.size();
    }

    public String getValueAt(int row, int col){
        System.out.println("getValueAt method was called."); //To verify the method was called
        String[] s = rows.get(row);
        return s[col];
    }

    public boolean isCellEditable(int col, int row){
        return true;
    }

    public void setValueAt(String s, int row, int col){
        System.out.println("setValueAt method was called"); //To verify the method was called
        rows.get(row)[col] = s;
        fireTableDataChanged();
    }

    public void test(){
        System.out.println("Test");
    }

    CustomTable(){
        rows.add(new String[]{"Bob", "Barker", "Glendale", "CA"});
        rows.add(new String[]{"Tom", "Petty", "Jacksonville", "FL"});
    }
}
pad3;
导入javax.swing.table.AbstractTableModel;
导入java.util.ArrayList;
公共类CustomTable扩展了AbstractTableModel{
字符串[]colName={“名字”、“姓氏”、“城市”、“州”};
ArrayList行=新的ArrayList();
公共字符串getColumnName(int-col){
返回colName[col];
}
public int getColumnCount(){
返回colName.length;
}
public int getRowCount(){
返回rows.size();
}
公共字符串getValueAt(int行,int列){
System.out.println(“调用了getValueAt方法”);//验证调用了该方法
字符串[]s=rows.get(row);
返回s[col];
}
公共布尔值可编辑(整数列,整数行){
返回true;
}
public void setValueAt(字符串s,整数行,整数列){
System.out.println(“调用了setValueAt方法”);//验证调用了该方法
rows.get(row)[col]=s;
fireTableDataChanged();
}
公开无效测试(){
系统输出打印(“测试”);
}
自定义表(){
add(新字符串[]{“Bob”、“Barker”、“Glendale”、“CA”});
add(新字符串[]{“Tom”、“Petty”、“Jacksonville”、“FL”});
}
}

您没有重写
AbstractTableModel.setValueAt
方法,因为您使用了不正确的签名。它应该是
public void setValueAt(Object s,int row,int col)
而不是
public void setValueAt(String s,int row,int col)

1+。另外,不要忘了使用
@Override
注释,以便编译器能够捕捉到此类错误。非常感谢。成功了。我将“String s”改为“Object s”,接下来的两行我将“=s;”改为“=(String)s;”现在我可以更新表了……现在我需要了解如何在重新运行SQL查询的基础上重新填充整个表……假设我有一个完全重新填充“行”的方法。类似于public void newRows(){rows=makeneewArraList();}。然后我希望新行显示在JTable中。我能想到的唯一方法是从一个新方法调用“fireTableDataChanged();”。但是,与test()方法一样,我如何调用这些扩展方法?@MichaelCornn:您有了一个新的不同问题,可能应该接受Losa的答案,并在自己的stackoverflow线程中提出您的新问题。此外,请不要在注释中发布代码,因为它们在注释中不可读。