what is the best way to display an arraylist? i am currently trying to display it in a textarea but not sure what method to use...

class Action2 implements ActionListener {
	     public void actionPerformed (ActionEvent e) {
	         String name = field2.getText();
		String homeworks = field3.getText();
		String exams = field4.getText(); 
		record = new StudentRec(name, homeworks, exams);
		db.addRecord(record);
		int i = db.getSize();		  
		label6.setText("size of db = " + i);
		
                 ArrayList<StudentRec> ls = db.printArrayList();
	         for(int j=0; j< ls.size(); j++){
                   textArea1.method(ls.get(j));}       // <-----
		  }
	 }

JTable

i think i should be adding a row though each iteration to the table though i only see an addColumn, no addRow?

JTable has a bit of a learning curve, but once you've done that it all hangs together very neatly.
You create a TableModel that uses your arraylist of StudentRecs to return column names, cell values etc as required by the TableModel interface definition and, hey presto, it just works.
Have a look at
http://download.oracle.com/javase/tutorial/uiswing/components/table.html#data

ok, so i've got a table, not an astetically pleasing one, but it works nonetheless...anyway the problems i'm facing now comes when adding a record, the first record takes to the table, but when i add a second it adds the first record again, any suggestions on how to get the second record (four five six) added to table? ...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;


public class MyDbGUIPanel extends JPanel {

  private JTextField field1, field2,field3,field4;
  private JLabel label1, label2, label3, label4, label5, label6, label7;
  private JButton button1, button2;
  private StudentDB db;
  private StudentRec record;
 // private JTextArea textArea1;
  private JTable jtable1;
  private DefaultTableModel tableModel1;
  private Object[] ia;
 
  
    public MyDbGUIPanel () {
		  super();
		  label1 = new JLabel("Create a database: ");
		  add (label1);
		  field1 = new JTextField(5);
		  add (field1);
		  button1 = new JButton("Add Database");
		  add(button1);
		  button1.addActionListener(new Action());
		//  field1.addActionListener(new Action());	  
		  label2 = new JLabel("   --------               ");
		  add(label2);
		  label3 = new JLabel("    Create a new record  (Name)");
		  add(label3);
		  field2 = new JTextField(15);
		  add (field2);
		  label4 = new JLabel(" (Homeworks)");
		  add(label4);
		  field3 = new JTextField(15);
		  add (field3);
		  label5 = new JLabel(" (Exams)");
		  add(label5);
		  field4 = new JTextField(15);
		  add (field4);
		  button2 = new JButton ("Add Record");
		  add (button2);
		  button2.addActionListener(new Action2());
                    label6 = new JLabel("     --------           ");
		  add(label6);
		  tableModel1 = new DefaultTableModel();
	           tableModel1.addColumn("Name, Homeworks, Exams");
	//	  tableModel1.addColumn("Homeworks");
	//	  tableModel1.addColumn("Exams");
		  jtable1 = new JTable(tableModel1);
		  add(jtable1);
		  
	     setPreferredSize (new Dimension(400,400));
	 }
	 
	  class Action implements ActionListener {
	     public void actionPerformed (ActionEvent e) {
	         String newDatabaseName = field1.getText();
				db = new StudentDB(newDatabaseName);
			   label2.setText("databse: " + db);	
	     } 
	 }
	 
	  class Action2 implements ActionListener {
	     public void actionPerformed (ActionEvent e) {
	         String name = field2.getText();
				String homeworks = field3.getText();
				String exams = field4.getText(); 
				record = new StudentRec(name, homeworks, exams);
				db.addRecord(record);
				int i = db.getSize();		  
		      label6.setText("size of db = " + i);
		   	 ArrayList<StudentRec> ls = db.printArrayList();

            ia = ls.toArray();
				tableModel1.addRow(ia);
//  field2.setText("");   name="";

		  }
	 }  	  
	   
}

I have no way to understand what's going on in Action2. You have an unknown printArrayList that you assign to an ArrayList that you convert to an array that you add to the table model using addRow. It looks like you are getting the whole updated (multi-record) database then using addRow - which only adds one row. Probably that's why it's only adding the first row every time?

As I said before, it's easier and cleaner in the end to create your own tableModel that accesses your actual source data directly.

i think that is exactly what is happening, the whole updated (multi-record) database is using addRow - which only adds one row, and only adding the first row every time. can you provide an example on how i should use the tableModel? not really sure how to start.

public ArrayList<StudentRec> printArrayList() {	 
	  return studentDB;
	 }

instead of printing the db i added a toarray method for converting a record and added that each time, which seems to work out well...

class Action2 implements ActionListener {
	     public void actionPerformed (ActionEvent e) {
	         String name = field2.getText();
				String homeworks = field3.getText();
				String exams = field4.getText(); 
				record = new StudentRec(name, homeworks, exams);
				db.addRecord(record);  //add record to db
				int i = db.getSize();  //get db size
		      label6.setText("size of db = " + i);
            //convert record to an array and add to table
            ia = record.toAnArray();
				tableModel1.addRow(ia);
				//clear 'new record' fields
            field2.setText("");
				field3.setText("");
				field4.setText("");
		  }
	 }

Yes, that looks better - adding just the new row, not the whole database.

any suggestions on how to delete a record? i am able to in a non gui using

public void deleteRecord(StudentRec recordToDelete) {
	   int index = studentDB.indexOf(recordToDelete);
		studentDB.remove(index);
	 }

but not having too much luck in the gui. this doesn't seem to be doing the trick...

class Action3 implements ActionListener {
   public void actionPerformed (ActionEvent e) {

     db.deleteRecord(record);				
     tableModel1.removeRow(jtable1.getSelectedRow());
		  }
	 }

How does te user specify which record to delete? Is it by selecting that row in the table? - in which case your code on line 6 looks OK to me, but there's no clue as to where "record" on line 5 comes from.
If you want help you really must be a lot more specific about what errors you are getting. "this doesn't seem to be doing the trick" gives us absolutely NOTHING to go on. If you have an error or exception message please post the whole message with the relevant lines of code. If it gives a wrong behaviour please describe the exact expected behaviour and actual behaviour. Help us to help you.

not getting an error... what happens is when i go to delete, i select
the row and click the 'delete record' button... the record erases from
the table but i'm not so sure it is deleting from the db(arraylist)
since the 'db size' label doesn't decrement. record on line 5 in action3 is coming from action2

public class StudentRec {

    private String name, homeworks, exams;
//	 private String[] arr;
	 
	 public StudentRec(String name, String homeworks, String exams) {
	     this.name = name;
		  this.homeworks = homeworks;
		  this.exams = exams;
	 }...
public class StudentDB   {

 
	private ArrayList<StudentRec> studentDB;

	private String databaseName;


    public StudentDB(String databaseName) {

		 studentDB = new ArrayList<StudentRec>();
		    
			this.databaseName = databaseName;
			
	 }...

    public void deleteRecord(StudentRec recordToDelete) {
	   int index = studentDB.indexOf(recordToDelete);
		studentDB.remove(index);
	 }
...
}
public class MyDbGUIPanel extends JPanel {
...

	  class Action2 implements ActionListener {
	     public void actionPerformed (ActionEvent e) {
	         String name = field2.getText();
		String homeworks = field3.getText();
		String exams = field4.getText(); 
	record = new StudentRec(name, homeworks, exams);  // record from action3 orginates here
		db.addRecord(record);  //add record to db
		int i = db.getSize();  //get db size
		label6.setText("size of db = " + i);
            //convert record to an array and add to table
                  ia = record.toAnArray();
	         tableModel1.addRow(ia);
		//clear 'new record' fields
                  field2.setText("");
		field3.setText("");
		field4.setText("");
	  }
     }  


	 class Action3 implements ActionListener {
	     public void actionPerformed (ActionEvent e) {

		  db.deleteRecord(record);	   //line 5 from previous post	
	           tableModel1.removeRow(jtable1.getSelectedRow());
	  }
         }...

You use the variable "record" in the action listener, but you don't set it there. Shouldn't it be whatever record is referred to by the currently selected row? Presumably it just contains whatever value it had the last time any other method used it?
Either way, your db size label won't update unless you notify it somehow that he database has been updated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.