swing中jTable的数据动态添加效果

 swing中jTable的数据动态添加效果
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login.frame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//import org.refworld.narcotics.map;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author wenliang
 */
public class Jtable extends JFrame implements ActionListener, Runnable {

    private JButton bt1, bt2, bt3;
    private JScrollPane jspane;
    private JTable table;
    private JPanel btnPanel, mainPanel;
    private DefaultTableModel model;
    int a = 0;
    int b = 1;

    public Jtable() {//构造方法
        initComponents();
        this.getContentPane().add("Center", mainPanel);
        this.setSize(600, 300);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    private void initComponents() {//构造面板
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        model = new DefaultTableModel(new Object[][]{{"原始列", "原始列", "原始列"},
                    {"原始列", "原始列", "原始列"}, {"原始列", "原始列", "原始列"}},
                new String[]{"表头1", "表头2", "表头3"});
        table = new JTable(model);
        jspane = new JScrollPane(table);
        bt1 = new JButton("加到第一行前");
        bt2 = new JButton("追加到最后一行");
        bt3 = new JButton("追加到当前行前");
        bt1.addActionListener(this);
        bt2.addActionListener(this);
        bt3.addActionListener(this);
        btnPanel = new JPanel(new GridLayout(1, 3));
        btnPanel.add(bt1);
        btnPanel.add(bt2);
        btnPanel.add(bt3);
        mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.add("Center", jspane);
        mainPanel.add("South", btnPanel);
    }

    public void actionPerformed(ActionEvent e) {
        Thread thread = new Thread(this);
        thread.start();

        Object object = e.getSource();
        if (object == bt1) { // 最加到第一行
            model.insertRow(0, new Object[]{"追加列_0", "追加列_0", "追加列_0"});
        } else if (object == bt2) {// 追加到最后行
        } else if (object == bt3) {// 追加到当前行前
            if (table.getSelectedRow() != -1) {
                model.insertRow(table.getSelectedRow(), new Object[]{
                            "追加列_0_0_0", "追加列_0_0_0", "追加列_0_0_0"});

            }

        }
    }

    /**
     * 测试MAIN方法已注释掉.....
     * @param args
     */
    public static void main(String args[]) {//main 方法
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Jtable jtable = new Jtable();

                jtable.setVisible(true);
            }
        });
    }

    public void run() {
        // TODO Auto-generated method stub

        while (true) {

            model.insertRow(model.getRowCount(), new Object[]{"追加列_0_0",
                        "追加列_0_0", "追加列_0_0"});
            // System.out.println(model.getRowCount());


            table.updateUI(); // 刷新

            System.out.println("刷新了一次" + a);
            a++;
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值