Question: Add to the existing code. Create a swing program that will use a linked list (Array list) to instanciate the data from the text file,
Add to the existing code. Create a swing program that will use a linked list (Array list) to instanciate the data from the text file, have a button use to navigate the linked list of your object or structure. have the buttons include "Previous", "Next", "Delete node" .
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //import header files import java.io.FileWriter; import java.io.IOException;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea;
//Java class with name CandidateDriver public class CandidateDriver {
// main() method public static void main(String[] args) {
JFrame f = new JFrame("Candidate Details"); final JTextArea tf = new JTextArea(); tf.setBounds(40, 40, 500, 300); JButton b = new JButton("Click Here"); b.setBounds(50, 100, 60, 30); JPanel MyPanel = new JPanel();
// Object of Candidate class Candidate c1 = new Candidate(1, "Donald", "Trumph", "White House", "donald@trumph.com", "8888888888", 68, "Male"); // Object of Candidate class Candidate c2 = new Candidate(2, "Joe", "Bidan", "White House", "Joe@Bidan.com", "8822888888", 62, "Male"); // Object of Candidate class Candidate c3 = new Candidate(); c3.setID(3);// set Id c3.setFirstName("Narendra");// set first name c3.setLastName("Modi");// set last name c3.setAddress("New Delhi");// set Address c3.setEmail("naredra@modi.com");// set email c3.setPhone("5566332211");// set phone c3.setAge(65);// set age c3.setGender("Male"); // Object of Candidate class Candidate c4 = new Candidate(); c4.setID(4);// set Id c4.setFirstName("Kamla");// set first name c4.setLastName("Haris");// set last name c4.setAddress("NY");// set Address c4.setEmail("Kamla@Haris.com");// set email c4.setPhone("6599332211");// set phone c4.setAge(60);// set age c4.setGender("Female"); // Object of Candidate class Candidate c5 = new Candidate(5, "Joe", "Root", "UK", "Joe@root.com", "7788994455", 31, "Male"); // code to create new java file try { // Object of FileWriter class FileWriter candidateFile = new FileWriter("persons.txt"); // write to the file candidateFile.write(c1.toString());// write c1 to the file candidateFile.write(c2.toString());// write c2 to the file candidateFile.write(c3.toString());// write c3 to the file candidateFile.write(c4.toString());// write c4 to the file candidateFile.write(c5.toString());// write c5 to the file candidateFile.close();// close file System.out.println("Successfully written to the file Persons.txt"); } // if any exception catch it catch (IOException ex) { System.out.println("ERROR"); ex.printStackTrace(); }
b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
tf.setText(c1.toString() + " " + c2.toString() + " " + c3.toString() + " " + c4.toString() + " " + c5.toString());
} });
MyPanel.add(b); MyPanel.add(tf); f.getContentPane().add(MyPanel, "Center"); f.setSize(600, 400); f.setVisible(true); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
