1 JTable深入浅出
1.1 使用Array构造JTable表格
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import java.beans.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import javax.swing.table.DefaultTableModel;
public class SimpleTable extends JFrame {
private DefaultTableModel model;
public SimpleTable() {
try {
String[] colName = {"歌曲名称", "歌手", "是否收藏"};
Object[][] musicList = { {"心似狂潮", "羽泉", new Boolean(true)}, {"心似狂潮",
"羽泉", new Boolean(false)}
};
model = new DefaultTableModel(musicList, colName);
this.jTable1 = new JTable(musicList, colName);
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocale(new java.util.Locale("zh", "CN", ""));
this.setTitle("测试JTable");
this.setSize(500, 300);
this.setLocation(150, 150);
this.getContentPane().setLayout(borderLayout1);
jScrollPane1.setBounds(new Rectangle(10, 39, 180, 233));
jButton1.setBounds(new Rectangle(218, 46, 108, 30));
jButton1.setText("添加");
jButton1.addActionListener(new SimpleTable_jButton1_actionAdapter(this));
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel1.add(jScrollPane1);
jPanel1.add(jButton1);
jScrollPane1.getViewport().add(jTable1);
jPanel1.setLayout(null);
}
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JTable jTable1;
JScrollPane jScrollPane1 = new JScrollPane();
JButton jButton1 = new JButton();
public static void main(String[] args) {
SimpleTable st = new SimpleTable();
st.setVisible(true);
}
public void jButton1_actionPerformed(ActionEvent e) {
Object row[]={"朋友", "周华健", new Boolean(false)};
model.addRow(row);
model.fireTableDataChanged();
jTable1.setModel(model);
}
}
class SimpleTable_jButton1_actionAdapter implements ActionListener {
private SimpleTable adaptee;
SimpleTable_jButton1_actionAdapter(SimpleTable adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
需要注意的地方:
表格由两部份组成:分别是行标题(Column Header)与行对象(Column Object).利用JTable所提供的getTableHeader()方法取得
行标题。在这个例子中,我们将JTable放在JScrollPane中,这种做法可以将Column Header与Colmn Object完整的显示出来,因为
JScrollPane会自动取得Column Header.但如果文坛读者将上面第15行去掉并修改第16行:
f.getContentPane().add(table,BorderLayout.CENTER);
则运行结果你会发现Column Header不见了。
如果你不想用JScrollPane,要解决这个问题,你必须将程序修改如下:
JTable table=new JTable(p,n);
table.setPreferredScrollableViewportSize(new Dimension(550,30));
f.getContentPane().add(table.getTableHeader(),BorderLayout.NORTH);
f.getContentPane().add(table,BorderLayout.CENTER);
1.2 使用TableModel构造JTable表格
1.3 利用JTable显示数据库表
1.3.1 利用资源文件封装数据库连接类(使用了单例模式)
ConnManager.java
package com.jtable;
/**
*
* <p>Title: HelloWorld之JAVA基础</p>
*
* <p>Description: JAVA基础</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: HelloWorld of QYSOFT</p>
*
* @author 商延涛
* @version 1.0
*/
import java.sql.*;
import java.util.*;
public class ConnManager {
private ConnManager() {
}
public static void main(String[] args) {
ConnManager connmanager = new ConnManager();
String name = connmanager.config.getString("dbUsrName");
System.out.println("name");
}
protected static ConnManager instance;
public static ConnManager getInstanceOf() {
if (instance == null) {
return new ConnManager();
}
return instance;
}
public Connection getConnection() {
try {
Class.forName(config.getString("dbDriver"));
return java.sql.DriverManager.getConnection(config.getString(
"dbUrl"),
config.getString("dbUsrName"),
config.getString("dbPassword"));
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
private final String path = "com.jtable.dbconfig";
private ResourceBundle config = java.util.ResourceBundle.getBundle(path,
java.util.Locale.getDefault());
}
1.3.2 连接数据库的DbModel
package com.jtable;
/**
*
* <p>Title: HelloWorld之JAVA基础</p>
*
* <p>Description: JAVA基础</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: HelloWorld of QYSOFT</p>
*
* @author 商延涛
* @version 1.0
*/
import javax.swing.table.*;
import javax.swing.JTable;
import java.util.*;
import java.sql.*;
public class DbModel extends AbstractTableModel {
public DbModel(String command) throws SQLException {
this.command = command;
conn = ConnManager.getInstanceOf().getConnection();
statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = statement.executeQuery(command);
rsm = rs.getMetaData();
}
public static void main(String[] args) {
}
public int getRowCount() {
try {
rs.last();
return rs.getRow();
} catch (SQLException ex) {
}
return 0;
}
public int getColumnCount() {
try {
return rsm.getColumnCount();
} catch (SQLException ex) {
return 0;
}
}
public String getColumnName(int c)
{
try {
return rsm.getColumnName(c + 1);
} catch (SQLException ex) {
return null;
}
}
public Object getValueAt(int row, int column) {
try {
rs.absolute(row + 1);
return rs.getObject(column + 1);
} catch (SQLException ex) {
return null;
}
}
public void addRows() throws SQLException {
if (conn == null || rs == null)
return;
rows = new Vector();
while (rs.next()) {
Vector v = new Vector();
for (int i = 0; i <= rsm.getColumnCount(); i++) {
v.add(rs.getString(i + 1));
}
rows.add(v);
}
}
private Vector rows;
private String command;
private Connection conn;
private java.sql.ResultSet rs;
private java.sql.Statement statement;
private ResultSetMetaData rsm;
}