我遇到了 JTable 列的问题。我创建了 JTable,但列不会显示。是的,我尝试了之前提出的关于添加 JScrollPane 的问题,但将 ScrollPane 放入其中完全破坏了我的表,并且该表不可见。
我从这个链接 ( JTable won't show column headers ) 尝试了 frame.getContentPane.add(new JScrollPane(table))
但没有像我上面说的那样有任何效果。
我没有使用布局管理器。
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=12345");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM consoles INNER JOIN hardware ON consoles.id=hardware.id");
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
String[] cols = new String[columnCount];
for (i=1;i<= columnCount;i++)
{
cols[i-1] = md.getColumnName(i);
}
DefaultTableModel model = new DefaultTableModel(cols,0);
while (rs.next())
{
Object[] row = new Object[columnCount];
for (i = 1 ; i <= columnCount ; i++)
{
row[i-1] = rs.getObject(i);
}
model.addRow(row);
}
JTable table = new JTable(model);
model.fireTableDataChanged();
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(true);
table.setFillsViewportHeight(true);
table.setSurrendersFocusOnKeystroke(true);
table.setBounds(146,59,763,360);
frame.getContentPane().add((table));
model.fireTableDataChanged();
}
最佳答案
JTable
旨在与 JScrollPane
配合使用,它会自动将 TableHeader
添加到滚动 Pane 中,例如
代替
frame.getContentPane().add((table));
尝试使用...
frame.getContentPane().add(new JScrollPane(table));
参见 How to Use Tables了解更多详情
注意,您应该避免使用 setBounds
而是使用适当的布局管理器,您还应该避免使用 null
布局,像素完美布局是现代 ui 设计中的一种错觉.影响组件单个尺寸的因素太多,没有一个是您可以控制的。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正
已更新...
您永远不需要调用模型上的 fireTableDataChanged
或任何其他事件方法,它们不是供您使用,而是供在模型中使用。
在将表格/滚动 Pane 添加到内容 Pane 之前,试试这个...
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(new JScrollPane(table));
让我演示一下......
使用布局管理器...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class TableExample {
public static void main(String[] args) {
new TableExample();
}
public TableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
int columnCount = 10;
String[] cols = new String[columnCount];
for (int i = 1; i <= columnCount; i++) {
cols[i - 1] = Integer.toString(i);
}
DefaultTableModel model = new DefaultTableModel(cols, 0);
JTable table = new JTable(model);
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(true);
table.setFillsViewportHeight(true);
table.setSurrendersFocusOnKeystroke(true);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
没有布局管理器...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class TableExample {
public static void main(String[] args) {
new TableExample();
}
public TableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
int columnCount = 10;
String[] cols = new String[columnCount];
for (int i = 1; i <= columnCount; i++) {
cols[i - 1] = Integer.toString(i);
}
DefaultTableModel model = new DefaultTableModel(cols, 0);
JTable table = new JTable(model);
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(true);
table.setFillsViewportHeight(true);
table.setSurrendersFocusOnKeystroke(true);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
问题不在于您向我们展示的代码,问题在于您选择取消构建 Swing API 的核心概念之一......布局管理器
关于java - JTable 列不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25155244/