当前位置: 技术问答>java相关
请问如何即时刷新table中鼠标所选中的那条记录?程序代码见内。高分请教
来源: 互联网 发布时间:2015-06-28
本文导语: 运行下面我的代码,请问如何才能达到如下效果 问题: 当我点击那个按钮时,即时刷新table中鼠标所选中的那条记录中的第4个字段内容为可编辑文本框中的字符串?程序中我的操作无法达到这种效果。注意,需求是...
运行下面我的代码,请问如何才能达到如下效果
问题:
当我点击那个按钮时,即时刷新table中鼠标所选中的那条记录中的第4个字段内容为可编辑文本框中的字符串?程序中我的操作无法达到这种效果。注意,需求是即时刷新鼠标选中的这条记录,也就是说刷新这条记录后,它仍然处于鼠标选中的状态。
数据库目标环境:
设置数据源为OMT,通过jdbc-odbc链接sqlserver数据库,目标表名为serverlog,共有4个字段,均为varchar类型,长度15。(其实这些都无关紧要)
说明:
我试过将数据库设置为支持滚动或者不支持滚动(在程序中可以看出两者都可选择),对table操作也无法刷新成功,难道是我刷新所用方法错误?
源代码如下:
参考自java2核心技术卷2高级特性第6章高级Swing例6.8:ResultSetTable.java
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.sql.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.table.*;
public class DialogServerLog extends JDialog {
private JPanel jPMain = new JPanel();
private XYLayout xYLayout1 = new XYLayout();
private JPanel jPTableView = new JPanel();
private XYLayout xYLayout2 = new XYLayout();
private XYLayout xYLayout3 = new XYLayout();
private JPanel jPAllDo = new JPanel();
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JScrollPane tableJScrollPane;
private JTable table;//数据库表显示
private String[] value = new String[4];//Table中的一条数据各个字段
private JTextField jTextField3 = new JTextField();
private JTextField jTextField4 = new JTextField();
private boolean SCROLLABLE = true;//数据库是否支持滚动光标,此处支持为true
private ResultSet rs;
private Connection con;
private Statement stmt;
private JButton jButton1 = new JButton();
private ResultSetTableModel model;
//*/
public static void main(String[] args ) {
DialogServerLog ds = new DialogServerLog();
ds.show();
}
//*/
public DialogServerLog() {
setTitle(" 服务器日志信息维护");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
try {
String dbUrl = "jdbc:odbc:OMT";
String user = "sa";
String password = "";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbUrl, user, password);
if (SCROLLABLE) {
stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
else {
stmt = con.createStatement();
}
String query = "SELECT * FROM serverLog";
rs = stmt.executeQuery(query);
if (SCROLLABLE)
model = new ScrollingResultSetTableModel(rs);
else
model = new CachingResultSetTableModel(rs);
table = new JTable(model);
tableJScrollPane = new JScrollPane(table);//容器
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(new Dimension(565, 430));
jPMain.setLayout(xYLayout1);
jPTableView.setLayout(xYLayout2);
jPTableView.setBorder(BorderFactory.createLoweredBevelBorder());
jPAllDo.setLayout(xYLayout3);
jPAllDo.setBorder(BorderFactory.createEtchedBorder());
///鼠标选择一条记录,用文本框显示各字段
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
for(int i=0;i
问题:
当我点击那个按钮时,即时刷新table中鼠标所选中的那条记录中的第4个字段内容为可编辑文本框中的字符串?程序中我的操作无法达到这种效果。注意,需求是即时刷新鼠标选中的这条记录,也就是说刷新这条记录后,它仍然处于鼠标选中的状态。
数据库目标环境:
设置数据源为OMT,通过jdbc-odbc链接sqlserver数据库,目标表名为serverlog,共有4个字段,均为varchar类型,长度15。(其实这些都无关紧要)
说明:
我试过将数据库设置为支持滚动或者不支持滚动(在程序中可以看出两者都可选择),对table操作也无法刷新成功,难道是我刷新所用方法错误?
源代码如下:
参考自java2核心技术卷2高级特性第6章高级Swing例6.8:ResultSetTable.java
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.sql.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.table.*;
public class DialogServerLog extends JDialog {
private JPanel jPMain = new JPanel();
private XYLayout xYLayout1 = new XYLayout();
private JPanel jPTableView = new JPanel();
private XYLayout xYLayout2 = new XYLayout();
private XYLayout xYLayout3 = new XYLayout();
private JPanel jPAllDo = new JPanel();
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JScrollPane tableJScrollPane;
private JTable table;//数据库表显示
private String[] value = new String[4];//Table中的一条数据各个字段
private JTextField jTextField3 = new JTextField();
private JTextField jTextField4 = new JTextField();
private boolean SCROLLABLE = true;//数据库是否支持滚动光标,此处支持为true
private ResultSet rs;
private Connection con;
private Statement stmt;
private JButton jButton1 = new JButton();
private ResultSetTableModel model;
//*/
public static void main(String[] args ) {
DialogServerLog ds = new DialogServerLog();
ds.show();
}
//*/
public DialogServerLog() {
setTitle(" 服务器日志信息维护");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
try {
String dbUrl = "jdbc:odbc:OMT";
String user = "sa";
String password = "";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbUrl, user, password);
if (SCROLLABLE) {
stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
else {
stmt = con.createStatement();
}
String query = "SELECT * FROM serverLog";
rs = stmt.executeQuery(query);
if (SCROLLABLE)
model = new ScrollingResultSetTableModel(rs);
else
model = new CachingResultSetTableModel(rs);
table = new JTable(model);
tableJScrollPane = new JScrollPane(table);//容器
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setSize(new Dimension(565, 430));
jPMain.setLayout(xYLayout1);
jPTableView.setLayout(xYLayout2);
jPTableView.setBorder(BorderFactory.createLoweredBevelBorder());
jPAllDo.setLayout(xYLayout3);
jPAllDo.setBorder(BorderFactory.createEtchedBorder());
///鼠标选择一条记录,用文本框显示各字段
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
for(int i=0;i