tags:

views:

681

answers:

1

I have 2 windows one window shown the JTable Model data, when double click the row will pop up a new window to edit the data, once submit how can I refresh the JTable?

Customer.java :

JPanel getJPanel() {
    if (jPanel == null) {

        jPanel = new JPanel();
        jPanel.setLayout(null);
        jPanel.setSize(new Dimension(792, 420));
        jPanel.add(getJScrollPane(), null);
        setUpTableData();
    }
   JScrollPane getJScrollPane() {
    if (jScrollPane == null) {
        jScrollPane = new JScrollPane();
        jScrollPane.setBounds(new Rectangle(20, 166, 759, 241));
        jScrollPane.setViewportView(getJTable());
    }
    return jScrollPane;
}

   JTable getJTable() {
    if (jTable == null) {
        jTable = new JTable();
        jTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent e) {
                new ContactEdit(name,email,phoneNo,phoneNo2,id).getJInternalFrame().setVisible(true);
                }
            }
        });
        DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
        String[] colName = {"No." ,"Name", "Email","Contact No. 1","Contact No. 2","ID"};
        tableModel.setColumnIdentifiers(colName);
        jTable.getTableHeader().setBackground(Color.WHITE);
        jTable.getTableHeader().setForeground(Color.BLUE);
        Font Tablefont = new Font("DialogInput",Font.BOLD,12); 
        jTable.getTableHeader().setFont(Tablefont);

    }
    return jTable;
}
   public void setUpTableData() {
    DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
    ArrayList<Address> list = sql.getContactLists();
    int i = 0;
    for (i = 0; i < list.size(); i++) {
        String[] bill = new String[6];
        for (int j = 0; j < 6; j += 6) {
            bill[j] = Integer.toString(i);
            bill[j + 1] = list.get(i).getName();
            bill[j + 2] = list.get(i).getEmail();
            bill[j + 3] = list.get(i).getPhoneNo();
            bill[j + 4] = list.get(i).getPhoneNo2();
            bill[j + 5] = list.get(i).getId();
        }
        tableModel.addRow(bill);
    }
    jTable.setModel(tableModel);
}

Edit.java

private JButton getJButton() {
    if (jButton == null) {
        jButton = new JButton();
        jButton.setBounds(new Rectangle(155, 138, 85, 25));
        jButton.setText("Update");
        jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                Address a = new Address(jTextField1.getText(),jTextField.getText(),jTextField3.getText(),jTextField2.getText(),"");
                                    sql.updateContact(a, Integer.parseInt(getJTextField4().getText()));                 
                            Customer c = new Customer();
                            c.setUpTableData();
                getJInternalFrame().setVisible(false);
                getJInternalFrame().dispose();
            }
        });
    }
    return jButton;
}

On JPanel Load will call this setUpTableData to retrieve the data from database. On the edit window, I have added an action listener on the update button to refresh the table, but I don't know how to make the jTable refresh the updated data? The setUpTableData on ActionListener give me Null Pointer.

+1  A: 

One solution would be to give the edit window an instance of he other JPanel. (You can accomplish that by passing the panel in to the constructor of the edit window) Then you can simply call the setUpTableData() method from inside the ActionListener.

The code to do this may look like the following:

Edit frame constructor

public EditFrame(CustomerListFrame cListFrame, ... other params) {
    this.cListFrame = cListFrame;
}

Update ActionListener

public void actionPerforment(ActionEvent e) {
    cListFrame.setUpTableData(the new data);
}
jjnguy
Hi thanks for your reply, sorry dont quite get what you mean by give an instance of e other JPanel? how to do that? Thank You
@newbie - my guess is that he means simply that when creating the other JPanel, this one should have a reference (given in parameter, for example) of the main panel, so that it would be able to call your method, from "there".
Gnoupi
So inside the constructor do I need to specify anything?
When I call the setUptableData() method inside the actionlstener it prompt me error message said NullPointer
@newbie My guess is that you aren't properly passing an instance of the JPanel into the edit frame. I am going to edit my answer to include more information
jjnguy
Hi, for ur example ur CustomerListFrame izzit the jtable window class name?
@newbie Yes. THe CustomerListFrame is your class that contains the JTable.
jjnguy
Thanks again, I tried your method on Edit Frame i create a constructor private Customer c = null; public Edit (Customer c){ this.c = c; }but on MainFrame jTable jTable.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent e) { //here i will visible the editFrame new Edit( ).getJInternalFrame().setVisible(true); how do I pass in the class itself?
`new Edit(MainFrame.this).getJInternalFrame().setVisible(true)` That will pass the current instance of `MainFrame` into the constructor for `Edit`.
jjnguy
Hi, it work thank you very much :)
@newbie You are welcome. (An upvote for my trouble maybe?, please)
jjnguy