0

How can I "refresh" my table, so that I could display same table with different data?

String columnNames[] = {"First Name", "Last Name", "Phone Number", "E-mail"};
    JTable contactTable = new JTable(data, columnNames);
    jScrollPane = new JScrollPane(contactTable,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Garry
  • 4,493
  • 3
  • 28
  • 48
misha
  • 134
  • 1
  • 10
  • 1
    Change the `TableModel`. See [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) for more details – MadProgrammer Jul 03 '15 at 07:38
  • What is your definition of refresh? – Garry Jul 03 '15 at 07:40
  • 1
    Here is answer from @MadProgrammer... http://stackoverflow.com/a/16786120/1129313 – Garry Jul 03 '15 at 07:47
  • possible duplicate of [How to refresh data in JTable I am using TableModel](http://stackoverflow.com/questions/16785982/how-to-refresh-data-in-jtable-i-am-using-tablemodel) – Martin Frank Jul 03 '15 at 09:35

1 Answers1

0

where is your data saved ?

i save it in a text file and use this way to refresh my data but you need a button to refresh every time

here is my code

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // Refresh Button

        try {
            for (int r = 0; r < 100; r++) { //initializing row
                for (int c = 0; c < 4; c++) { //initializing column
                    jTable1.setValueAt(null, r, c);
                }
            }

            BufferedReader rdfile = new BufferedReader(new FileReader("items.txt"));

            String[] item = new String[100];
            String[] temp;

            int x = 0;  //read item
            while ((item[x] = rdfile.readLine()) != null) {
                temp = item[x].split("\t");
                jTable1.setValueAt((1000 + x + 1), x, 0);
                for (int j = 1; j < 4; j++) {
                    jTable1.setValueAt(temp[j - 1], x, j);
                }

                x++;
            }
            rdfile.close();

        } catch (IOException e) {
        }

    }
Fri3nd
  • 23
  • 3