Results 1 to 1 of 1
  1. #1
    DC200 is offline Member
    Join Date
    Dec 2008
    Rep Power

    Default Iterating through ArrayList using a GUI

    I'm trying to iterate through an ArrayList containing data from a MySQL table. The program does compile and run without generating any errors, but the problem is that since I have no choice but to instantiate the iterator within the Action class (see sample code below), it is "reset" whenever the GUI ActionListener picks something up.

    Java Code:
    //note: each sqlRecord object stores one row of data from the table (four strings), and has four methods (getName, getAddress, getBirth, getGender)
    class MainGUI extends JFrame {
            //gui items
    	ArrayList<sqlRecord> sqlArray = new ArrayList<sqlRecord>();
    	public MainGUI() {
    		ActionListener listener = new Actions();
    		//actionlistener items
    		//add gui components together
    	class Actions implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			try {
    				Iterator<sqlRecord> sqlItr = sqlArray.iterator();
    				//menu item actions
    				if (e.getActionCommand().equals("Open DB")) {
    				//open database
    				else if (e.getActionCommand().equals("Open")) {
    					Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+openTextField.getText(),"root","password");
    					System.out.println("connected to database");
    					Statement st = con.createStatement();
    					ResultSet res = st.executeQuery("SELECT * FROM MainTable");
    					System.out.println("retrieving available data");
    					while ( {
    						sqlArray.add(new sqlRecord(res.getString("Name"),res.getString("Address"),res.getString("Birth"),res.getString("Gender")));
    				//iterate through arraylist
    				else if (e.getActionCommand().equals("Next")) {
    					sqlRecord current =;
    			catch (Exception q) {
    				System.out.println("mysql error");
    	public static void main(String[] args) {
    		MainGUI window = new MainGUI();
    When the "Next" button is clicked repeatedly, the iterator returns only the first sqlRecord object.

    What I need to do is instantiate the iterator outside of the Actions class, but that would generate a ConcurrentModificationException.

    I'm thinking of switching over to a simple 2-dimensional Array to store the data, though I would still prefer an ArrayList over that since it would make things a lot easier for me. Any help on how I can overcome this problem would be appreciated.
    Last edited by DC200; 08-02-2009 at 03:36 AM.

Similar Threads

  1. Help for iterating data from arraylist uniquely
    By 82rathi.angara in forum New To Java
    Replies: 2
    Last Post: 09-16-2008, 06:55 PM
  2. Iterating through ArrayList using For loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 09:53 AM
  3. Iterating through ArrayList
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 09:50 AM
  4. Replies: 0
    Last Post: 11-14-2007, 04:22 PM
  5. Replies: 0
    Last Post: 11-14-2007, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts