简体   繁体   English

在不重新启动重绘表的方法的情况下更新gui中的Jtable内容?

[英]Update Jtable content in the gui without reinitating the method to redraw the table?

The method I've provided here is supposed to create a new table, perform the recall, and then update the table with the message that is received by the person contacted. 我在这里提供的方法应该创建一个新表,执行重新调用,然后使用联系人员收到的消息来更新该表。 It all works fine, except for the received message. 除了收到的消息外,一切正常。 I can even click the button again that initiates the recall and it then updates the table to include the message that was received, but nothing I add to the code seems to update the table automatically. 我什至可以再次单击按钮来启动召回,然后它会更新表以包括收到的消息,但是我添加到代码中的任何内容似乎都不会自动更新表。 As you can see here I've even tried completely redrawing the table and that doesn't work either. 正如您在这里看到的,我什至尝试完全重绘表格,但是这也不起作用。 The only thing that actually makes the message show up is by re initiating the recall. 真正使消息显示出来的唯一方法是重新启动召回。

I've tried re validate and repaint but those don't seem to work. 我尝试过重新验证和重新粉刷,但是这些似乎不起作用。

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;
                }
            }
        }

    }

}

When interacting with the data of the table, we want to use the TableModel of the table. 与表的数据进行交互时,我们要使用表的TableModel The TableModel is what actually holds the data, and allows us to easily manipulate the data in the table. TableModel实际上是保存数据的工具,它使我们能够轻松地处理表中的数据。

You can either implement your own AbstractTableModel or use the DefaultTableModel , which already provides an implementation, that comes equipped with method for adding row and other goodies. 您既可以实现自己的AbstractTableModel也可以使用DefaultTableModel ,后者已经提供了实现,并带有用于添加行和其他东西的方法。

With DefaultTableModel , you can simply call the method addRow(Object[]) or addRow(Vector) to add a row dynamically at runtime. 使用DefaultTableModel ,您可以简单地调用方法addRow(Object[])addRow(Vector)在运行时动态添加行。 For example: 例如:

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);

The DefaultTableModel implementation will fire the necessary updates to call for a repainting of the table. DefaultTableModel实现将触发必要的更新以调用表的重新绘制。

See the DefaultTableModel API for other useful methods and constructors. 有关其他有用的方法和构造函数 ,请参见DefaultTableModel API Add see How to Use Tables for general information on using tables. 添加有关使用表的一般信息,请参见如何使用表。

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

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