I have created a DLM for my application to hold the contents of my jList so that i can pass those(type -> strings) to a .txt file.However, everything i have tried has not worked quite good so far.To explain my problem a little further, i am creating a small application for a logistic office which needs to be able to add clients, remove them and sort them in order alphabetically or based on the client's afm number.Also it needs to be able to save the list's clients and then when the application re-opens it must populate the list using the data inside a .txt file that gets created/updated when the user clicks on the save jMenu Button.My current problem is that although i do create the .txt file, i can only append 1 line from the model to the .txt file and if i hit save again, it justs deletes the previous data from the first line and adds 3 new strings.Basically, i want my program to check if a .txt file with the name specified exists when i hit enter and then : if not, create one and append all the current clients and their info otherwise if it does, simply append any changes made to the list(any new client's) to the .txt file.I've researched quite a bit on stackOverFlow and on Oracle's doc but didn't find a solution to my problem that's fairly simple(i consider myself a begginer in java so i am looking for the easiest possible and most efficient at the same time way to solve this).I hope you can help me out with any hint or sample or links.Thanks in advance and here are my two classes :
ListModel.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class ListModel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JList list;
private DefaultListModel model;
public FileManager fm = new FileManager();
public String input,input2,input3;
int counter;
int selectedIndex;
private ListModel() {
setLayout(new BorderLayout());
model = new DefaultListModel();
list = new JList(model);
JScrollPane pane = new JScrollPane(list);
JButton addButton = new JButton("Add Client");
JButton removeButton = new JButton("Remove Client");
// model = new DefaultListModel();
//model.addListDataListener(new ListModel());
// add the client in the list
addButton.addActionListener(new ActionListener() {
@SuppressWarnings("unchecked") // for model.addElement
public void actionPerformed(ActionEvent e) {
input = JOptionPane.showInputDialog("Insert the client's name:");
input2 = JOptionPane.showInputDialog("Insert the client's last name:");
input3 = JOptionPane.showInputDialog("Insert the client's afm:");
model.addElement(input + " " + input2 + " " + "|" + "AFM : " + input3 + "| " + "Position: " + "(" + counter + ")");
counter++;
//System.out.println(model.get(counter));
}
});
// Remove the client from the list at the selected index
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//int position = 0;
if (model.getSize() > 0)
//position = Integer.parseInt(JOptionPane.showInputDialog( "Insert the client's position in the list that you want to remove:"));
//model.removeElementAt(position);
//counter--;
JOptionPane.showMessageDialog(null, "The client that you have selected will be removed!");
selectedIndex = list.getSelectedIndex();
if(selectedIndex != -1) {
model.remove(selectedIndex);
JOptionPane.showMessageDialog(null, "Client has been successfully removed!");
} else {
JOptionPane.showMessageDialog(null, "Please choose a user to remove(if there is any).");
}
}
});
JMenuItem mntmSaveFile = new JMenuItem("Save File");
mntmSaveFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
fm.openFile();
fm.writeToFile(input, input2, input3);
fm.closeFile();
System.out.println("Listener method called!");
}
});
// Creating the window using JFrame
add(pane, BorderLayout.NORTH);
JMenuBar menuBar = new JMenuBar();
pane.setColumnHeaderView(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
mnFile.add(mntmSaveFile);
JMenuItem mntmExitApplication = new JMenuItem("Exit Application");
mnFile.add(mntmExitApplication);
JMenu mnClientOrder = new JMenu("Client Order");
menuBar.add(mnClientOrder);
JMenuItem mntmAlphabetically = new JMenuItem("Alphabetically");
mnClientOrder.add(mntmAlphabetically);
JMenuItem mntmAfmBased = new JMenuItem("AFM based");
mnClientOrder.add(mntmAfmBased);
add(addButton, BorderLayout.WEST);
add(removeButton, BorderLayout.EAST);
//System.out.println(dlm.capacity());
}
public static void main(String[] args) {
JFrame frame = new JFrame("LOGISTIKO GRAFIO");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new ListModel());
frame.setSize(400, 230);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
FileManager.java
import java.io.File;
import java.util.Formatter;
import javax.swing.JOptionPane;
public class FileManager {
File file = new File("ListArchive.txt");
private Formatter x;
public void openFile() {
try {
x = new Formatter("C:/Users/Stelios Papamichael/Desktop /Java/Runnable_Programs/LogisticOfficeApplication/ListArchive.txt");
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "File not saved!");
}
}
public void writeToFile(String name, String lastName , String afm) {
x.format("%s %s %s\n", name , lastName , afm );
JOptionPane.showMessageDialog(null, "File saved!");
}
public void closeFile() {
x.close();
}
}