Reputation: 140
My main purpose with this GUI program is to have the user enter the first name of a student into the first name box and the corresponding last name into the last name box and then hit save. Hitting Save will then save the first and last name into their respective arrays. After the user is done entering names they will then click sort. This will then sort the names in alphabetical order and then re-draw the GUI to show the result for the newly sorted array of names. My main problems I have is that when I enter a name I get a laundry list of errors (seen below.) I also do not know how to redraw the GUI to show the array of names in a list. Any help would be appreciated, thank you!
Here is my current code. (The variable names are long but I hate making variable names and then forgetting what they are used for.) import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.*;
public class main extends JFrame {
JTextField studentNameInputFirst, studentNameInputLast, studentNamesEnetered;
JButton nextName, sort;
JLabel firstName, lastName;
String disp = "";
int totalNumberOfStudents = 0;
String[] studentFirstNames = new String[totalNumberOfStudents];
String[] studentLastNames = new String[totalNumberOfStudents];
public main() {
Container container = getContentPane();
container.setLayout(new FlowLayout());
studentNamesEnetered = new JTextField();
studentNameInputFirst = new JTextField(15);
studentNameInputLast = new JTextField(15);
nextName = new JButton("Save");
sort = new JButton("Sort");
firstName = new JLabel("First Name: ");
lastName = new JLabel("Last Name: ");
container.add(firstName);
container.add(studentNameInputFirst);
container.add(lastName);
container.add(studentNameInputLast);
container.add(nextName);
container.add(sort);
nextName.addActionListener(new nextNameListener());
sort.addActionListener(new sortListener());
setSize(325, 120);
setVisible(true);
}
private class nextNameListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
totalNumberOfStudents++;
studentFirstNames[totalNumberOfStudents] = studentNameInputFirst.getText();
studentLastNames[totalNumberOfStudents] = studentNameInputLast.getText();
studentNameInputLast = null;
studentNameInputFirst = null;
}
}
private class sortListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
public static void main(String[] args) {
main drawGui = new main();
drawGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Here is the error I get when running the above code:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at main$nextNameListener.actionPerformed(main.java:48)
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.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)
EDIT: After using the answer provided suggesting an ArrayList instead of a normal array, I get no more errors. I made changes as well to the naming of my program to follow the Java naming conventions and for my own sanity of knowing what the program does. Changes were made on lines, 13, 14, 45, and 46. Thanks for your help!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class ClassNameSorting extends JFrame {
JTextField studentNameInputFirst, studentNameInputLast, studentNamesEnetered;
JButton nextName, sort;
JLabel firstName, lastName;
String disp = "";
ArrayList<String> studentNameFirst = new ArrayList<String>();
ArrayList<String> studentNameLast = new ArrayList<String>();
public ClassNameSorting() {
Container container = getContentPane();
container.setLayout(new FlowLayout());
studentNamesEnetered = new JTextField();
studentNameInputFirst = new JTextField(15);
studentNameInputLast = new JTextField(15);
nextName = new JButton("Save");
sort = new JButton("Sort");
firstName = new JLabel("First Name: ");
lastName = new JLabel("Last Name: ");
container.add(firstName);
container.add(studentNameInputFirst);
container.add(lastName);
container.add(studentNameInputLast);
container.add(nextName);
container.add(sort);
nextName.addActionListener(new nextNameListener());
sort.addActionListener(new sortListener());
setSize(325, 120);
setVisible(true);
}
private class nextNameListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
studentNameFirst.add(studentNameInputFirst.getText());
studentNameLast.add(studentNameInputLast.getText());
studentNameInputLast = null;
studentNameInputFirst = null;
}
}
private class sortListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
public static void main(String[] args) {
ClassNameSorting drawGui = new ClassNameSorting();
drawGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Upvotes: 0
Views: 263
Reputation: 324157
int totalNumberOfStudents = 0;
String[] studentFirstNames = new String[totalNumberOfStudents];
String[] studentLastNames = new String[totalNumberOfStudents];
Your array size is 0. You need to allocate the array with then number of entries you expect to add to the array.
Don't use an Array because you don't know what the size should be. Instead use an ArrayList
.
Upvotes: 2