gpt4 book ai didi

java - 无法显示 jTable 的标题

转载 作者:行者123 更新时间:2023-11-29 05:50:43 25 4
gpt4 key购买 nike

这些是我在 java swing 中为我的 jTable 编写的代码。

private JTable getJTable() {
if (jTable == null) {
Vector columnNames = new Vector(); //Vector class allows dynamic array of objects
Vector data = new Vector();
JPanel panel = new JPanel();
panel.setSize(new Dimension(198, 106));

try {
DBController db = new DBController();
db.setUp("IT Innovation Project");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
String dsn = "IT Innovation Project";
String s = "jdbc:odbc:" + dsn;
Connection con = DriverManager.getConnection(s, "", "");
String sql = "Select * from forumTopics";
java.sql.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();
((Connection) statement).close();
} catch (Exception e) {
System.out.println(e);
}

jTable = new JTable(data, columnNames);
TableColumn column;

for (int i = 0; i < jTable.getColumnCount(); i++) {
column = jTable.getColumnModel().getColumn(i);
column.setMaxWidth(250);
}

String header[] = {"description", "title", "category", "posted by"};

for(int i=0;i<jTable.getColumnCount();i++) {
TableColumn column1 = jTable.getTableHeader().getColumnModel().getColumn(i);
column1.setHeaderValue(header[i]);
}

jTable.setBounds(new Rectangle(82, 218, 736, 292));
jTable.setEnabled(false);
jTable.setRowHeight(50);
jTable.getRowHeight();
}
return jTable;
}

我为表格的标题设置了一个数组。但是,该程序只显示我的数据库中的数据,没有任何标题。有人可以启发我吗?提前致谢。

最佳答案

把你的JTable放在JScrollPane

add(new JScrollPane( getJTable() ) );

关于java - 无法显示 jTable 的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13983780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com