怎么实现JAVA界面的自动刷新啊?
关键代码:voidok_actionPerformed(ActionEvente){try{//连接数据库//Stringname="com.microsoft.jdbc...
关键代码:
void ok_actionPerformed(ActionEvent e) {
try { //连接数据库
// String name = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
url="jdbc:microsoft:sqlserver://PC-200904211802:1433;DatabaseName=Student";
//url="jdbc:odbc:Student";
//mydb为数据库
String user="sa";
String password="198701";
Connection con= DriverManager.getConnection(url,user,password);
ps = con.createStatement(ResultSet.
TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
int i = 0;
try {
if (xh.isSelected()) {
rs = ps.executeQuery("select * from result where student_ID='" +
input.getText().trim() + "'");
}
else if (kc.isSelected()) {
rs = ps.executeQuery("select * from result where course_Name='" +
input.getText().trim() + "'");
}
else if (bh.isSelected()) {
rs = ps.executeQuery("select * from result where class_NO='" +
input.getText().trim() + "'");
}
else
rs = ps.executeQuery("select * from result");
}
catch (Exception err) {
err.getMessage();
JOptionPane.showMessageDialog(null, "没有数据!");
}
while (rs.next()) {
rowData[i][0] = rs.getString("exam_No");
rowData[i][1] = rs.getString("student_ID");
rowData[i][2] = rs.getString("student_Name");
rowData[i][3] = rs.getString("class_NO");
rowData[i][4] = rs.getString("course_Name");
rowData[i][5] = rs.getString("result");
i++;
}
}
catch (Exception err) {
err.getMessage();
JOptionPane.showMessageDialog(null, err);
}
}
}class sacnresult_ok_actionAdapter
implements java.awt.event.ActionListener {
sacnresult adaptee;
sacnresult_ok_actionAdapter(sacnresult adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.ok_actionPerformed(e);
}
}
本来应该是当我点击确定按钮时就可以在表里显示结果,可是实际确实我点击后没反应,但是要是我把窗口最小化之后在放大,那结果就出现了,请问你们谁知道在里面加什么代码可以让它在我点击确定的时候就直接显示查询后的结果啊???高手们,拜托了 展开
void ok_actionPerformed(ActionEvent e) {
try { //连接数据库
// String name = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
url="jdbc:microsoft:sqlserver://PC-200904211802:1433;DatabaseName=Student";
//url="jdbc:odbc:Student";
//mydb为数据库
String user="sa";
String password="198701";
Connection con= DriverManager.getConnection(url,user,password);
ps = con.createStatement(ResultSet.
TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
int i = 0;
try {
if (xh.isSelected()) {
rs = ps.executeQuery("select * from result where student_ID='" +
input.getText().trim() + "'");
}
else if (kc.isSelected()) {
rs = ps.executeQuery("select * from result where course_Name='" +
input.getText().trim() + "'");
}
else if (bh.isSelected()) {
rs = ps.executeQuery("select * from result where class_NO='" +
input.getText().trim() + "'");
}
else
rs = ps.executeQuery("select * from result");
}
catch (Exception err) {
err.getMessage();
JOptionPane.showMessageDialog(null, "没有数据!");
}
while (rs.next()) {
rowData[i][0] = rs.getString("exam_No");
rowData[i][1] = rs.getString("student_ID");
rowData[i][2] = rs.getString("student_Name");
rowData[i][3] = rs.getString("class_NO");
rowData[i][4] = rs.getString("course_Name");
rowData[i][5] = rs.getString("result");
i++;
}
}
catch (Exception err) {
err.getMessage();
JOptionPane.showMessageDialog(null, err);
}
}
}class sacnresult_ok_actionAdapter
implements java.awt.event.ActionListener {
sacnresult adaptee;
sacnresult_ok_actionAdapter(sacnresult adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.ok_actionPerformed(e);
}
}
本来应该是当我点击确定按钮时就可以在表里显示结果,可是实际确实我点击后没反应,但是要是我把窗口最小化之后在放大,那结果就出现了,请问你们谁知道在里面加什么代码可以让它在我点击确定的时候就直接显示查询后的结果啊???高手们,拜托了 展开
2个回答
2009-06-10
展开全部
while (rs.next()) {
rowData[i][0] = rs.getString("exam_No");
rowData[i][1] = rs.getString("student_ID");
rowData[i][2] = rs.getString("student_Name");
rowData[i][3] = rs.getString("class_NO");
rowData[i][4] = rs.getString("course_Name");
rowData[i][5] = rs.getString("result");
i++;
}
//在此处添加如下代码:
xxxxxxxxxxxx_model.fireTableDataChanged();
//或:
table.setModel(new XxxxxxxxxxTableModel(rowData,.......));
解决方法1:
如果你的表格模型扩展自AbstractTableModel,
表格数据改变后,则调用模型的fireTableDataChanged()方法;
解决方法2:
重新设置表格模型.
例如:
table.setModel(new DefaultTableModel(data,columnNames));
下面是我写的一个上例子.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTableTest
{
public static void main(String args[])
{
JTableTestFrame f=new JTableTestFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class JTableTestFrame extends JFrame
{
private JTable table=null;
private ArrayTableModel atm=null;
private JButton reload=null;
private final static String[] columnNames={"Number","addend","augend","sum"};
private int start=0;
public JTableTestFrame()
{
atm=new ArrayTableModel(columnNames);
table=new JTable(atm);
reload=new JButton("Reload");
reload.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reload();
}
});
JPanel p=new JPanel(new BorderLayout());
p.add(new JLabel("JTable Test"),BorderLayout.NORTH);
p.add(new JScrollPane(table),BorderLayout.CENTER);
p.add(reload,BorderLayout.SOUTH);
this.add(p);
this.setSize(300,300);
this.setVisible(true);
}
public void reload()
{
int rows=10;
int cols=columnNames.length;
Object[][] data=new Object[rows][cols];
for(int i=0;i<rows;i++)
{
start+=i;
data[i][0]=start;
data[i][1]=start+1;
data[i][2]=start+2;
data[i][3]=start*2+3;
}
atm.setData(data);
atm.fireTableDataChanged();
}
}
class ArrayTableModel extends AbstractTableModel
{
private String[] columnNames=null;
private Object[][] data=null;
public ArrayTableModel(String[] columnNames)
{
this.columnNames=columnNames;
this.data=new Object[1][columnNames.length];
}
public ArrayTableModel(Object[][] data,String[] columnNames)
{
this.data=data;
this.columnNames=columnNames;
}
public Object getValueAt(int r,int c)
{
return this.data[r][c];
}
public int getRowCount()
{
return this.data.length;
}
public int getColumnCount()
{
return this.columnNames.length;
}
public String[] getColumnNames() {
return columnNames;
}
public String getColumnName(int c)
{
return this.columnNames[c].toString();
}
public void setColumnNames(String[] columnNames) {
this.columnNames = columnNames;
}
public Object[][] getData() {
return data;
}
public void setData(Object[][] data) {
this.data = data;
}
}
rowData[i][0] = rs.getString("exam_No");
rowData[i][1] = rs.getString("student_ID");
rowData[i][2] = rs.getString("student_Name");
rowData[i][3] = rs.getString("class_NO");
rowData[i][4] = rs.getString("course_Name");
rowData[i][5] = rs.getString("result");
i++;
}
//在此处添加如下代码:
xxxxxxxxxxxx_model.fireTableDataChanged();
//或:
table.setModel(new XxxxxxxxxxTableModel(rowData,.......));
解决方法1:
如果你的表格模型扩展自AbstractTableModel,
表格数据改变后,则调用模型的fireTableDataChanged()方法;
解决方法2:
重新设置表格模型.
例如:
table.setModel(new DefaultTableModel(data,columnNames));
下面是我写的一个上例子.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTableTest
{
public static void main(String args[])
{
JTableTestFrame f=new JTableTestFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class JTableTestFrame extends JFrame
{
private JTable table=null;
private ArrayTableModel atm=null;
private JButton reload=null;
private final static String[] columnNames={"Number","addend","augend","sum"};
private int start=0;
public JTableTestFrame()
{
atm=new ArrayTableModel(columnNames);
table=new JTable(atm);
reload=new JButton("Reload");
reload.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
reload();
}
});
JPanel p=new JPanel(new BorderLayout());
p.add(new JLabel("JTable Test"),BorderLayout.NORTH);
p.add(new JScrollPane(table),BorderLayout.CENTER);
p.add(reload,BorderLayout.SOUTH);
this.add(p);
this.setSize(300,300);
this.setVisible(true);
}
public void reload()
{
int rows=10;
int cols=columnNames.length;
Object[][] data=new Object[rows][cols];
for(int i=0;i<rows;i++)
{
start+=i;
data[i][0]=start;
data[i][1]=start+1;
data[i][2]=start+2;
data[i][3]=start*2+3;
}
atm.setData(data);
atm.fireTableDataChanged();
}
}
class ArrayTableModel extends AbstractTableModel
{
private String[] columnNames=null;
private Object[][] data=null;
public ArrayTableModel(String[] columnNames)
{
this.columnNames=columnNames;
this.data=new Object[1][columnNames.length];
}
public ArrayTableModel(Object[][] data,String[] columnNames)
{
this.data=data;
this.columnNames=columnNames;
}
public Object getValueAt(int r,int c)
{
return this.data[r][c];
}
public int getRowCount()
{
return this.data.length;
}
public int getColumnCount()
{
return this.columnNames.length;
}
public String[] getColumnNames() {
return columnNames;
}
public String getColumnName(int c)
{
return this.columnNames[c].toString();
}
public void setColumnNames(String[] columnNames) {
this.columnNames = columnNames;
}
public Object[][] getData() {
return data;
}
public void setData(Object[][] data) {
this.data = data;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询