简体   繁体   中英

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

I've learned how to display a JTable in a Frame, but I can't figure out how to actually change the data. I've read a ton of tutorials on the subject, but nothing seems to click. Can you answer some questions about the code below?

1) In the actionListener, I call tab.getModel().getValueAt(1,1) and tab.getValueAt(1,1). I get the same data, "Petty." Is the "getModel()" necessary if it delivers the same data?


I figured that "getModel()" allowed me to access any custom methods I wrote in the CustomTable.java class, but that doesn't seem to be true.

The command tab.getModel().setValueAt(pane, 1, 2); does nothing. It doesn't even run the System.out.println command in the method. So the method isn't even getting called.

2) Why can I call "getValueAt" but not "setValueAt"?


I wrote a new method called "test()". If I call it in the actionPerformed method, it's produces a compile error. It's as if the method doesn't exist.

3) So how do I call these custom methods in AbstractTable Model?

In the program I'm working now, the table is populated by the results of a SQL query. I have a method in a Service class that populates the ArrayList below with custom objects built from the query. It displays the data just fine.

I wrote a method "reQuery" in the AbstractTableModel class, which calls a method in the Service and repopulates the ArrayList with data from a fresh query. This method works properly, but I can't call it (much less update the table data).

What am I missing here?


The main method is just "new MainFrame();" THe MainFrame and CustomTable classes are below.

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"});
    }
}

You don't override the AbstractTableModel.setValueAt method, because you use incorrect signature. It should be public void setValueAt(Object s, int row, int col) instead of public void setValueAt(String s, int row, int col) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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