Results 1 to 3 of 3
  1. #1
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    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:
    Java 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:
    Java Code:
    final JTable table = new JTable(myResults, myHeaders);
    Thanks
    Felissa:p

  2. #2
    Marcus is offline Member
    Join Date
    Jun 2007
    Posts
    92
    Rep Power
    0

    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:cool:

  3. #3
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    hi, think i made a step forward, but i'm still stuck within.
    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;
           }
           
       }
    The ReporterTabelModel.java looks like this:
    Java 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:p

Similar Threads

  1. Sorting JTable (Vectors) Problem
    By ramapple in forum AWT / Swing
    Replies: 6
    Last Post: 07-06-2009, 11:15 PM
  2. Replies: 0
    Last Post: 03-29-2008, 03:36 PM
  3. How to add in a new row in Jtable?
    By Ry4n in forum AWT / Swing
    Replies: 0
    Last Post: 01-18-2008, 12:26 PM
  4. problem in redrawing JTable
    By abhinav in forum AWT / Swing
    Replies: 0
    Last Post: 11-21-2007, 09:08 PM
  5. Help with JTable
    By fernando in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:57 AM

Posting Permissions

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