I cant figure out how to save my guis input so when I close it and reopen it the
ID: 3534456 • Letter: I
Question
I cant figure out how to save my guis input so when I close it and reopen it the information I changed or added is in the gui thanks for the help.
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
public class PartManager implements ActionListener{
JFrame window = new JFrame();
Inventory partsList;
JButton delete = new JButton("Delete");
JButton add = new JButton("Add");
JList displaybox;
DefaultListModel listnum;
String[] list;
JButton edit = new JButton("Edit");
public PartManager(){
partsList = new Inventory();
Parts t1 = new Parts("POS");
Parts t2 = new Parts("POS 2");
Parts t3 = new Parts("POS 3");
partsList.add(t1);
partsList.add(t2);
partsList.add(t3);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500,400);
window.setTitle("Parts");
window.setLayout(null);
listnum = new DefaultListModel();
list = new String[partsList.getSize()];
for (int n = 0; n < partsList.getSize() ; n++){
Parts c = partsList.getPart(n);
list[n] = c.getName();
listnum.addElement(list[n]);
}
displaybox = new JList(listnum);
JScrollPane displayscroll = new JScrollPane(displaybox);
displayscroll.setLocation(10,50);
displayscroll.setSize(200, 100);
window.add(displayscroll);
delete.setSize(100,25);
delete.setLocation(225,80);
delete.addActionListener(this);
window.add(delete);
add.setSize(100,25);
add.setLocation(225,110);
add.addActionListener(this);
window.add(add);
edit.setSize(100,25);
edit.setLocation(225,50);
edit.addActionListener(this);
window.add(edit);
window.setVisible(true);
}
public static void main(String[] args){
PartManager temp = new PartManager();
}
public void addToList(Parts tempPart){
partsList.add(tempPart);
listnum.addElement(tempPart.getName());
}
public void removeFromList(Parts p, int n){
partsList.remove(p);
listnum.remove(n);
displaybox.setSelectedIndex(n);
displaybox.ensureIndexIsVisible(n);
}
public void resetDisplayBox(Parts tempPart, int index){
listnum.setElementAt(tempPart.getName(), index);
displaybox.setSelectedIndex(index);
displaybox.ensureIndexIsVisible(index);
}
public void actionPerformed(ActionEvent e) {
int index = displaybox.getSelectedIndex();
if(e.getSource() == add){
Parts newPart = new Parts("");
index = listnum.getSize();
PartEditor tempWindow = new PartEditor(true, newPart, this, index);
}
else if (e.getSource() == edit){
if(index>=0){
Parts f = partsList.getPart(index);
PartEditor editwindow = new PartEditor(false,f, this, index);
}
}else if(e.getSource()==delete){
if(index>=0){
Parts f = partsList.getPart(index);
this.removeFromList(f, index);
}
}
}
}
import java.util.ArrayList;
public class Inventory {
ArrayList<Parts> partList = new ArrayList<Parts>();
public void add(Parts p){
partList.add(p);
}
public void remove(Parts p){
partList.remove(p);
}
public int getSize(){
return partList.size();
}
public Parts getPart(int i){
return partList.get(i);
}
public boolean doesContain(Parts p){
boolean temp = partList.contains(p);
return temp;
}
}
public class Parts {
public String name;
public Parts(String temp){
name=temp;
}
public String getName(){
return name;
}
public void setName(String newName){
name=newName;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class PartEditor implements ActionListener {
private JTextField name;
private Parts part;
private boolean isNew;
JFrame editorWindow;
private int index;
PartManager mainWindow;
public PartEditor(boolean isNewTemp, Parts part, PartManager temp, int tempIndex){
this.part = part;
this.mainWindow = temp;
isNew=isNewTemp;
index=tempIndex;
JDialog hi = new JDialog(editorWindow, "Hi", true);
editorWindow = new JFrame();
editorWindow.setSize(300,500);
editorWindow.setTitle("Contact Editor");
editorWindow.setLayout(null);
JLabel fnlabel = new JLabel("First Name");
fnlabel.setSize(150, 10);
fnlabel.setLocation(10,15);
editorWindow.add(fnlabel);
name = new JTextField();
name.setText(part.getName());
name.setSize(150, 25);
name.setLocation(100, 295);
editorWindow.add(name);
JButton apply = new JButton("Apply!");
apply.setSize(150, 25);
apply.setLocation(50,425);
apply.addActionListener(this);
editorWindow.add(apply);
editorWindow.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
part.setName(name.getText());
if(isNew==true){
mainWindow.addToList(part);
}else {
mainWindow.resetDisplayBox(part, index);
}
editorWindow.dispose();
}
}
Explanation / Answer
Have made necessary changes to PartManager.java, please rate :) :
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class PartManager implements ActionListener{
JFrame window = new JFrame();
Inventory partsList;
JButton delete = new JButton("Delete");
JButton add = new JButton("Add");
JButton save= new JButton("Save");
JList displaybox;
DefaultListModel listnum;
String[] list;
JButton edit = new JButton("Edit");
String path="e:/list.txt";
public PartManager(){
partsList = new Inventory();
try {
ArrayList<String> ar=readFile(path);
for(String e:ar)
{
Parts t = new Parts(e);
partsList.add(t);
}
} catch (IOException e)
{
System.out.println("File not found, creating one.....");
}
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500,400);
window.setTitle("Parts");
window.setLayout(null);
listnum = new DefaultListModel();
list = new String[partsList.getSize()];
for (int n = 0; n < partsList.getSize() ; n++){
Parts c = partsList.getPart(n);
list[n] = c.getName();
listnum.addElement(list[n]);
}
displaybox = new JList(listnum);
JScrollPane displayscroll = new JScrollPane(displaybox);
displayscroll.setLocation(10,50);
displayscroll.setSize(200, 100);
window.add(displayscroll);
delete.setSize(100,25);
delete.setLocation(225,80);
delete.addActionListener(this);
window.add(delete);
add.setSize(100,25);
add.setLocation(225,110);
add.addActionListener(this);
window.add(add);
edit.setSize(100,25);
edit.setLocation(225,50);
edit.addActionListener(this);
window.add(edit);
save.setSize(100,25);
save.setLocation(225,150);
save.addActionListener(this);
window.add(save);
window.setVisible(true);
}
public ArrayList<String> readFile(String path) throws IOException
{
BufferedReader br=new BufferedReader(new FileReader(new File(path)));
String line="";
ArrayList<String> ar=new ArrayList<String>();
while((line=br.readLine())!=null)
{
ar.add(line);
}
return ar;
}
public void writeToFile(ArrayList<Parts> partList, String path) throws IOException
{
File file = new File(path);
if (file.exists())
{
file.delete();
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for(Parts e:partList)
{
bw.write(e.getName());
bw.newLine();
}
bw.close();
}
public static void main(String[] args){
PartManager temp = new PartManager();
}
public void addToList(Parts tempPart){
partsList.add(tempPart);
listnum.addElement(tempPart.getName());
}
public void removeFromList(Parts p, int n){
partsList.remove(p);
listnum.remove(n);
displaybox.setSelectedIndex(n);
displaybox.ensureIndexIsVisible(n);
}
public void resetDisplayBox(Parts tempPart, int index){
listnum.setElementAt(tempPart.getName(), index);
displaybox.setSelectedIndex(index);
displaybox.ensureIndexIsVisible(index);
}
public void actionPerformed(ActionEvent e) {
int index = displaybox.getSelectedIndex();
if(e.getSource() == add){
Parts newPart = new Parts("");
index = listnum.getSize();
PartEditor tempWindow = new PartEditor(true, newPart, this, index);
}
else if (e.getSource() == edit){
if(index>=0){
Parts f = partsList.getPart(index);
PartEditor editwindow = new PartEditor(false,f, this, index);
}
}else if(e.getSource()==delete){
if(index>=0){
Parts f = partsList.getPart(index);
this.removeFromList(f, index);
}
}
else if(e.getSource()==save)
{
try {
writeToFile(partsList.partList, path);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.