Hi, I need help with a Java Project in Eclipse that I am totally lost on. We are
ID: 3839304 • Letter: H
Question
Hi, I need help with a Java Project in Eclipse that I am totally lost on. We are to create a program that makes a to do list, has a text field per task to do, and writes it to a file. If the file does not already exist, it creates it, and if it did already exist, populates the text fields with the tasks. Here are the full requirements.
1. Use of static methods where necessary
2. Read in information from a file
3. Make it graphical with a frame and panel
4. Make sure you use loops, conditionals, logical operators and basic operators
5. Ensure that you have proper class structure
6. Allow users to add new items – depending on your application. If it does not allow for this, please explain why.
7. Use an array to store information
8. Use an ArrayList to store information
It should be emty on the first input, and create the file for the list to be stored. On the second input, the file should have a list like so:
Do the dishes
Clean your room
Do your homework
You should then be able to add more things to the list, and have a button that updates the file accordingly.
Explanation / Answer
1. ToDoList.java - Written in Swings
package com.java.ToDoList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
public class ToDoList extends javax.swing.JFrame {
String value = "";
public ToDoList() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
todovalue = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
viewList = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
displayList = new javax.swing.JList();
addList = new javax.swing.JButton();
viewAllValues = new javax.swing.JScrollPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("To - Do List ");
todovalue.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
todovalueActionPerformed(evt);
}
});
jLabel2.setText("Enter your To - Do list");
jLabel3.setText("To Do List Display");
viewList.setText("Click to View list");
viewList.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
viewListActionPerformed(evt);
}
});
displayList.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(displayList);
addList.setText("Add");
addList.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addListActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(158, 158, 158))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(36, 36, 36)
.addComponent(todovalue, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(87, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(viewList)
.addGap(34, 34, 34))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
.addComponent(jScrollPane1)
.addGap(18, 18, 18)
.addComponent(viewAllValues, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(22, 22, 22))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(addList)
.addGap(152, 152, 152))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(17, 17, 17)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(todovalue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(addList)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(48, 48, 48)
.addComponent(viewList))
.addComponent(jScrollPane1)
.addComponent(viewAllValues))
.addContainerGap())
);
pack();
}// </editor-fold>
private void todovalueActionPerformed(java.awt.event.ActionEvent evt) {
// gets the added value from text field
}
private void viewListActionPerformed(java.awt.event.ActionEvent evt) {
FileOperations fo = new FileOperations();
fo.read(); //reads the file
}
private void addListActionPerformed(java.awt.event.ActionEvent evt) {
value = todovalue.getText();
FileOperations fo = new FileOperations();
fo.addToDoListInFile(value); //adds the data into file
todovalue.setText("");
JOptionPane.showMessageDialog(null, "Success", "Added", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ToDoList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ToDoList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ToDoList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ToDoList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ToDoList().setVisible(true);
}
});
}
public void JListExample(List values) {
//create the model and add elements
DefaultListModel<String> listModel = new DefaultListModel<>();
for (Object value : values) {
listModel.addElement(value.toString());
}
//create the list
displayList = new JList<>(listModel);
displayList.setVisibleRowCount(3);
viewAllValues = new JScrollPane(displayList);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JList Example");
this.setSize(200, 200);
this.setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JButton addList;
private javax.swing.JList displayList;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField todovalue;
private javax.swing.JScrollPane viewAllValues;
private javax.swing.JButton viewList;
// End of variables declaration
}
2. FileOperations.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.java.ToDoList;
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.List;
/**
*
* @author user8
*/
public class FileOperations {
String value = "";
BufferedWriter writer = null;
String path = "E:/To-Do_list.txt"; // change the path
List writeList = new ArrayList();
List readList = new ArrayList();
ToDoList td = new ToDoList();
public void addToDoListInFile(String value) {
this.value = value;
writeList.add(value);
write(path, writeList);
}
public void write(String path, List l) {
try {
File f = new File(path);
f.getParentFile().mkdirs();
f.createNewFile();
writer = new BufferedWriter(new FileWriter(path, true));
for (int i = 0; i < l.size(); i++) {
writer.write(l.get(i).toString());
writer.newLine();
}
writer.close();
} catch (Exception e) {
}
}
public void read() {
try {
BufferedReader in = new BufferedReader(new FileReader(path));
String str;
while ((str = in.readLine()) != null) {
readList.add(str);
}
td.JListExample(readList);
in.close();
} catch (IOException e) {
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.