JTable表格实现动态更新的两种方式

JTable表格的构造方法

JTable()
构造一个默认 JTable是使用默认的数据模型,默认的列模型和默认的选择模型对其进行初始化。
JTable(int numRows, int numColumns)
构造一个 JTable与 numRows和 numColumns使用空单元格 DefaultTableModel 。
JTable(Object[][] rowData, Object[] columnNames)
构造一个 JTable以显示二维数组中的值 rowData ,列名称为 columnNames 。
JTable(TableModel dm)
构造一个 JTable ,它以 dm为数据模型,默认列模型和默认选择模型。
JTable(TableModel dm, TableColumnModel cm)
构造一个 JTable ,它以 dm作为数据模型 cm , cm作为列模型,以及默认选择模型。
JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm)
构造一个 JTable ,它以 dm作为数据模型 cm , cm作为列模型, sm作为选择模型。
JTable(Vector rowData, Vector columnNames)

方法一
通过表格方法updateUI配合Vector使用


import java.awt.Container;
import java.awt.Font;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Demo extends JFrame {
    private static final long serialVersionUID = 1L;
    public Container panel = null;
    private JScrollPane jsp;
    private JTable studentTable;;
    private JButton cancleButton;
    private Vector> vs;
    String[][] str1 = { { "1", "2", "3" }, { "11", "22", "33" } };
    String[][] str3 = { { "4", "5", "6" }, { "44", "55", "66" } };

    String[] str2 = { "第一行", "第二行", "第三行" };

    public Demo() {
        panel = this.getContentPane();
        panel.setLayout(null);
        List list = Arrays.asList("第一行", "第二行", "第三行");
        Vector v = new Vector(list);

        vs = new Vector>();

        List list1 = Arrays.asList("1", "2", "3");
        Vector v1 = new Vector(list1);

        List list2 = Arrays.asList("11", "22", "33");
        Vector v2 = new Vector(list2);

        List list3 = Arrays.asList("111", "222", "333");
        Vector v3 = new Vector(list3);

        vs.add(v1);
        vs.add(v2);
        vs.add(v3);

        studentTable = new JTable(vs, v);
        studentTable.setFont(new Font("宋体", Font.BOLD, 40));
        studentTable.setRowHeight(40);

        jsp = new JScrollPane(studentTable);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(jsp);
        jsp.setBounds(160, 100, 500, 400);

        cancleButton = new JButton("删除");
        cancleButton.addActionListener(e -> remove());

        panel.add(cancleButton);

        cancleButton.setBounds(360, 510, 100, 50);
        studentTable.updateUI();

        this.setLocation(450, 200);
        this.setSize(800, 650);
        this.setVisible(true);

    }

    private void remove() {
        int i = studentTable.getSelectedRows().length - 1;
        while (i >= 0) {
            // System.out.println(studentTable.getSelectedRows()[i--]);
            vs.remove(studentTable.getSelectedRows()[i--]);
            studentTable.updateUI();
        }
    }

    public static void main(String[] args) {
        new Demo();
    }
}

方法二
使用TableModel构造Jtable 在TableModel内调用fireTableDataChanged()方法,使表格进行动态刷新

通过每次调用refrese()方法,刷新表格内数据


public class MyTableModel extends AbstractTableModel {

    private final String[] columnNames = {"编号","教师信息","教授科目","电话号码","信息标题"};
    private Object[][] rowData = null; 

    public MyTableModel(Object[][] rowData) {
        this.rowData = rowData;
    }
    @Override
    public int getRowCount() {
        return rowData.length;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return rowData[rowIndex][columnIndex];
    }
    public void refresh(Object[][] rowData){
        this.rowData = rowData;
        this.fireTableDataChanged();
    }
}

你可能感兴趣的:(JAVA,GUI)