In the class WordPair() the method remove() is supposed to take in an array and split the strings inside of it and then add those string to two different array lists. wordListB works correctly and the array gets filled fine. Also when I a system print inside of this method it prints out wordListC correctly as well. The problem I am having it when the getWord method is called in the wordSelection method of the JlistFromFile class to retrieve a string from worldListC at a specific index, At this point the array prints out empty. I set up a test in the main and the class itself is working how its supposed to. I can't figure out what is making arrayListC to become cleared out.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.Collections;
import java.util.Arrays;
import java.io.*;
/* Simple example of using the contents of a file to populate a JList
*
* @author Jill Courte
*/
public class JListFromFile extends JFrame {
private TextField wordA;
private TextField wordB;
private JButton openButton;
private JButton newButton;
private JButton addButton;
private JButton deleteButton;
private JButton saveButton;
private TextField output;
private JList listFromFile;
private JPanel listPanel;
private JPanel textPanel;
private JPanel inputPanel;
private JPanel buttonsPanel;
private SimpleWordList dataSource;
private WordPair wordPair;
public JListFromFile ()
{
// create the object to provide the data
dataSource = new SimpleWordList();
wordPair = new WordPair();
// use a border layout for the main window
getContentPane().setLayout(new BorderLayout(20, 20));
buttonsPanel = new JPanel();
openButton = new JButton("Open");
newButton = new JButton("New");
addButton = new JButton("Add");
deleteButton = new JButton("Delete");
saveButton = new JButton("Save");
addButton.setEnabled( false );
deleteButton.setEnabled( false );
saveButton.setEnabled( false );
buttonsPanel.add(openButton);
buttonsPanel.add(newButton);
buttonsPanel.add(addButton);
buttonsPanel.add(deleteButton);
buttonsPanel.add(saveButton);
//add the button listeners
openButton.addActionListener(new OpenButtonListener());
addButton.addActionListener(new AddButtonListener());
add(buttonsPanel, BorderLayout.NORTH);
// create the panel to hold the list
listPanel = new JPanel();
// create your JList
listFromFile = new JList();
listFromFile.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listFromFile.setSelectedIndex(0);
//add the list selection listener
listFromFile.addListSelectionListener(new WordSelection());
//add the translation text box
textPanel = new JPanel();
output = new TextField();
output.setEditable(false);
output.setPreferredSize(new Dimension(200, 25));
textPanel.add(output);
// add scroll bars to list
JScrollPane listScrollPane = new JScrollPane(listFromFile);
listScrollPane.setPreferredSize(new Dimension(150, 200));
//add user input textfield
wordA = new TextField ("Word to Add");
wordB = new TextField ("Translated word");
wordA.setPreferredSize(new Dimension(200, 25));
wordB.setPreferredSize(new Dimension(200, 25));
wordA.setEnabled(false);
wordB.setEnabled(false);
//create panel to hold input textfield
inputPanel = new JPanel();
inputPanel.add(wordA);
inputPanel.add(wordB);
//add the list (via the scroll pane) to the panel
listPanel.add(listScrollPane);
//add the panels to the frame
add(listPanel, BorderLayout.WEST);
add(textPanel, BorderLayout.CENTER);
add(inputPanel, BorderLayout.SOUTH);
pack();
}
private File findFile ()
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// Start in current directory
chooser.setCurrentDirectory(new File("."));
int status = chooser.showOpenDialog(null);
if (status != JFileChooser.APPROVE_OPTION)
{
JOptionPane.showMessageDialog(null, "No File Selected");
throw new NoFileChoosenException();
}
else
{
File file = chooser.getSelectedFile();
System.out.println(file.getName());
System.out.println(file.getPath());
return file;
}
}
private class OpenButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
listFromFile.setListData(new Object[0]);
File file = null;
try
{
file = findFile();
//tell the data source what file to use
dataSource.createList(file);
//JList needs an array of strings, retrieve it from the data source
String [] data = dataSource.getData();
//data = dataSource.insertion(data);
//data = wordPair.remove(data);
if (data != null)
listFromFile.setListData(data);
}
catch (Exception e)
{
}
addButton.setEnabled(true);
deleteButton.setEnabled(true);
saveButton.setEnabled(true);
wordA.setEnabled(false);
wordB.setEnabled(false);
}
}
private class AddButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
wordA.setEnabled(true);
wordB.setEnabled(true);
String inputStringA = wordA.getText();
String inputStringB = wordB.getText();
//dataSource.addToList(inputStringA, inputStringB);
String [] data2 = dataSource.getData();
//data2 = dataSource.insertion(data2);
// data2 = wordPair.remove(data2);
listFromFile.setListData(new Object[0]);
listFromFile.setListData(data2);
}
}
private class WordSelection implements ListSelectionListener
{
public void valueChanged (ListSelectionEvent event)
{
int index = listFromFile.getSelectedIndex();
if (index >= 0)
{
String s = wordPair.getWord(index);
output.setText(s);
}
}
}
public static void main(String[] args)
{
JListFromFile jListFromFile = new JListFromFile();
jListFromFile.setVisible(true);
}
}
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.ListIterator;
/*example of using collection class and input parsing for CIT 271
*@author Jill Courte
*/
public class SimpleWordList {
private ArrayList<String> wordListA = new ArrayList<String>();
private ArrayList<String> wordListB = new ArrayList<String>();
private ArrayList<String> wordListC = new ArrayList<String>();
private File currentFile = null;
private WordPair wordPair;
//words are in the file one word on a line
//words are read, checked for duplicates, and inserted into the file
public void createList (File file)
{
Scanner fileIn = null;
currentFile = file;
String line;
try
{
fileIn = new Scanner(new FileInputStream(currentFile.getPath()));
}
catch (IOException e)
{
System.out.println(e.getMessage());
System.exit(0);
}
while (fileIn.hasNextLine())
{
line = fileIn.nextLine();
if (! isDuplicate(line))
insertIntoList(line);
}
fileIn.close();
}
//Uses an iterator to check for the existance of the given
//word in the list
private boolean isDuplicate (String word)
{
Iterator<String> it = wordListA.iterator();
while (it.hasNext())
{
//not ignoring case to look in class at some issues with input
String s = it.next();
if (word.equals(s))
return true;
}
return false;
}
private void insertIntoList (String word)
{
int index = 0;
boolean positionFound = false;
int result;
while (! positionFound && index < wordListA.size())
{
String tmp = wordListA.get(index);
result = tmp.compareTo(word);
if (result > 0)
positionFound = true;
else
index++;
}
//index holds the position to insert the word
wordListA.add(index, word);
//System.out.println(word);
}
public String [] getData ()
{
String [] arr = new String[wordListA.size()];
wordPair = new WordPair();
String [] temp2 = wordPair.remove(wordListA.toArray(arr));
// use the collection class method to turn this into a
// primitive array
return temp2;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
public class WordPair
{
private ArrayList<String> wordListB;
private ArrayList<String> wordListC;
private String [] arrayA;
private String [] arrayB;
private String wordA;
private String wordB;
public String[] remove (String numList[])
{
wordListB = new ArrayList<String>();
wordListC = new ArrayList<String>();
ArrayList<String> ar = new ArrayList<String>();
for( int i=0; i < numList.length; i++)
{
String string = numList[i];
String[] parts = string.split("=");
wordA = parts[0];
wordListB.add(wordA);
wordB = parts[1];
wordListC.add(wordB);
}
System.out.println(wordListC);
arrayA = wordListB.toArray(new String[wordListB.size()]);
return arrayA;
}
public String getWord(int index)
{
System.out.println(wordListC);
return wordListC.get(index);
}
}
I don't have any experience using a debugger- Well now is a great time to start!