0

我正在尝试显示表格,在 for 循环中动态填充行数据并显示,单击按钮即可。这是我的代码,有人可以帮助我吗?

public class GetQuotes {

    private JTextField wtField = new JTextField(10);
    private JTextField htField = new JTextField(10);
    private JTextField lnField = new JTextField(10);
    private JTextField wiField = new JTextField(10);
    final JPanel gui = new JPanel(new BorderLayout(2,3));
    JPanel labelFields = new JPanel(new BorderLayout(2,2));
    JTable table;
    JPanel guiCenter = new JPanel(new BorderLayout(2,3));
    //default constructor
    public GetQuotes(){

    labelFields.setBorder(new TitledBorder("Enter Inputs"));

    JPanel labels = new JPanel(new GridLayout(0,1,1,1));
    //labels.setBorder(new TitledBorder("GridLayout"));
    JPanel fields = new JPanel(new GridLayout(0,1,1,1));

     labels.add(new JLabel("Weight"));
     labels.add(new JLabel("Length"));
     labels.add(new JLabel("Width"));
     labels.add(new JLabel("Height"));

     fields.add(wtField);
     fields.add(lnField);
     fields.add(wiField);
     fields.add(htField);

      labelFields.add(labels,BorderLayout.CENTER);
      labelFields.add(fields,BorderLayout.EAST);


     JPanel buttonConstrsint = new JPanel(new FlowLayout(FlowLayout.CENTER));
     JButton getQuotesButton = new JButton("Get Quotes..");

      getQuotesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                   Quote[] data = getData();

                    table = new JTable(generateTableModel(data));
                    table.setModel(generateTableModel(data));

                    JScrollPane pane = new JScrollPane(table);
                    guiCenter.add(pane, BorderLayout.SOUTH);

                    gui.add(guiCenter,BorderLayout.CENTER);


               }

            }
        });

    buttonConstrsint.add(getQuotesButton);
    guiCenter.add(buttonConstrsint,BorderLayout.NORTH);

    gui.add(labelFields,BorderLayout.NORTH);
    gui.add(guiCenter,BorderLayout.CENTER);

    JOptionPane.showMessageDialog(null, gui);


    }

    private Quote[] getData(){

        //this function returns the needed data

    }

    //test the class
    public static void main(String[]args) throws Exception{
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new GetQuotes();
            }
        });

        }

    public static TableModel generateTableModel(Quote[] dataa) {

        String[][] data = new String[100][2] ;
          String[] colNames = {"col1","col2"};

        //here how to add another column data  dataa[j].getPrice 
          for(int i=0; i<colNames.length; i++){
          for(int j=0; j<dataa.length; j++){
              data[j][i] = dataa[j].getServiceName();

          }
          }

        return new DefaultTableModel(data, colNames);
    }
}

1) 我需要帮助在 generateTableModel 函数的 for 循环中填充 rowData。目前我可以填写单列数据,如何构建第二列数据。

2)一旦表在这里构造并返回到上面的actionperformed函数,我将表添加到jscrollpanel和guicenterpanel。但不知何故,这并没有显示表格。

有人可以帮帮我吗?

4

1 回答 1

2

部分答案:

table = new JTable(generateTableModel(data));
// table.setModel(generateTableModel(data));  // *** this is redundant

JScrollPane pane = new JScrollPane(table);
guiCenter.add(pane, BorderLayout.SOUTH);

gui.add(guiCenter,BorderLayout.CENTER);

// don't forget to revalidate and repaint your container:
gui.revalidate();
gui.repaint();

摆脱冗余的代码行,添加或删除组件后不要忘记重新验证和重新绘制容器。


关于:

1) 我需要帮助在 generateTableModel 函数的 for 循环中填充 rowData。目前我可以填写单列数据,如何构建第二列数据。

我不确定我们是否有足够的信息来回答这个问题。您当前的代码是做什么的?它会创建一个两列 JTable 吗?里面装的是什么?你调试过你的代码吗?

于 2013-10-12T14:01:48.590 回答