In: Computer Science
Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to a new text file. At first, only the Read button is enabled. After it is clicked, it is disabled and the Reverse button is enabled. After it is clicked, it is disabled and the Write button is enabled. After it is clicked, it is disabled. The name of the input text file is "input.txt". The input text file will contain no more than 100 lines of text. This fact is not needed by the program. It simply means that memory usage is not an issue. The name of the output text file is "output.txt". Notes: 1. The name of your main class should be ReverseFileViaStack. 2. Use the Java class library Stack class for your stack. 3. Use a border layout for your contents pane. 4. In the north region include the following title: Reverse a Text File via a Stack. 5. Use a panel in the center region to contain the 2 list boxes (side-by-side). 6. Place the 3 buttons in the south region. 7. A useful resource for this project is the week 5 video: Java GUI List Components
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
public class DealingFiles extends JFrame implements
ActionListener
{
private Container listContents;
// declaring ui fields
private JLabel south_side, north_side, center;
private JButton button_read, button_write, button_reverse;
//declaring lists
private JList<String> dataList1;
private JList<String> dataList2;
// Fonts:
private final Font boldFont = new Font(Font.DIALOG, Font.BOLD,
14);
private final Font normalFont = new Font(Font.DIALOG, Font.PLAIN,
14);
private DefaultListModel<String> listModel1 = new
DefaultListModel<>();
private DefaultListModel<String> listModel2 = new
DefaultListModel<>();
private Stack<String> myStack = new Stack<>();
public DealingFiles()
{
super("Reverse a txt File using STACK");
// declaring models
listModel1 = new DefaultListModel();
dataList1 = new JList<>(listModel1);
listModel2 = new DefaultListModel();
dataList2 = new JList<>(listModel2);
listContents = getContentPane();
// layout management
listContents.setLayout(new BorderLayout());
north_side = new JLabel("Text File reverse using stack.");
//buttons
button_read = new JButton("Read");
button_reverse = new JButton("Reverse");
button_write = new JButton("write");
//adding buttons to panel
listContents.add(button_read);
listContents.add(button_reverse);
listContents.add(button_write);
ButtonHandler button_handler = new ButtonHandler();
button_read.addActionListener(button_handler);
button_reverse.addActionListener(button_handler);
button_write.addActionListener(button_handler);
// adding elements
listContents.add(north_side, BorderLayout.NORTH);
listContents.add(center, BorderLayout.CENTER);
listContents.add(south_side, BorderLayout.SOUTH);
setSize(800, 600);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
Object src = e.getSource();
if (src == button_read)
{
readDataFile();
return;
}
if (src == button_reverse)
{
reverseDatFile();
return;
}
if (src == button_write)
{
writeDataFile();
return;
}
}
private void readDataFile()
{
try (BufferedReader sc = new BufferedReader(new
FileReader(file)))
{
while ((dataList1 = sc.readLine()) != null)
{
dataList1.addElement();
}
}
}
private void reverseDatFile()
{
listModel2.elementAt();
dataList1.addElement();
}
private void applyFonts()
{
UIManager.put("Button.font", boldFont);
UIManager.put("ComboBox.font", boldFont);
UIManager.put("Label.font", boldFont);
UIManager.put("List.font", normalFont);
}
private void writeDataFile()
{
try
{
FileOutputStream stream = new
FileOutputStream("input.txt", false);
PrintWriter pwriter = new
PrintWriter(stream);
pwriter.print(dataList2);
pwriter.println(true);
pwriter.close();
}
catch(FileNotFoundException fnfe)
{
System.out.println("file not
found");
}
}
public static void main(String[] args)
{
DealingFiles obj = new DealingFiles();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}