JTable aus ArrayList füllen

sCHween

Grünschnabel
Hei ho

ich schlage mich seit einigen Tagen mit ner Aufgabe rum und denke dass ich langsam verstehe wie das mit den Models t.
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;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		Record result = results.get( rowIndex );
		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;
     } 
	}

}

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...:)
 
Dafür musst du entweder die Variable statisch machen
Code:
static ArrayList<Record> results;
und dann kannst du mit 'test.results' auf diese Variable ganz normal zugreifen

oder die einen sog. getter schreiben: (in der test.java)
Code:
static void getResults() {
return results;
}
und dir dann mir 'test.getResultz()' das resultz objekt "geben" lassen

;-);-);-)
 
Zuletzt bearbeitet:
Moin!
Na die einfachste Art wäre doch sicherlich, die Liste einfach dem TableModel per Konstruktor zu übergeben....



*grüssle*
MeinerEiner
 
hi danke für Eure Antworten..
Ich hab jetzt mal was gebastelt :)
Vielleicht könnt ihr kurz reinschauen und mir helfen, was man noch verbessern könnten.

Vielen Dank im voraus...

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;
       }

	public String getDate() {
		// TODO Auto-generated method stub
		return null;
	}
       
   }
}


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;
        if(data == null)
        {
           throw new IllegalArgumentException("data must not be null");
        } 
    }
    
	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: 
			throw new IllegalArgumentException(String.format("Column {0} does not exist", columnIndex));
		}
	}

	@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: 
			throw new IllegalArgumentException(String.format("Column {0} does not exist", column));
		}
	}
}
 

Neue Beiträge

Zurück