Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 10:21 AM
Member
 
Join Date: Jun 2007
Posts: 95
Rep Power: 0
Felissa is on a distinguished road
Default Problem with JTable
Hi, it's just a simple question, i like to have a jtable displays just a few columns...

here's the code:
Code:
package ch.svenu.jTableXP;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import infrastructure.interfaces.*;
 
public class jT extends DefaultTableModel{

	public jT(){
		//Faked results
		ArrayList<Record> myResults = new ArrayList<Record>();
		myResults.add(new TempResult("One", "U2", "2007-04-01 08:15:02", "3:42"));
		myResults.add(new TempResult("Two had found three fours of five", "The Black Eyed Peas", "2007-04-01 08:19:31", "4:07"));
		myResults.add(new TempResult("Number three", "Jonny Cash", "2007-04-01 08:25:10", "2:58"));
        
        //Tableheaders
        String[] myHeaders = {"Title",
                "Artist",
                "Start",
                "Duration"};
        
        final JTable table = new JTable(myResults, myHeaders);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        
	}

	/**
     * Temporary class that implements Record.
     * For testing only.
     * 
     * @author sCHween
     */
    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;
        }
        
    }

}
Now this line is my problem:
Code:
final JTable table = new JTable(myResults, myHeaders);
Thanks
Felissa
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-04-2007, 10:23 AM
Member
 
Join Date: Jun 2007
Posts: 92
Rep Power: 0
Marcus is on a distinguished road
Default
Here is a nice tutorial on the use of JTables:

This will tell you had to add items, create cells and add them, and how else to mess with the container of a jTable.

Greetings.
Marcus
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 10:25 AM
Member
 
Join Date: Jun 2007
Posts: 95
Rep Power: 0
Felissa is on a distinguished road
Default
hi, think i made a step forward, but i'm still stuck within.
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;
       }
       
   }
The ReporterTabelModel.java looks like this:
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;
     } 
	}

}
Now the problem i got - the getValueAt.
There i need access to my ArrayList results and with the columnIndex i have to read the value.
But i couldn't access my results served from test.java.
Thanks
Felissa
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting JTable (Vectors) Problem ramapple AWT / Swing 6 07-07-2009 12:15 AM
Problem with jTable that is binded with a table in MySQL Database rajkenneth NetBeans 0 03-29-2008 04:36 PM
How to add in a new row in Jtable? Ry4n AWT / Swing 0 01-18-2008 01:26 PM
problem in redrawing JTable abhinav AWT / Swing 0 11-21-2007 10:08 PM
Help with JTable fernando AWT / Swing 1 08-07-2007 07:57 AM


All times are GMT +2. The time now is 10:54 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org