我已经显示 Jtable 取决于数据库中的特定列,而我的 jtable 中的最后两列是图像字段。如何在不影响最后两列的情况下动态更改列。请告诉我。怎么做。
问问题
9051 次
2 回答
4
使用Vector
自java.util.Collection
请参阅http://www.roseindia.com中的此示例
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.TableColumn;
public class JTableResultSet {
public static void main(String[] args) {
Vector columnNames = new Vector();
Vector data = new Vector();
JPanel panel = new JPanel(); //
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
String sql = "Select name,address from user";
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData metaData = resultSet.getMetaData();
int columns = metaData.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(metaData.getColumnName(i));
}
while (resultSet.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(resultSet.getObject(i));
}
data.addElement(row);
}
resultSet.close();
statement.close();
} catch (Exception e) {
System.out.println(e);
}
JTable table = new JTable(data, columnNames);
TableColumn column;
for (int i = 0; i < table.getColumnCount(); i++) {
column = table.getColumnModel().getColumn(i);
column.setMaxWidth(250);
}
JScrollPane scrollPane = new JScrollPane(table); panel.add(scrollPane);
JFrame frame = new JFrame();
frame.add(panel); //adding panel to the frame
frame.setSize(600, 400); //setting frame size
frame.setVisible(true); //setting visibility true
}
}
于 2012-08-14T05:44:08.720 回答
2
TableModel.setValueAt(Objet value, int row, int col)
于 2012-08-14T05:38:41.860 回答