1
public void readCar() {
    String choice = String.valueOf(car_combo1.getSelectedItem());
    int ctr = 0;
    String filename = null;

    if (choice.equals("STATE-Description")) {
        filename = TEXT_CAR1.getText();
        ctr = 12;
    } else if (choice.equals("DISCAS-Camera")) {
        filename = TEXT_CAR2.getText();
        ctr = 3;
    } else if (choice.equals("DISCAS-CAR(for removal)")) {
        filename = TEXT_CAR3.getText();
        ctr = 11;
    } else if (choice.equals("DISCAS-PAR(for removal)")) {
        filename = TEXT_CAR4.getText();
        ctr = 9;
    } else if (choice.equals("Closing CAS for chg w/ customer initiated")) {
        filename = TEXT_CAR5.getText();
        ctr = 11;
    }

    try {
        File file = new File(filename);

        FileReader fr = new FileReader(file.getAbsoluteFile());
        BufferedReader br = new BufferedReader(fr);

        for (int i = 0; i < ctr; i++) {
            dataCar[i] = br.readLine();
            String[] temp;
            temp = dataCar[i].split("\\*");
            car_table.setValueAt(temp[0], i, 0);
            car_table.setValueAt(temp[1], i, 1);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

This is the read function

private void car_combo1ActionPerformed(java.awt.event.ActionEvent evt) {
    String choice = String.valueOf(car_combo1.getSelectedItem());
    JTableHeader th = car_table.getTableHeader();

    if (choice.equals("STATE-Description")) {
        car_table.getColumnModel().getColumn(0).setHeaderValue("STATE");
        car_table.getColumnModel().getColumn(1).setHeaderValue("Description");
        String[] change_choice_to = {"-", "1006", "1009", "1011", "1013", "1015", "1017", "1018", "1019", "1020", "1021", "1022", "1023"};

        DefaultComboBoxModel model = new DefaultComboBoxModel(change_choice_to);
        car_combo2.setModel(model);

        DefaultTableModel model2 = (DefaultTableModel) car_table.getModel();
        model1.fireTableDataChanged();
        readCar();

    }

}

table.repaint(); and tableModel.fireTableDataChanged(); doesn't work. I tried both of those function but the jtable wouldn't refresh. The notepad with more lines in it stays in the jTable when a lesser lined notepad is called.

I'm using BufferedReader and FileReader to read it from the notepad.

Is there anyway to refresh the table into its default then show the data from the notepad again? Thank you.

R. Tienzo
  • 13
  • 3
  • You speak of "show the data from the notepad again" as if we know what your program looks like or is doing. Please try to improve your question by providing a lot more detail and code so that we may better understand it. A JTable will change what it displays when you've changed the data in its model, and much of this will depend on how you've structured your model. – Hovercraft Full Of Eels Jul 02 '15 at 02:06
  • 1
    The `TableModel` is responsible for providing notifications to the table when it's contents change. Have a look at [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) for more details. Also consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jul 02 '15 at 02:07
  • 1
    *"Is there anyway to refresh the table into its default then show the data from the notepad again? Thank you."* - Read read the file into a new `TableModel` and set it to the `JTable`... – MadProgrammer Jul 02 '15 at 02:08
  • 1
    No point in calling either `repaint` or `tableModel`, you've actually not changed the `TableModel` at all, so there is no change. You seem to have two `JTable`'s but I have no idea why, your `readCar` method just sets the cell values for the existing contents of the `opsim_table`, this is kind of dangerous as you could get an index out of bounds error if you have more lines then you have rows in the table. Wrap the data directly in a new `TableModel` and then apply the model to the table when you're done loading – MadProgrammer Jul 02 '15 at 02:35
  • 1
    oops sorry about that. I'm using the same code for the same table. wait I'll edit – R. Tienzo Jul 02 '15 at 02:41

2 Answers2

1

So, the basic idea is either you want to update the existing TableModel or replace it with a new one.

Because the number rows seems to change between requests, replacing the model seems to be the safer bet, for example;

This is taken from you readCar method as it's the important part...

try (BufferedReader br = new BufferedReader(new FileReader(new File(filename)))) {

    DefaultTableModel model = new DefaultTableModel(new String[]{"STATE", "Description"}, 0);
    for (int i = 0; i < ctr; i++) {
        dataCar[i] = br.readLine();
        String[] temp = dataCar[i].split("\\*");
        model.addRow(temp);
    }
    car_table.setModel(model);
} catch (IOException e) {
    e.printStackTrace();
}

All this does is create a new DefaultTableModel, for each line of the file, adds a row to the model and then applies the model to the table when it's done.

If this fails, either there is something else in your code which isn't working or the file is been read (or saved) properly

Take a look at How to Use Tables and The try-with-resources Statement for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0
  public void eraseTableCar(){
        for(int i = 0; i<110 ; i++){
             car_table.setValueAt(" ",i,0);
             car_table.setValueAt(" ",i,1);
        }
   }



        public void readCar() {
            String choice = String.valueOf(car_combo1.getSelectedItem());
            int ctr = 0;
            String filename = null;

if (choice.equals("STATE-Description")) {
    filename = TEXT_CAR1.getText();
    ctr = 12;
} else if (choice.equals("DISCAS-Camera")) {
    filename = TEXT_CAR2.getText();
    ctr = 3;
} else if (choice.equals("DISCAS-CAR(for removal)")) {
    filename = TEXT_CAR3.getText();
    ctr = 11;
} else if (choice.equals("DISCAS-PAR(for removal)")) {
    filename = TEXT_CAR4.getText();
    ctr = 9;
} else if (choice.equals("Closing CAS for chg w/ customer initiated")) {
    filename = TEXT_CAR5.getText();
    ctr = 11;
}

eraseTableCar();

try {
    File file = new File(filename);

    FileReader fr = new FileReader(file.getAbsoluteFile());
    BufferedReader br = new BufferedReader(fr);

    for (int i = 0; i < ctr; i++) {
        dataCar[i] = br.readLine();
        String[] temp;
        temp = dataCar[i].split("\\*");
        car_table.setValueAt(temp[0], i, 0);
        car_table.setValueAt(temp[1], i, 1);
    }

} catch (IOException e) {
    e.printStackTrace();
}

}

This code works although I don't know if it's a good practice in coding

R. Tienzo
  • 13
  • 3