更新gui中的Jtable内容而不重新引用重绘表的方法?

时间:2014-10-12 17:27:26

标签: java swing user-interface jtable httpurlconnection

我在此提供的方法应该创建一个新表,执行调用,然后使用联系人收到的消息更新表。除收到的消息外,一切正常。我甚至可以再次单击该按钮启动调用,然后更新表以包含收到的消息,但我添加到代码中的任何内容似乎都不会自动更新表。正如你在这里看到的那样,我甚至试图完全重新绘制表格,但这也不起作用。实际上使消息显示的唯一方法是重新启动召回。

我尝试重新验证并重新粉刷,但那些似乎不起作用。

public void doRecall() throws Exception {

    newFrame = new JFrame("Recall Initiated Awaiting Replies");
    newFrame.setSize(600, 300);
    newFrame.setIconImage(img);

    Member con = new Member();

    String columnNames[] = { "First Name", "Last Name", "Phone No.",
            "Response" };

    Object[][] data = new Object[v.size()][4];

    for (int j = 0; j < v.size(); j++) {

        con = (Member) v.elementAt(k);

        data[j][0] = con.getFName();
        data[j][1] = con.getLName();
        data[j][2] = con.getPhoneNo();
        data[j][3] = con.getResponse();

        k++;

    }
    k = 0;

    abtable = new JTable(data, columnNames);

    JScrollPane scrollPane = new JScrollPane(abtable);
    abtable.setPreferredScrollableViewportSize(new Dimension(500, 370));

    JPanel pane = new JPanel();
    JLabel label = new JLabel("Members Currently In The Recall Roster");
    pane.add(label);

    newFrame.getContentPane().add(pane, BorderLayout.SOUTH);
    newFrame.getContentPane().add(scrollPane, BorderLayout.CENTER);

    newFrame.setLocation(screenWidth / 4, screenHeight / 4);
    newFrame.setVisible(true);

    String customMessage = getMessage();

    k = 0;

    for (int j = 0; j < v.size(); j++) {

        con = (Member) v.elementAt(k);

        try {
            String toPhoneNumber = con.getPhoneNo();
            customMessage = customMessage.replaceAll("\\s+", "+");
            System.out.println(customMessage);

            String requestUrl = ("http://cloud.fowiz.com/api/message_http_api.php?username=&phonenumber=+"
                    + toPhoneNumber + "&message=" + customMessage + "&passcode=");

            URL url = new URL(requestUrl);

            HttpURLConnection uc = (HttpURLConnection) url.openConnection();
            System.out.println(uc.getResponseMessage());
            String reply = uc.getResponseMessage();

            if (reply.equalsIgnoreCase("ok")) {

            }

        } catch (Exception ex) {
            System.out.println(ex.getMessage());

        }
        k++;

    }
    k = 0;
    for (int j = 0; j < v.size(); j++) {

        con = (Member) v.elementAt(k);

        boolean phoneCheck = false;
        while (phoneCheck != true) {
            for (int j1 = 0; j1 < v.size(); j1++) {
                con = (Member) v.elementAt(k);

                mR2 = new MailReader();
                String host = "pop.gmail.com";// change accordingly
                String mailStoreType = "pop3";
                String username = "";// change
                                                            // accordingly
                String password = "";// change accordingly
                MailReader.check(host, mailStoreType, username, password);
                if (MailReader.searchForPhone(con.getPhoneNo()) == true) {
                    con.setResponse(MailReader.getReply(con.getPhoneNo()));

                    newFrame.addNotify();
                    System.out.println("IT WORKED");

                    newFrame.remove(scrollPane);
                    JTable abctable = new JTable(data, columnNames);
                    JScrollPane scrollPane2 = new JScrollPane(abctable);
                    newFrame.getContentPane().add(scrollPane2,
                            BorderLayout.CENTER);
                    abtable.repaint();
                    newFrame.repaint();
                    newFrame.revalidate();

                    phoneCheck = true;
                }
            }
        }

    }

}

1 个答案:

答案 0 :(得分:2)

当与表的数据交互时,我们想要使用表的TableModelTableModel是实际保存数据的内容,并允许我们轻松操作表中的数据。

您可以实现自己的AbstractTableModel或使用已提供实施的DefaultTableModel,其中包含添加行和其他好东西的方法。

使用DefaultTableModel,您只需调用方法addRow(Object[])addRow(Vector)即可在运行时动态添加行。例如:

String columnNames[] = { 
        "First Name", "Last Name", "Phone No.", "Response" };
DefaultTableModel model = new DefaultTableModel(columnNames, 0):
JTable table = new JTable(model);
...
// dynamically add a row
Object[] row = { data1, data2, data2, data4 };
model.addRow(row);

DefaultTableModel实现将触发必要的更新以调用重新绘制表格。

有关其他有用的方法和构造函数,请参阅DefaultTableModel API。有关使用表格的一般信息,请参阅How to Use Tables