bruh bruh - 1 month ago 15
Java Question

jtable won't update while the program is running

okay i already posted this but i delete it because no one is answering for 24 hours already

i don't really know what to do anymore. i have so many tables in my system and they all have the same codes but this is the only on that doesn't work. my problem is that every time i add candidate/data to the jtable, it won't appear. it will only appear if i terminate the program then run it again. here is my codes:

This is the class that contains storing to file and jtable methods

public class DatabaseForCandidates {
String pres;
int resPres;
static Vector rowsPres;
static String [] columnPres={"PRESIDENTIAL CANDIDATES", "CURRENT NUMBER OF VOTES", ""};
static File filePres;
static FileWriter fileWrite;
static FileReader fileRead;
static Scanner read;

public DatabaseForCandidates() {
filePres=new File("President.txt");
fileWrite=null;
fileRead=null;
read=null;
}
public void setColumns(){
PresidentTable.tblModel=new DefaultTableModel();
PresidentTable.tblModel.setColumnIdentifiers(columnPres);
PresidentTable.tblNatPresident=new JTable(PresidentTable.tblModel);
}

public void storePresidentRecords(){
try {
fileWrite=new FileWriter(filePres,true);
getDataPres();
storeToTable();

//storing to a file
fileWrite.write("**"+pres+"**"); fileWrite.write(resPres+"**");
fileWrite.write("\r\n");

fileWrite.close();
JOptionPane.showMessageDialog(null, "Candidate is now nominated for President");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error: "+e.getMessage());
}
}

public void getDataPres(){
String linePres="";
try {
fileRead=new FileReader(Database.fileNew);
read=new Scanner(fileRead);

while(read.hasNext()){
linePres+=read.nextLine()+"\n";
}
read.close();

String [] infoPres=linePres.split("/");

pres=infoPres[4]+" "+infoPres[3];
resPres=0;
}catch(Exception e){}
}

public void storeToTable(){
rowsPres=new Vector();
rowsPres.add(pres); rowsPres.add(resPres);
PresidentTable.tblModel.addRow(rowsPres);
}

public void retrievePresidentRecords(){
String holdStr="";
try {
fileRead=new FileReader(filePres);
read=new Scanner(fileRead);

while(read.hasNext()){
holdStr+=read.nextLine()+"\n";
}
read.close();

StringTokenizer strToken=new StringTokenizer(holdStr, "**");

while(strToken.hasMoreElements()){
rowsPres=new Vector();
for(int i=0; i<columnPres.length; i++){
rowsPres.add(strToken.nextElement());
}
PresidentTable.tblModel.addRow(rowsPres);
}

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


this is the class of jtable

public class PresidentTable extends JPanel{
static JTable tblNatPresident;
static DefaultTableModel tblModel;
DatabaseForCandidates data;

PresidentTable(){
setName("panelNatPres");

data=new DatabaseForCandidates();

data.setColumns();
data.retrievePresidentRecords();

PresidentTable.tblNatPresident=new JTable(PresidentTable.tblModel);
add(new JScrollPane(PresidentTable.tblNatPresident));
}
}


this is the listener of the button if you will add candidate or not

if(e.getSource().equals(PresidentPanel.btnPresAdd)){
DatabaseForCandidates data=new DatabaseForCandidates();
boolean found=false;
try {
String add=PresidentPanel.txtVNum.getText();
String vnum=null;
for (int row=0; (row<ViewTablePanel.tblModel.getRowCount()) && (!found);row++) {
vnum=ViewTablePanel.tblModel.getValueAt(row, 1).toString();
if(vnum.equals(add)){
found=true;
data.storePresidentRecords();
}
}
if(!found){
JOptionPane.showMessageDialog(null, add+" is not found.");
}
} catch (Exception e1) {}
}


just ask me if you don't understand my code and if you have any clarification. please help thank you so much :)

Answer

Without you posting the whole code it's hard to debug. However a few things that could be wrong come to mind:

  1. You don't notify the JTable that the data has changed by calling: .fireTableDataChanged()
  2. You don't update the frame with: .repaint(); and .revalidate();
  3. You update your file, but do not read the file updates or otherwise update the change to your JTable.