Java JTable 不会显示列标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2320812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:06:40  来源:igfitidea点击:

JTable won't show column headers

javaswingjtable

提问by John R Doner

I have the following code to instantiate a JTable: the table comes up with the right number of rows and columns, but there is no sign of the titles atop the columns.

我有以下代码来实例化 JTable:该表提供了正确的行数和列数,但列顶部没有标题的迹象。

public Panel1()
{
    int  nmbrRows;

    setLayout(null);
    setBackground(Color.magenta);
    Vector colHdrs;

    //create column headers

    colHdrs = new Vector(10);
    colHdrs.addElement(new String("Ticker"));

    // more statements like the above to establish all col. titles       

    nmbrRows = 25;
    DefaultTableModel tblModel = new DefaultTableModel(nmbrRows, colHdrs.size());
    tblModel.setColumnIdentifiers(colHdrs);

    scrTbl = new JTable(tblModel);
    scrTbl.setBounds(25, 50, 950, 600);
    scrTbl.setBackground(Color.gray);
    scrTbl.setRowHeight(23);    
    add(scrTbl);

//rest of constructor
...

}

Comparing this to other table-making code, I don't see any missing steps, but something must be absent.

将此与其他制表代码进行比较,我没有看到任何缺失的步骤,但必须缺少某些内容。

采纳答案by Erkan Haspulat

Put your JTableinside a JScrollPane. Try this:

把你的JTable里面一个JScrollPane. 尝试这个:

add(new JScrollPane(scrTbl));

回答by silver

The main difference between this answer and the accepted answer is the use of setViewportView()instead of add().

此答案与接受的答案之间的主要区别是使用setViewportView()代替add()

How to put JTablein JScrollPaneusing Eclipse IDE:

如何把JTableJScrollPane使用Eclipse IDE:

  1. Create JScrollPanecontainer via Design tab.
  2. Stretch JScrollPaneto desired size (applies to Absolute Layout).
  3. Drag and drop JTablecomponent on top of JScrollPane(Viewport area).
  1. JScrollPane通过设计选项卡创建容器。
  2. 拉伸JScrollPane到所需大小(适用于绝对布局)。
  3. JTable组件拖放到JScrollPane(视口区域)的顶部。

In Structure > Components, tableshould be a child of scrollPane. enter image description here

在 Structure > Components 中,table应该是scrollPane. 在此处输入图片说明

The generated code would be something like this:

生成的代码将是这样的:

JScrollPane scrollPane = new JScrollPane();
...

JTable table = new JTable();
scrollPane.setViewportView(table);

回答by Tobias Sj?beck

As said in previous answers the 'normal' way is to add it to a JScrollPane, but sometimes you don't want it to scroll (don't ask me when:)). Then you can add the TableHeader yourself. Like this:

正如之前的答案中所说,“正常”方法是将它添加到 JScrollPane,但有时您不希望它滚动(不要问我什么时候:))。然后你可以自己添加TableHeader。像这样:

JPanel tablePanel = new JPanel(new BorderLayout());
JTable table = new JTable();
tablePanel.add(table, BorderLayout.CENTER);
tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);

回答by chaithra_a

    public table2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 485, 218);
    setTitle("jtable");
    getContentPane().setLayout(null);

    String data[][] = { { "Row1/1", "Row1/2", "Row1/3" },
            { "Row2/1", "Row2/2", "Row2/3" },
            { "Row3/1", "Row3/2", "Row3/3" },
            { "Row4/1", "Row4/2", "Row4/3" }, };

    String header[] = { "Column 1", "Column 2", "Column 3" };

    // Table
    JTable table = new JTable(data,header);


    // ScrollPane
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBounds(36, 37, 407, 79);
    getContentPane().add(scrollPane);

   }

}

}

try this!!

尝试这个!!