Joh*_*ner 76 java swing jtable
我有以下代码来实例化一个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
...
}
Run Code Online (Sandbox Code Playgroud)
将此与其他制表代码进行比较,我没有看到任何缺失的步骤,但必须缺少某些内容.
Erk*_*lat 167
把你的JTable
内心JScrollPane
.试试这个:
add(new JScrollPane(scrTbl));
Run Code Online (Sandbox Code Playgroud)
sil*_*ver 23
这个答案和接受的答案之间的主要区别在于使用setViewportView()
而不是add()
.
如何把JTable
在JScrollPane
使用Eclipse IDE:
JScrollPane
通过设计选项卡创建容器.JScrollPane
到所需的大小(适用于绝对布局).JTable
组件拖放到JScrollPane
(视口区域)的顶部.在Structure> Components中,table
应该是一个孩子scrollPane
.
生成的代码将是这样的:
JScrollPane scrollPane = new JScrollPane();
...
JTable table = new JTable();
scrollPane.setViewportView(table);
Run Code Online (Sandbox Code Playgroud)
小智 8
如前面的答案所述,'正常'方式是将它添加到JScrollPane,但有时你不希望它滚动(不要问我什么时候:)).然后您可以自己添加TableHeader.像这样:
JPanel tablePanel = new JPanel(new BorderLayout());
JTable table = new JTable();
tablePanel.add(table, BorderLayout.CENTER);
tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
103035 次 |
最近记录: |