简体   繁体   中英

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. The TableModel is what actually holds the data, and allows us to easily manipulate the data in the table.

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.

With DefaultTableModel , you can simply call the method addRow(Object[]) or addRow(Vector) to add a row dynamically at runtime. 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.

See the DefaultTableModel API for other useful methods and constructors. Add see How to Use Tables for general information on using tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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