繁体   English   中英

Java的。 Swing JTable。 更改JTable中的数据并在表模型中调用自定义方法

[英]Java. Swing JTable. Changing data in a JTable and calling custom methods in the Table Model

我已经学会了如何在Frame中显示JTable,但我无法弄清楚如何实际更改数据。 我已经阅读了很多关于这个主题的教程,但似乎没有任何点击。 您能否回答一些有关以下代码的问题?

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


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

命令tab.getModel()。setValueAt(pane,1,2); 什么也没做。 它甚至不在该方法中运行System.out.println命令。 所以该方法甚至没有被调用。

2)为什么我可以调用“getValueAt”而不是“setValueAt”?


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

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

在我现在正在使用的程序中,该表由SQL查询的结果填充。 我在Service类中有一个方法,它使用从查询构建的自定义对象填充下面的ArrayList。 它显示数据就好了。

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

我在这里错过了什么?


主要方法只是“new MainFrame();” 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"});
    }
}

您不会覆盖AbstractTableModel.setValueAt方法,因为您使用了错误的签名。 它应该是public void setValueAt(Object s, int row, int col)而不是public void setValueAt(String s, int row, int col)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM