English 中文(简体)
如何实例化一个空的JTable?
原标题:How to instantiate an empty JTable?
  • 时间:2010-02-23 03:59:05
  •  标签:
  • swing
  • jtable

我正在攀登Java学习曲线,第一次需要一个JTable。我想要的是显示一个空表,所有单元格都是空的,除了列标题。然后,根据用户操作,表格被填充了字符串、整数和浮点数的混合物。

我在网上找到的所有示例都创建在实例化时就被填充的表。有没有简单的方法可以推迟填充表,但在启动时显示它?

提前感谢任何帮助。

最佳回答

创建一个具有指定行数和列数的表模型,并将其用于您的JTable中。例如:

String[] colHeadings = {"COLUMN1","COLUMN2"};
int numRows = 5 ;
DefaultTableModel model = new DefaultTableModel(numRows, colHeadings.length) ;
model.setColumnIdentifiers(colHeadings);
JTable table = new JTable(model);

然后您可以在模型上调用方法以更新值、添加行等。

问题回答

我希望这是你所期待的......。

这是我为一个旧项目所做的事情。我将所有单元格设置为null,因为我只想要一个4行4列的表格,所以在声明语句中初始化它们很容易。

jTable1.setModel(new DefaultTableModel(
   new Object [][] {
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                   }, new String [] {"Title 1", "Title 2", "Title 3", "Title 4"}
                ));

然后要更新行和/或列,请使用以下方法...

jTable1.setValueAt(Object data, int row, int column);

或者对于多行或多列,使用循环。祝你好运。





相关问题
auto abbreviating JLabel

Anyone written a JLabel (extension/util/??) that automatically abbreviates the contents depending on it s preferred/visible size? much appreciated.

how can install swing/matisse on eclipse?

i tried this link: http://downloads.myeclipseide.com/downloads/products/eworkbench/discovery-6.0/ but i m fail how is this?

Java JTextField with input hint

I would like to add a hint value to my javax.swing.JTextField. It should look like Firefox rendering of <input type="text" title="bla">. This creates an edit field with the text bla in the ...

Swing Generator

I need to develop some java gui using swing. A few years ago I did develop a bit with swing. But it was pretty exhausting, you see, back than there weren t much tools to help you. But I do believe ...

Create a ImageIcon that is the mirror of another one

I ll like to know if there is a way to create a ImageIcon that is the mirror of another ImageIcon. Searching on Google, I found how to do it by using many AWT libraries. Is there a way to do it with ...

热门标签