JTable aus ArrayList füllen

Status
Nicht offen für weitere Antworten.

sCHween

Mitglied
Hei ho

ich schlage mich seit einigen Tagen mit ner Aufgabe rum und denke dass ich langsam verstehe wie das mit den Models funzt.
Allerdings happerts noch mit basics Sachen in Bezug auf Java, daher stell ich hier die Frage einfach mal...

Folgenden Code habe ich:
test.java
Code:
package gui;

import java.util.ArrayList;

import infrastructure.interfaces.Record;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;

public class test{
	ArrayList<Record> results;
	public test() {
		// // // // // // // // // // // // // // // // // // // // // //
		// TEMPORARY RESULTS IMPLEMENTATION //
		// // // // // // // // // // // // // // // // // // // // // //
		ArrayList<Record> results = new ArrayList<Record>();
		results.add(new TempResult("One", "U2", "2007-04-01 08:15:02", "3:42"));
		results.add(new TempResult("Two had found three fours of five",
				"The Black Eyed Peas", "2007-04-01 08:19:31", "4:07"));
		results.add(new TempResult("Number three", "Jonny Cash",
				"2007-04-01 08:25:10", "2:58"));
		
		// Das JTable initialisieren
		final TableModel rtm = new ReporterTableModel();
		final JTable table = new JTable(rtm);

		final JFrame frame = new JFrame("Reporter ");
		frame.getContentPane().add(new JScrollPane(table));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		new test();
	}
   

   // // // // // // // // // // // // // // // // // // // // // //
   // TEMPORARY CLASS IMPLEMENTATIONS							  //
   // // // // // // // // // // // // // // // // // // // // // //
    
   /**
    * Temporary class that implements Record.
    * For testing only.
    * 
    * @author brudt1
    */
   class TempResult implements Record
   {
       private String artist;
       private String duration;
       private String start;
       private String title;
       
       public TempResult(String t, String a, String s, String d)
       {
           artist = a;
           title = t;
           start = s;
           duration = d;
       }

       public String getArtist()
       {
           return artist;
       }

       public String getEffDuration()
       {
           return duration;
       }

       public String getStartTime()
       {
           return start;
       }

       public String getTitle()
       {
           return title;
       }
       
   }
}

Mein ReporterTabelModel.java sieht so aus:
Code:
package gui;

import javax.swing.table.AbstractTableModel;
import infrastructure.interfaces.Record;

public class ReporterTableModel extends AbstractTableModel{


	public int getColumnCount() {
	        return 4;
	}

	public int getRowCount() {
		return 0;
	}

	[COLOR="Red"]public Object getValueAt(int rowIndex, int columnIndex) {
		Record result = results.get( rowIndex );
		return null;
	}[/COLOR]

	@Override
	public String getColumnName(int column) {
		switch( column ){
        case 0: return "Artist";
        case 1: return "Title";
        case 2: return "Starttime";
        case 3: return "Duration";
        default: return null;
     } 
	}

}

Mein Problem liegt bei der Methode getValueAt.
Da muss ich ja jetzt auf meine ArrayList zugreifen und anhand der des columnIndex den entsprechenden Wert auslesen.
Allerdings schaff ichs nicht auf mein results aus test.java zuzugreiffen.

Vielen Dank für eure Hilfe - blicke echt nicht durch...🙂
 
Objekte kann man per Parameter übergeben,
so stehts in jedem Buch am Anfang beschrieben,

wenn du hochkomplexe Dinge wie TableModel ohne Grundlagen angehst, kann das ja nur schiefgehen..
 
Hi SlaterB

Danke für Deine Antwort. Ich weiss, die Frage ist für mein Niveau zu hoch, allerdings hab ich sie nicht gewählt 😉

Code:
public Object getValueAt(int rowIndex, int columnIndex) {

kann ich ja nicht einfach mit dem Parameter erweitern, da sonst getValueAt(int, int) wieder implementiert werden muss.

Ich wär Dir echt dankbar wenn du mir weiterhelfen könntest!
 
Hi,

packe deine Daten ( ArrayList<Record> results ) ins Model und greife darauf über getValueAt() und getValueAt() zu. Sonst wirds recht schnell recht häßlich :wink:

Gruß,
madboy
 
tasch auch 😉

ich hab hier mal eine Version die läuft!
Allerdings bewege ich mich hier auf ziemlich dünnen Eis!

Thanks für eure Unterstützung...

test.java

Code:
package gui;

import java.util.ArrayList;

import infrastructure.interfaces.Record;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;

public class test{
	
	public test() {
		// // // // // // // // // // // // // // // // // // // // // //
		// TEMPORARY RESULTS IMPLEMENTATION            //
		// // // // // // // // // // // // // // // // // // // // // //
		ArrayList<Record> results = new ArrayList<Record>();
		results.add(new TempResult("One", "U2", "2007-04-01 08:15:02", "3:42"));
		results.add(new TempResult("Two had found three fours of five",
				"The Black Eyed Peas", "2007-04-01 08:19:31", "4:07"));
		results.add(new TempResult("Number three", "Jonny Cash",
				"2007-04-01 08:25:10", "2:58"));
		
		// INIT JTABLE
		final TableModel rtm = new ReporterTableModel(results);
		final JTable table = new JTable(rtm);

		final JFrame frame = new JFrame("Reporter ");
		frame.getContentPane().add(new JScrollPane(table));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		new test();
	}

   // // // // // // // // // // // // // // // // // // // // // //
   // TEMPORARY CLASS IMPLEMENTATIONS		  //
   // // // // // // // // // // // // // // // // // // // // // //
    
   /**
    * Temporary class that implements Record.
    * For testing only.
    * 
    * @author brudt1
    */
   class TempResult implements Record
   {
       private String artist;
       private String duration;
       private String start;
       private String title;
       
       public TempResult(String t, String a, String s, String d)
       {
           artist = a;
           title = t;
           start = s;
           duration = d;
       }

       public String getArtist()
       {
           return artist;
       }

       public String getEffDuration()
       {
           return duration;
       }

       public String getStartTime()
       {
           return start;
       }

       public String getTitle()
       {
           return title;
       }
       
   }
}


ReporterTableModel.java

Code:
package gui;

import infrastructure.interfaces.Record;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

public class ReporterTableModel extends AbstractTableModel{
   
	private ArrayList<Record> data;
    
    public ReporterTableModel(ArrayList<Record> data) {
        this.data = data;
    }
    
	public int getColumnCount() {
		return 4;
	}

	public int getRowCount() {
		return data.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		Record result = data.get(rowIndex);
		switch (columnIndex) {
		case 0:
			return result.getArtist();
		case 1:
			return result.getTitle();
		case 2:
			return result.getStartTime();
		case 3:
			return result.getEffDuration();
		default:
			return null;
		}
	}

	@Override
	public String getColumnName(int column) {
		switch (column) {
		case 0:
			return "Artist";
		case 1:
			return "Title";
		case 2:
			return "Starttime";
		case 3:
			return "Duration";
		default:
			return null;
		}
	}
}
 
Code:
abstract class AbstractListTableModel<TRowType> extends AbstractTableModel
{
   private List<TRowType> data = new ArrayList<TRowType>();
   private String columnNames[];

   public AbstractListTableModel(String columnNames[])
   {
      this.columnNames = columnNames;
      if(columnNames == null)
      {
         throw new IllegalArgumentException("columnNames must not be null");
      }
   }

   public final void setData(List<TRowType> data)
   {
      this.data.clear();
      if(data != null)
      {
         this.data.addAll(data);
      }
      fireTableDataChanged();
   }

   public final List<TRowType> getData()
   {
      List<TRowType> result = new ArrayList<TRowType>(data.size());
      result.addAll(data);
      return result;
   }

   protected abstract Object getValueAt(TRowType src, int column);

   public final Object getValueAt(int row, int column)
   {
      return getValueAt(data.get(row), column);
   }

   protected void setValueAt(TRowType dst, int column, Object value)
   {
   }

   public final void setValueAt(Object value, int row, int column)
   {
      setValueAt(data.get(row), column, value);
      fireTableRowsUpdated(row, row);
   }

   public final int getRowCount()
   {
      return data.size();
   }

   public int getColumnCount()
   {
      return columnNames.length;
   }

   public String getColumnName(int column)
   {
      return columnNames[column];
   }

}
Verwendung
Code:
AbstractListTableModel<Record> model = new AbstractListTableModel<Record>(
         new String[] { "Artist", "Title", "Starttime", "Duration" } ) {
   protected Object getValueAt(Record src, int column)
   {
      switch (column)
      {
      case 0: return src.getArtist();
      case 1: return src.getTitle();
      case 2: return src.getStartTime();
      case 3: return src.getEffDuration();
      default: throw new IllegalArgumentException(String.format("Column {0} does not exist", column));
      }
   }
};

model.setData(results);
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben