したがって、特定のボタンを押すと、ファイルは「安全に」更新されるはずです。私が持っているコードは次のとおりです。
public void writeBallot() throws IOException{
//need to make a new file
File realfile = new File("Ballots.txt");
File tempfile = new File("ballottemp.txt");
tempfile.createNewFile();
PrintWriter p = new PrintWriter(tempfile);
for ( int i = 0 ; i<theButtons.length; i ++)
p.println(indivelection.get(i) + ":" + intcounts[i]);
realfile.delete();
tempfile.renameTo(realfile);
そして、特定のボタンを押すと、とりわけ、このメソッドが実行されるはずです。ボタン用に書いたコードは次のとおりです。
if(e.getSource()==castVoteButton){
int reply = JOptionPane.showConfirmDialog(null,"Are you sure you are done voting?", "Are You Sure?", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION){
for (int j = 0; j<thePanels.length; j++){
((Ballot) thePanels[j]).buttonStatus(false);
((Ballot)thePanels[j]).checkyocolors();
}
castVoteButton.setEnabled(false);
loginButton.setEnabled(true);
for (int p = 0; p<thePanels.length; p++)
try {
((Ballot)thePanels[p]).writeBallot();
} catch (IOException e1) {
e1.printStackTrace();}
今、別のクラスでファイルの更新を書き込むメソッドをテストしましたが、動作しているように見えますが、ActionListener に明らかに必要な try、catch でラップすると、何もしないようです。
JButton が押されたときにメソッドを実装できるように、提案やヒントはありますか?