如何在滚动窗格中设置表格以及如何显示表格标题

时间:2015-06-02 05:45:51

标签: java swing jtable

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class StatusPage extends JPanel
{
    DefaultTableModel dm;
    JTable table;
    JScrollPane jsb;

    public StatusPage()
    {
        String col_name[] = {"S.No.","Book Name","Author Name","Available Book","Issue Book","Total Book"};
        String data[][] = {{"1","Vikash","All","A","All","A"},{"1","Vikash","All","A","All","A"},{"1","Vikash","All","A","All","A"},{"1","Vikash","All","A","All","A"},{"1","Vikash","All","A","All","A"},{"1","Vikash","All","A","All","A"},{"1","Vikash","All","A","All","A"}};

        dm = new DefaultTableModel(data,col_name);
        table = new JTable(dm);
        jsb = new JScrollPane(table);
        add(table);
    }
}

请检查错误。表头不显示。如何设置table的大小。我使用Google但代码无法正常工作

1 个答案:

答案 0 :(得分:3)

要显示表格标题,您应在面板或框架中添加JScrollPane而不是table

所以你应该这样做:

add(new JScrollPane(table));