用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

TableModel

2015-03-15 作者: 云代码会员举报

[java]代码库

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jtabel;

import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;


public class TableModel extends JFrame {
    public DefaultTableModel tableModel;
    public JTable table;
    JScrollPane tablePane;
    Connection ct;
    PreparedStatement ps;
    ResultSet rs;
    public ListSelectionListener selectAndDisplay;
    public JTextField show;
    int s;
    public TableModel(){
        selectAndDisplay = new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
               int row = table.getSelectedRow();
             //   System.out.println("转化前 :"+row);
               if(row!=-1){
                   s = table.convertColumnIndexToModel(row);
                   System.out.println("转化后:"+s);
                   show.setText((String) table.getValueAt(s,0));
               }
            }
        };
        
        String[] columnStrings ={
          "goods_Id",
            "goods_name",
            "goods_price",
            "goods_number"
        };
        
        tableModel = new DefaultTableModel();
        for(String i :columnStrings)
        tableModel.addColumn(i);
        
        try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       ct=DriverManager.getConnection("jdbc:odbc:bean");
          System.out.println("select * from test.dbo,Inventory");
          ps = ct.prepareStatement("select * from Market.dbo.Inventory");
          rs= ps.executeQuery();
          while(rs.next()){
              Vector<String> Ve= new Vector<>();
              Ve.add(rs.getString(1));
               Ve.add(rs.getString(2));
                Ve.add(rs.getString(3));
                 Ve.add(rs.getString(4));
                 tableModel.addRow(Ve);
          }
        }catch(Exception e){
            System.out.println("ERROR:"+e);
        }

       
        
        table = new JTable(tableModel);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getSelectionModel().addListSelectionListener(selectAndDisplay);
        TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<>(tableModel);
        table.setRowSorter(sorter);
        
        
        show = new JTextField();
        tablePane = new JScrollPane(table);
        this.setLayout(new BorderLayout());
        this.add(tablePane);
        this.add(show,BorderLayout.SOUTH);
        this.setTitle("JTable Model Test ");
        this.setSize(500,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
    }
    public static void main(String[] args) {
        new TableModel();
    }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...