9

JTable 标头未显示...

即使将其添加到 JScrollPane 之类的容器中,我的 JTable 标头也不会显示...告诉我为什么会发生以及如何修复或调试它。我通过互联网搜索,他们所说的只是将容器添加到您的 jtable,我做了,但我的标题仍然没有显示。


public void table(){

try{

        rs = stat.executeQuery("SELECT * FROM payments;");

        Vector<String> header = new Vector<String>();

        header.add("PAYMENT"); 
        header.add("AMOUNT");
        header.add("MODIFIER");
        header.add("DATE MODIFIED");

        Vector<Vector<Object>> data = new  Vector<Vector<Object>>();

        while(rs.next()) {

            Vector<Object> row = new Vector<Object>();

            row.add(rs.getString("description"));
            row.add(rs.getString("amount")); 
            row.add(rs.getString("remarks")); 
            row.add(rs.getString("date"));


            data.add(row);

        } // loop

        table = new JTable(data, header);

        JScrollPane scrollPane = new JScrollPane(table);

        panel.add(table);
        panel.add(table.getTableHeader());
        //panel.removeAll();
        //scroll.add(table);
        validate();

    }catch(Exception e){
        System.out.println("Error in table: "+e);
    }//try and catch

}
4

3 回答 3

13

从删除开始

panel.add(table.getTableHeader());

JTable旨在将其标题添加到JScrollPane. 一个组件的实例只能属于一个父/容器,上面的行是从滚动窗格中删除它

还有,改这个...

panel.add(table);

panel.add(scrollPane);
于 2013-09-28T10:39:41.720 回答
5

我遇到同样的问题你必须将 JTable 添加到 JScrollPane 然后将 JscrollPane 添加到 JFrame 例如

JTable table = new JTable();
JScrollPane scrollPane= new  JScrollPane(table);
frame.add(scrollPane);
于 2014-05-12T05:44:47.693 回答
1

具有动态数据的最佳 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);



  }
}
于 2020-04-15T03:47:16.017 回答