Today I once again seek your help/support for a project I am working on at my job. Right now I have created a GUI that allows a user to upload a file in "ach" format. (Which is just a text file with specific formatting). Once uploaded the program will break up the lines of text using a buffered reader into four possible JLists. In each text file there is up to four "batch headers" (number that begins with 5), and any amount of "entry details" (number that begins with 6). The ach file may look like this:
500000000000000000001
600000000000000000001
600000000000000000002
600000000000000000003
555555555555555555551
622222222222222222221
622222222222222222223
622222222222222222224
So the first "five" number and all the "sixes" that follow would be "batch one" and be displayed in the first JList and the second "five" number and all the "sixes" that follow would be displayed in the second JList. The other two JLists would be empty in this situation since there are only two batches.
Now here is what I am trying to accomplish:
Once the user uploads a file and the fields are populated (which I have successfully done), the user needs to have the ability to click on one of the "six" numbers in the list. Once they click on it, a JOptionsPane will ask them for a reason code. That is my first problem, how to implemenet the listener. Because the JList fields are populated using a String Array that is filled later in the program using the Buffered Reader, I am not sure how to accomplish this.
I won't get into the second problem right now as my focus is on this. Below is my code, any help would be greatly appreciated!
package nacha;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.filechooser.FileNameExtensionFilter;
public class NachaVersion3
{
final static boolean RIGHT_TO_LEFT = false;
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
static int entryDetailCountOne;
static int entryDetailCountTwo;
static int entryDetailCountThree;
static int entryDetailCountFour;
static String batchHeaderDisplayOne=" ";
static String batchHeaderDisplayTwo=" ";
static String batchHeaderDisplayThree=" ";
static String batchHeaderDisplayFour=" ";
static ArrayList<String> entryDetailListGUI = new ArrayList<String>();
static ArrayList<String> entryDetailList = new ArrayList<String>();
static ArrayList<String> entryDetailList2 = new ArrayList<String>();
static ArrayList<String> entryDetailList3 = new ArrayList<String>();
static ArrayList<String> entryDetailList4 = new ArrayList<String>();
public static void addComponentsToPane(Container pane){
if (RIGHT_TO_LEFT){
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
Border border = BorderFactory.createRaisedBevelBorder();
JButton submit;
JLabel labelTitle;
JLabel labelEntryDetailHeader1;
JLabel labelEntryDetailHeader2;
JLabel labelEntryDetailHeader3;
JLabel labelEntryDetailHeader4;
final JList listEntryDetail1;
JList listEntryDetail2;
JList listEntryDetail3;
JList listEntryDetail4;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill){
c.fill = GridBagConstraints.HORIZONTAL;
}
labelTitle = new JLabel("ACH File Converter");
labelTitle.setBorder(border);
labelTitle.setFont(new java.awt.Font("Arial", Font.BOLD, 60));
labelTitle.setHorizontalAlignment(SwingConstants.CENTER);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=3;
c.gridx=0;
c.gridy=0;
pane.add(labelTitle, c);
labelEntryDetailHeader1 = new JLabel("Entry Detail Files Under Header: "+batchHeaderDisplayOne.substring(0,50));
labelEntryDetailHeader1.setBorder(border);
labelEntryDetailHeader1.setFont(new java.awt.Font("Arial", Font.LAYOUT_LEFT_TO_RIGHT, 14));
c.fill=GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=0;
c.gridy=1;
pane.add(labelEntryDetailHeader1, c);
labelEntryDetailHeader2 = new JLabel("Entry Detail Files Under Header: "+batchHeaderDisplayTwo.substring(0,50));
labelEntryDetailHeader2.setBorder(border);
labelEntryDetailHeader2.setFont(new java.awt.Font("Arial", Font.LAYOUT_LEFT_TO_RIGHT, 14));
c.fill=GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=1;
c.gridy=1;
pane.add(labelEntryDetailHeader2, c);
listEntryDetail1 = new JList(entryDetailList.toArray());
listEntryDetail1.setBorder(border);
listEntryDetail1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listEntryDetail1.setLayoutOrientation(JList.VERTICAL);
listEntryDetail1.setVisibleRowCount(10);
listEntryDetail1.setSize(200,200);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=0;
c.gridy=2;
JScrollPane scrollPane1 = new JScrollPane(listEntryDetail1);
pane.add(scrollPane1, c);
listEntryDetail2 = new JList(entryDetailList2.toArray());
listEntryDetail2.setBorder(border);
listEntryDetail2.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listEntryDetail2.setLayoutOrientation(JList.VERTICAL);
listEntryDetail2.setVisibleRowCount(10);
listEntryDetail2.setSize(200,200);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=1;
c.gridy=2;
JScrollPane scrollPane2 = new JScrollPane(listEntryDetail2);
pane.add(scrollPane2, c);
labelEntryDetailHeader3 = new JLabel("Entry Detail Files Under Header: "+batchHeaderDisplayThree.substring(0,50));
labelEntryDetailHeader3.setBorder(border);
labelEntryDetailHeader3.setFont(new java.awt.Font("Arial", Font.LAYOUT_LEFT_TO_RIGHT, 14));
c.fill=GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=0;
c.gridy=3;
pane.add(labelEntryDetailHeader3, c);
labelEntryDetailHeader4 = new JLabel("Entry Detail Files Under Header: "+batchHeaderDisplayFour.substring(0,50));
labelEntryDetailHeader4.setBorder(border);
labelEntryDetailHeader4.setFont(new java.awt.Font("Arial", Font.LAYOUT_LEFT_TO_RIGHT, 14));
c.fill=GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=1;
c.gridy=3;
pane.add(labelEntryDetailHeader4, c);
listEntryDetail3 = new JList(entryDetailList3.toArray());
listEntryDetail3.setBorder(border);
listEntryDetail3.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listEntryDetail3.setLayoutOrientation(JList.VERTICAL);
listEntryDetail3.setVisibleRowCount(10);
listEntryDetail3.setSize(200,200);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=0;
c.gridy=4;
JScrollPane scrollPane3 = new JScrollPane(listEntryDetail3);
pane.add(scrollPane3, c);
listEntryDetail4 = new JList(entryDetailList4.toArray());
listEntryDetail4.setBorder(border);
listEntryDetail4.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
listEntryDetail4.setLayoutOrientation(JList.VERTICAL);
listEntryDetail4.setVisibleRowCount(10);
listEntryDetail4.setSize(200,200);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=1;
c.gridx=1;
c.gridy=4;
JScrollPane scrollPane4 = new JScrollPane(listEntryDetail4);
pane.add(scrollPane4, c);
submit = new JButton("Submit");
submit.setFont(new java.awt.Font("Arial", Font.BOLD, 40));
submit.setHorizontalAlignment(SwingConstants.CENTER);
submit.setVerticalAlignment(SwingConstants.CENTER);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth=2;
c.gridheight=8;
c.gridx=0;
c.gridy=5;
c.ipady=153;
pane.add(submit,c);
}
private static void createAndShowGUI(){
JFrame frame = new JFrame("ACH File Converter");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]){
BufferedReader br = null;
BufferedWriter bw = null;
int countFive = 0;
ArrayList<String> batchHeaderList = new ArrayList<String>();
JFileChooser chooser = new JFileChooser();
UIManager.put("ScrollPane.background", Color.BLACK);
FileNameExtensionFilter filter = new FileNameExtensionFilter("ACH Files", "ach");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Please choose ACH file to upload");
int returnVal = chooser.showOpenDialog(chooser);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
try{
String sCurrentLine;
br = new BufferedReader(new FileReader(chooser.getSelectedFile()));
bw = new BufferedWriter(new FileWriter(chooser.getCurrentDirectory()+"//NachaOutput.ACH"));
while((sCurrentLine = br.readLine()) !=null)
{
if (sCurrentLine.startsWith("5")){
batchHeaderList.add(sCurrentLine);
countFive++;
if (countFive == 1){
batchHeaderDisplayOne=sCurrentLine;
}else if (countFive ==2 ){
batchHeaderDisplayTwo=sCurrentLine;
}else if (countFive == 3){
batchHeaderDisplayThree=sCurrentLine;
}else if (countFive == 4){
batchHeaderDisplayFour=sCurrentLine;
}
}else if (sCurrentLine.startsWith("6") && countFive==1){
entryDetailList.add(sCurrentLine);
}else if (sCurrentLine.startsWith("6") && countFive==2){
entryDetailList2.add(sCurrentLine);
}else if (sCurrentLine.startsWith("6") && countFive==3){
entryDetailList3.add(sCurrentLine);
}
}
} catch (IOException e){
e.printStackTrace();
} finally {
try {
br.close();
bw.close();
} catch (IOException e){
e.printStackTrace();
}
}
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGUI();
}
});
}
}
}
If you desire to test this code in order to help me all you need to do is copy and paste the ACH sample text from above into a text file and save it as .ACH