| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1209 人关注过本帖
标题:表格单击事件失效,请教高手指点
只看楼主 加入收藏
mahayu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:160
注 册:2007-8-3
收藏
 问题点数:0 回复次数:1 
表格单击事件失效,请教高手指点
我做了一个从数据库检索记录,把结果反馈到表格里,单击表格任意一行,可以把对应的数据显示到文本框里,我用表格的jTable1_mouseClicked事件,获取选中行的数据,并反馈到文本框里,但是现在经过查询新数据后,表格的这个单击事件就无效了
详细看附件

package tablesql;

import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.table.JTableHeader;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

public class Frame1
    extends JFrame {
  JPanel contentPane;
  JTable jTable1 = new JTable();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextField T1 = new JTextField();
  JTextField T2 = new JTextField();
  JButton Bs = new JButton();
  JTextField Ts = new JTextField();
  JTextField T3 = new JTextField();
  public Frame1() {
    try {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      jbInit();
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }

//查询输出表格的函数
  public void fz(String sq) {
    String a = sq;
    String sql;
    Vector aa = new Vector();
    dbsql db = new dbsql();
    db.connect();
    if (!sq.equals("")) {
      sql = "select * from zuser where ID='" + a + "'";
    }
    else {
      sql = "select * from zuser ";
    }

    ResultSet rs = db.query(sql);

    try {
      while (rs.next()) {
        Vector t = new Vector();
        for (int k = 1; k < 6; k++) {
          t.add(rs.getString(k));
        }
        aa.add(t);
      }
    }
    catch (SQLException ex) {
      System.out.print(ex.toString());
    }

    Vector kk = new Vector();
    kk.add("a");
    kk.add("b");
    kk.add("c");

    this.jScrollPane1.getViewport().remove(jTable1);
    jTable1 = new JTable(aa, kk);
    this.jScrollPane1.getViewport().add(jTable1);
    db.close();
  }

  /**
   * Component initialization.
   *
   * @throws java.lang.Exception
   */
  private void jbInit() throws Exception {
    contentPane = (JPanel) getContentPane();
    contentPane.setLayout(null);
    setSize(new Dimension(400, 300));
    setTitle("Frame Title");
    fz("");  //初始化表格的值
    jTable1.setBounds(new Rectangle(68, 55, 217, 162));
    jTable1.addMouseListener(new Frame1_jTable1_mouseAdapter(this));
    jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    jScrollPane1.setBounds(new Rectangle(55, 46, 302, 199));
    jScrollPane1.setBorder(BorderFactory.createLineBorder(Color.black));
    jScrollPane1.setBounds(new Rectangle(47, 89, 246, 150));
    jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.
                                              HORIZONTAL_SCROLLBAR_AS_NEEDED);
    jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.
                                            VERTICAL_SCROLLBAR_AS_NEEDED);
    jScrollPane1.setAutoscrolls(true);
    T1.setBounds(new Rectangle(56, 63, 77, 20));
    T2.setBounds(new Rectangle(148, 64, 77, 20));
    Bs.setBounds(new Rectangle(135, 24, 81, 23));
    Bs.setToolTipText("");
    Bs.setText("查询");
    Bs.addActionListener(new Frame1_Bs_actionAdapter(this));
    Ts.setBounds(new Rectangle(42, 25, 77, 20));
    T3.setBounds(new Rectangle(250, 64, 77, 20));

    contentPane.add(jScrollPane1);
    contentPane.add(T1);
    contentPane.add(T2);
    contentPane.add(Bs);
    contentPane.add(Ts);
    contentPane.add(T3);
    jScrollPane1.getViewport().add(jTable1);
  }

  /**
   * 单击表格时,获取所单击行的值
   * @param e MouseEvent
   */
  public void jTable1_mouseClicked(MouseEvent e) {
    jTable1.updateUI();
    int row = jTable1.getSelectedRow();
    Object oo = jTable1.getModel().getValueAt(row, 0);
    Object b = jTable1.getModel().getValueAt(row, 1);
    T1.setText(oo.toString());
    T2.setText(b.toString());
  }

  public void Bs_actionPerformed(ActionEvent e) {
    fz(Ts.getText());
  }


  public void jTable1_mouseReleased(MouseEvent e) {
    jScrollPane1.getViewport().add(jTable1);
    int row = jTable1.getSelectedRow();

    Object oo = jTable1.getModel().getValueAt(row, 0);
    Object b = jTable1.getModel().getValueAt(row, 1);
    T1.setText(oo.toString());
    T2.setText(b.toString());
  }
}

class Frame1_Bs_actionAdapter
    implements ActionListener {
  private Frame1 adaptee;
  Frame1_Bs_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.Bs_actionPerformed(e);
  }
}

class Frame1_jTable1_mouseAdapter
    extends MouseAdapter {
  private Frame1 adaptee;
  Frame1_jTable1_mouseAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }

  public void mouseClicked(MouseEvent e) {
    adaptee.jTable1_mouseClicked(e);
  }

  public void mouseReleased(MouseEvent e) {
    adaptee.jTable1_mouseReleased(e);
  }
}

TableSql.rar (54.39 KB)
搜索更多相关主题的帖子: import 单击 表格 java awt 
2008-01-26 15:18
mahayu
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:160
注 册:2007-8-3
收藏
得分:0 
继续求助,顶上去.

研究Java和C#,,Jsp,Sql等各类编程技术, QQ:442013799  QQ群:47934757
2008-01-28 13:02
快速回复:表格单击事件失效,请教高手指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040324 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved