子窗口刷新父窗口问题 /*就想在这个位置实现主窗口的刷新*/ mp=new MainPanel(); mp.freshTable();...重新new的 当然没法刷新,至少要获取到父窗口吧. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那怎么获取啊,getOwner()? 看你的程序,其实根本上还是对JTable的API掌握不熟悉,没有及时刷新界面,其实完全没必要用你说的刷新的思路,你把数据库连接相关的也贴下,或者把程序需要的数据库表结构列出来,我直接给你改成可以及时刷新JTable的,如何? package kunpeng.ui;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import javax.swing.table.AbstractTableModel;import javax.swing.*;public class ResultSetTableModel extends AbstractTableModel { private static final long serialVersionUID = 1L; /** * Construction of ResultSetTableModel * * @param aResultSet * data obtained from database query. The type of ResultSet must * be TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE * @param aColumnNames * column names of the JTable. */ public ResultSetTableModel(ResultSet aResultSet, String[] aColumnNames) { rs = aResultSet; columnNames = aColumnNames; try { rsmd = rs.getMetaData(); } catch (SQLException e) { e.printStackTrace(); } } public String getColumnName(int c) { if (c < columnNames.length) return columnNames[c]; else return ""; } public int getColumnCount() { try { return rsmd.getColumnCount();//返回列数 } catch (SQLException e) { e.printStackTrace(); return 0; } } public int getRowCount() { //返回行数 try { rs.last(); return rs.getRow(); } catch (SQLException e) { e.printStackTrace(); return 0; } } public Object getValueAt(int r, int c) { try { rs.absolute(r + 1); return rs.getObject(c + 1); } catch (SQLException e) { e.printStackTrace(); return null; } } private ResultSet rs; private ResultSetMetaData rsmd; private String[] columnNames;} //数据库连接import kunpeng.ui.ResultSetTableModel;import kuneng.SetupDB.SetupDB;/** * This class defines some operation of database. * * @author lansoul */public class KunPengConnection{ public static Connection getConnection() { Connection con = null; try { Properties props = new Properties();//properties文件是一个文本文件 FileInputStream fis = new FileInputStream("database.properties");//这个文件保存着数据库属性 props.load(fis); fis.close(); String drivers = props.getProperty("jdbc.drivers"); Class.forName(drivers); //数据库驱动 String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); con = DriverManager.getConnection(url, username, password);//建立数据库链接 return con; } catch (SQLException e) { JOptionPane.showMessageDialog((JFrame) null, "系统与数据库连接失败! 请重启系统或查看 readme 文件的配置说明!", "系统错误", JOptionPane.ERROR_MESSAGE); SetupDB.createDBConfigFile();//SetupDB产生相应配置文件 SetupDB.setupDB(); System.exit(1); return null; } catch (Exception e) { System.out.println("数据库配置文件出错!"); SetupDB.createDBConfigFile(); System.out.println("重建数据库配置文件, 请重新启动本系统!"); System.exit(1); return null; } } public static ResultSetTableModel getTableModel(String query, String[] columnNames) { Connection con = null; ResultSet rs = null; ResultSetTableModel model = null; try { con = getConnection(); //声明statement Statement stmt = con .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(query); model = new ResultSetTableModel(rs, columnNames); return model; } catch (SQLException e) { e.printStackTrace(); return model; } }} public void showDialog(Component parent){//你NewContactDialog 这个方法里不是已经获取到parent了么,我看你的代码parent传的是MainWindow,//那么你需要在这里做this.mw=parent;this.mw=parent;...}//然后你的MainWindow需要有一个刷新MainPanel的方法... public void refreshMainPanel(){ mainPanel.refreshTable(); mainPanel.repaint(); }... //在你需要刷新的这个位置 /*就想在这个位置实现主窗口的刷新*/ mp=new MainPanel(); mp.freshTable(); dialog.getOwner().repaint();//改成mw.freshMainPanel();}我自己没代码调试 只能想当然了,你自己试试吧. 需求文档如何写? java 跟踪鼠标~~ 大家帮忙啊,我使用MySQLQueryBrowser.exe时,输入SQL语句时不能输入中文怎么办呀,帮忙啊 如何将区位码转换成汉字?? 我是新学的,遇到个问题,有人能帮我吗?感谢~ 最简单的HELLO WORLD 怎么出错了…… 怎样提取一个文件所在的目录? 一个数字输入的初级问题! 关于Random函数问题,本人在线给分! 如何使用Java制作ie风格的工具栏? 如何检查eclipse工程里哪些类头没有注释? javamail如何获取邮件头信息中的IP地址呢?
import java.sql.ResultSetMetaData;
import java.sql.SQLException;import javax.swing.table.AbstractTableModel;
import javax.swing.*;public class ResultSetTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L; /**
* Construction of ResultSetTableModel
*
* @param aResultSet
* data obtained from database query. The type of ResultSet must
* be TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE
* @param aColumnNames
* column names of the JTable.
*/
public ResultSetTableModel(ResultSet aResultSet, String[] aColumnNames) {
rs = aResultSet;
columnNames = aColumnNames;
try {
rsmd = rs.getMetaData();
} catch (SQLException e) {
e.printStackTrace();
}
} public String getColumnName(int c) {
if (c < columnNames.length)
return columnNames[c];
else
return "";
} public int getColumnCount() {
try {
return rsmd.getColumnCount();//返回列数
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
} public int getRowCount() { //返回行数
try {
rs.last();
return rs.getRow();
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
} public Object getValueAt(int r, int c) {
try {
rs.absolute(r + 1);
return rs.getObject(c + 1);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
} private ResultSet rs; private ResultSetMetaData rsmd; private String[] columnNames;
}
//数据库连接
import kunpeng.ui.ResultSetTableModel;
import kuneng.SetupDB.SetupDB;/**
* This class defines some operation of database.
*
* @author lansoul
*/
public class KunPengConnection{
public static Connection getConnection() {
Connection con = null;
try {
Properties props = new Properties();//properties文件是一个文本文件
FileInputStream fis = new FileInputStream("database.properties");//这个文件保存着数据库属性
props.load(fis);
fis.close(); String drivers = props.getProperty("jdbc.drivers");
Class.forName(drivers); //数据库驱动
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
con = DriverManager.getConnection(url, username, password);//建立数据库链接
return con;
} catch (SQLException e) {
JOptionPane.showMessageDialog((JFrame) null,
"系统与数据库连接失败! 请重启系统或查看 readme 文件的配置说明!", "系统错误",
JOptionPane.ERROR_MESSAGE);
SetupDB.createDBConfigFile();//SetupDB产生相应配置文件
SetupDB.setupDB();
System.exit(1);
return null;
} catch (Exception e) {
System.out.println("数据库配置文件出错!");
SetupDB.createDBConfigFile();
System.out.println("重建数据库配置文件, 请重新启动本系统!");
System.exit(1);
return null;
}
} public static ResultSetTableModel getTableModel(String query,
String[] columnNames) {
Connection con = null;
ResultSet rs = null;
ResultSetTableModel model = null;
try {
con = getConnection(); //声明statement
Statement stmt = con
.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(query);
model = new ResultSetTableModel(rs, columnNames);
return model;
} catch (SQLException e) {
e.printStackTrace();
return model;
}
}
}
public void showDialog(Component parent){
//你NewContactDialog 这个方法里不是已经获取到parent了么,我看你的代码parent传的是MainWindow,
//那么你需要在这里做this.mw=parent;
this.mw=parent;
...
}
//然后你的MainWindow需要有一个刷新MainPanel的方法
...
public void refreshMainPanel(){
mainPanel.refreshTable();
mainPanel.repaint();
}
...
//在你需要刷新的这个位置
/*就想在这个位置实现主窗口的刷新*/
mp=new MainPanel();
mp.freshTable();
dialog.getOwner().repaint();
//改成
mw.freshMainPanel();}我自己没代码调试 只能想当然了,你自己试试吧.