JTable 和 AbstractTableModel 几个关键点

JTable 和 AbstractTableModel 几个关键点1重写的方法@OverridepublicintgetColumnCount(){//TODOAuto-generatedmethodstubreturnthis.columnNames.size();}@OverridepublicStringgetColumnName…

JTable

1    重写的方法

@Override
    public int getColumnCount() {
        // TODO Auto-generated method stub
        return this.columnNames.size();
    }

    @Override
    public String getColumnName(int column) {
        // TODO Auto-generated method stub
        return (String) this.columnNames.get(column);
    }

    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return this.rowData.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        // TODO Auto-generated method stub
        return ((Vector)this.rowData.get(rowIndex)).get(columnIndex);
    }

2   JTable 中有更新数据模型的方法  setModel(新模型实例)

StuModel sm = new StuModel("select * from stu where name like '%"+s+"%'");
        jt.setModel(sm);

 

以下代码供参考

package stuMS;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.*;

public class NewMs extends JFrame implements ActionListener{

    /**
     * @param args
     */
    JPanel jp1,jp2;
    JLabel jl1;
    JButton jb1,jb2,jb3,jb4;
    JTable jt;
    JScrollPane jsp;
    JTextField jtf;
    
    

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        NewMs nm  = new NewMs();
    }
    
    public NewMs(){
        jp1 = new JPanel();
        jtf = new JTextField(10);
        jb1= new JButton("查询");
        jb1.addActionListener(this);
        jl1 = new JLabel("输入名字");
        
        jp1.add(jl1);
        jp1.add(jtf);
        jp1.add(jb1);
        
        jp2 = new JPanel();
        jb2 = new JButton("tianjia");
        jb3 = new JButton("xiugai");
        jb4 = new JButton("shanchu");
        
        jp2.add(jb2);
        jp2.add(jb3);
        jp2.add(jb4);
        
        
        StuModel sm = new StuModel("select * from stu");
        
        jt = new JTable(sm);
        
        jsp = new JScrollPane(jt);
        
        this.add(jp1,BorderLayout.NORTH);
        this.add(jp2,BorderLayout.SOUTH);
        this.add(jsp,BorderLayout.CENTER);
        this.setSize(400,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==jb1){
            System.out.println("i want query");
        }
        String s = jtf.getText().trim();
        StuModel sm = new StuModel("select * from stu where name like '%"+s+"%'");
        jt.setModel(sm);
    }

}

 

 

package stuMS;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;

public class StuModel extends AbstractTableModel{
    
    Vector rowData, columnNames;
    
    PreparedStatement  ps = null;
    Connection ct = null;
    ResultSet rs = null;
    
    public StuModel(String sql){
        columnNames = new Vector<String>();
        columnNames.add("ID");
        columnNames.add("name");
        columnNames.add("sex");
        columnNames.add("bornin");
        columnNames.add("major");
        
        rowData = new Vector<Vector<String>>();
    

        
        try {
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
            ct = DriverManager.getConnection("jdbc:microsoft:sqlserver://hzz:1433;databaseName=Heros","sa","13871955777");
            ps = ct.prepareStatement(sql);
            rs = ps.executeQuery();
            
            while(rs.next()){
                Vector hang = new Vector<String>();
                hang.add(rs.getInt(1));
                hang.add(rs.getString(2));
                hang.add(rs.getString(3));
                hang.add(rs.getString(4));
                hang.add(rs.getString(5));
                rowData.add(hang);
                
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
                try {
                    if(rs!=null)rs.close();
                    if(ps!=null) ps.close();
                    if(ct!=null) ct.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }
        
    }
    
//    public StuModel(){
//        String s = "select * from stu";
//        StuModel(s);
//        
//    }
    

    @Override
    public int getColumnCount() {
        // TODO Auto-generated method stub
        return this.columnNames.size();
    }

    @Override
    public String getColumnName(int column) {
        // TODO Auto-generated method stub
        return (String) this.columnNames.get(column);
    }

    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return this.rowData.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        // TODO Auto-generated method stub
        return ((Vector)this.rowData.get(rowIndex)).get(columnIndex);
    }

}

 

转载于:https://www.cnblogs.com/hzzhero/p/4599024.html

今天的文章JTable 和 AbstractTableModel 几个关键点分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/31070.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注