我的JTable中看不到列标题我创建了一个JPanel并将JTable添加到JPanel。
Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3"},
{ "Row2-Column1", "Row2-Column2", "Row2-Column3"} };
Object columnNames[] = { "Column One", "Column Two", "Column Three"};
JTable jTable = new JTable(rowData, columnNames);
答案 0 :(得分:53)
JTable的API声明:
“请注意,如果您希望在独立视图中使用JTable(在外部 一个JScrollPane)并希望显示标题,你可以使用它 getTableHeader()并单独显示。“
或者只是将表格添加到滚动窗格,然后将滚动窗格添加到面板...
答案 1 :(得分:15)
以下是如何在不使用JScrollPane的情况下向JTable添加标头:
int rows = 21;
int cols = 3;
JTable table = new JTable(rows, cols);
JTableHeader header = table.getTableHeader();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(header, BorderLayout.NORTH);
panel.add(table, BorderLayout.CENTER);
答案 2 :(得分:2)
您可以在代码中添加以下代码
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setPreferredSize(new Dimension(0, 0));
table.getTableHeader().setDefaultRenderer(renderer);
答案 3 :(得分:0)
带有动态数据的最佳JTable示例(工作顺利,只需将其粘贴到编辑器中并作为Java应用程序执行即可):
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class TableTest {
private JFrame frame;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TableTest window = new TableTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TableTest() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
table = new JTable();
DefaultTableModel daDefaultTableModel = new DefaultTableModel(0, 0);
String[] columnNames = new String[] {"Column Header1", "Column Header2", "Column
Header3"};
daDefaultTableModel.setColumnIdentifiers(columnNames);
//Dummy data for Table
daDefaultTableModel.addRow(new Object[] {"Test1","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test2","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test3","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test4","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test5","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test6","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test7","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test8","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test9","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test10","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test11","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test12","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test13","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test14","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test15","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test16","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test17","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test18","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test19","Test2","Test3"});
table.setModel(daDefaultTableModel);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(10, 38, 414, 212);
frame.getContentPane().add(scrollPane);
}
}