java - how to refresh the content of the jtable if you're using notepad to putting data in the jtable -


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 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);          (int = 0; < 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 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(); , tablemodel.firetabledatachanged(); doesn't work. tried both of function jtable wouldn't refresh. notepad more lines in stays in jtable when lesser lined notepad called.

i'm using bufferedreader , filereader read notepad.

is there anyway refresh table default show data notepad again? thank you.

so, basic idea either want update existing tablemodel or replace new one.

because number rows seems change between requests, replacing model seems safer bet, example;

this taken readcar method it's important part...

try (bufferedreader br = new bufferedreader(new filereader(new file(filename)))) {      defaulttablemodel model = new defaulttablemodel(new string[]{"state", "description"}, 0);     (int = 0; < ctr; i++) {         datacar[i] = br.readline();         string[] temp = datacar[i].split("\\*");         model.addrow(temp);     }     car_table.setmodel(model); } catch (ioexception e) {     e.printstacktrace(); } 

all create new defaulttablemodel, each line of file, adds row model , applies model table when it's done.

if fails, either there else in code isn't working or file been read (or saved) properly

take @ how use tables , the try-with-resources statement more details


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -