自分のアプリケーションのDLMを作成して、それらの(型 - >文字列)を.txtファイルに渡すことができるようにしました。私が試したことはそれほどうまくいきませんでした。私の問題をもう少し詳しく説明するために、クライアントを追加して削除し、アルファベット順にソートするか、ソート順にソートできるロジスティックオフィス用の小さなアプリケーションを作っています。クライアントのafm番号。また、リストのクライアントを保存する必要があります。アプリケーションが再び開くと、ユーザーがjMenuをクリックしたときに作成または更新される.txtファイル内のデータを使用してリストを作成する必要があります私の現在の問題は、.txtファイルを作成しますが、モデルから.txtファイルに1行しか追加できません。また、再度保存すると、最初の行の前のデータが削除され、3行追加されます新しいストライクngs.Basically、私は私のプログラムが、指定された名前の.txtファイルが存在するかどうかを確認するために、私がenterを押した後、もしそうでなければ、現在のクライアントとその情報をすべて追加する。そうでなければ、私は、stackOverFlowとOracleの文書でかなりのことを研究しましたが、私の問題の解決策は見つけられませんでした。これはかなり単純です(自分自身をJavaの初心者だと考えています。私はこれを解決するために同時に最も簡単で最も効率的な方法を探しています)。ヒントやサンプルやリンクで私を助けてくれることを願っています。DefaultListModelのデータを.txtファイルに追加する方法
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();
}
}
http://stackoverflow.com/help/someone-answersを参照してください。 – c0der