简体   繁体   English

向JTable添加多行

[英]Adding multiple rows to JTable

This is a pretty newbie question, but I'm having a difficult time adding multiple rows to a Jtable. 这是一个相当新手的问题,但是我很难在Jtable中添加多行。

I have a program which reads data from a file that the user has searched for, and I would like to output it all on a table. 我有一个程序可以从用户搜索的文件中读取数据,我希望将其全部输出到表上。

The problem is, whenever there is multiple items that match that search, the information doesn't come outputted all on one table, rather the popup menu comes up multiple times, each time displaying just one row on the Jtable, instead of giving me just one popup that will have all the rows in one Jtable. 问题是,只要有多个与该搜索匹配的项目,信息就不会全部输出到一张桌子上,而是弹出菜单多次出现,每次在Jtable上仅显示一行,而不仅仅是给我一个弹出窗口,它将所有行都放在一个Jtable中。

Here is the code: 这是代码:

for (int x=0;x<data.length(); ++x)
            {
                if (database[4].equalsIgnoreCase(valenceInput))                                                                                  //Search database for the number inputted by user
                {

                    Object[][] rows = {                                                                                       
                            {database[0],database[1],database[2],database[3],database[4]}               
                        };

                    Object[] cols = {
                            "Name","Symbol","Atomic Number","Atomic Mass", "# of Valence Electrons"
                        };
                    JTable table = new JTable(rows, cols);

                    JOptionPane.showMessageDialog(null, new JScrollPane(table));

                    found=true;                                                                                                                        //Booleon set to true to ensure desired output
                    break;
                }
            }

This is what the output comes up as: http://i.imgur.com/RFkRtug.png 输出结果如下: http : //i.imgur.com/RFkRtug.png

Directly after this, I will get another popup which will display another row of data that matched the search, I'm trying to figure out how to put all the rows on one Jtable instead of it showing me many each with just 1 row of data. 在此之后,我将立即弹出另一个弹出窗口,该弹出窗口将显示与搜索匹配的另一行数据,我试图找出如何将所有行放在一个Jtable上,而不是向我显示仅包含一行数据的许多行。

Build the TableModel within the for-loop and show the table once it's done... for-loop构建TableModel ,并在完成后显示该表...

DefaultTableModel model = new DefaultTableModel(
    new Object[]{"Name","Symbol","Atomic Number","Atomic Mass", "# of Valence Electrons"},
    0);
for (int x=0;x<data.length(); ++x)
{
    if (database[4].equalsIgnoreCase(valenceInput))                                                                                  //Search database for the number inputted by user
    {
        Object[] rows = {database[0],database[1],database[2],database[3],database[4]};
        model.addRow(rows);
    }
}

JTable table = new JTable(model);
JOptionPane.showMessageDialog(null, new JScrollPane(table));

See How to Use Tables for more details 有关更多详细信息,请参见如何使用表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM