So, here is my program
[code]
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class GUI2 extends JFrame {
private JButton first;
private JButton prev;
private JButton next;
private JButton last;
public int index = 0;
private List lines;
private int currentIndex;
private JFrame textFrame;
public GUI2() {
super("Inventory Program Part 5");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lines = new ArrayList();
setLayout(new FlowLayout());
first = new JButton("First");
add(first);
prev = new JButton("Previous");
add(prev);
next = new JButton("Next");
add(next);
last = new JButton("Last");
add(last);
HandlerClass handler = new HandlerClass();
first.addActionListener(handler);
prev.addActionListener(handler);
next.addActionListener(handler);
last.addActionListener(handler);
}
public GUI2(String arrayRecord) {
this();
try {
BufferedReader reader = new BufferedReader(new FileReader(
arrayRecord));
String line = "";
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void showFile() {
for (int i = 0; i < lines.size(); i++) {
System.out.println(lines.get(i));
}
}
public static void main(final String[] args) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
GUI2 f = new GUI2(args[0]); //<-<-<- error here
f.setSize(200, 200);
f.setVisible(true);
}
});
} catch (Exception e) {
System.out.println("Error");
}
}
private class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
textFrame = new JFrame("Inventory");
textFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
textFrame.getContentPane();
JTextArea textArea = new JTextArea();
textFrame.add(textArea);
Object button = event.getSource();
if (first == button) {
currentIndex = 0;
} else if ((prev == button) && (currentIndex > 0)) {
--currentIndex;
} else if (next == button) {
++currentIndex;
// Wrap
if (currentIndex == lines.size()) {
currentIndex = 0;
}
} else if (last == button) {
currentIndex = lines.size() - 1;
}
textArea.setText(lines.get(currentIndex));
textFrame.setSize(200, 150);
textFrame.setVisible(true);
}
}
}
[/code]
[code]
import javax.swing.JFrame;
public class myMessin{
public static void main (String args[]){
GUI2 go = new GUI2();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
GUI2 file = new GUI2("arrayRecord.txt");
file.showFile();
}
}
[/code]
When I run this I get multiple errors:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at GUI2$HandlerClass.actionPerformed(GUI2.java:112)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
and I can't seem to figure out what is wrong. Any ideas?