Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

help with JTable and Table Models

Thread view: 
Richard - 11 Nov 2005 19:04 GMT
Hi all

I am having a bit of trouble with my TableModel class that extends
AbstractTableModel. My code so far is

------------------------------------------------------------------------
------------------------------------------------------------------------

public class ResultsTableModel extends AbstractTableModel {

    private Vector columnHeaders;
    private Vector data;

    public ResultsTableModel(Vector data, Vector columnHeaders) {
        this.data = data;
        this.columnHeaders = columnHeaders;
    }

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

    public int getColumnCount() {
        return columnHeaders.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return ((Vector) data.get(rowIndex)).get(columnIndex);
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

public static class Test extends TestCase {
    public void testTableModel(){
        Vector rows = new Vector();
        Vector row = new Vector();
        row.add("1a");
        row.add("1b");
        row.add("1c");
        rows.add(row);

        Vector row2 = new Vector();
        row2.add("2a");
        row2.add("2b");
        row2.add("2c");
        rows.add(row2);

        Vector columns = new Vector();
        columns.add("a");
        columns.add("b");
        columns.add("c");

        ResultsTableModel model = new ResultsTableModel(rows, columns);
        assertEquals(2, model.getRowCount());
        assertEquals(3, model.getColumnCount());
        assertEquals("1a", model.getValueAt(0,0));
        assertEquals("2a", model.getValueAt(1,0));
    }
} //of test
}

------------------------------------------------------------------------
------------------------------------------------------------------------

When I construct this table model and pass this into the JTable
constructor I lose the table column headers values. All the headers are
just a,b,c... (the default I presume). If I just pass the vectors into
the JTable constructor and bypass the TableModel then I get the table
headers correct. ie not a,b,c but header1, header2...

I have looked at the AbstractTableModel class to try and figure out
what bit of wiring up I am missing but cant figure it out. Has anyone
got any ideas? Im sure it must be simple

Thanks

Richard
VisionSet - 11 Nov 2005 19:22 GMT
> All the headers are
> just a,b,c... (the default I presume). If I just pass the vectors into
> the JTable constructor and bypass the TableModel then I get the table
> headers correct. ie not a,b,c but header1, header2...

You need to implement

getColumnName(int column)

--
Mike W
Richard - 17 Nov 2005 20:02 GMT
> > All the headers are
> > just a,b,c... (the default I presume). If I just pass the vectors
[quoted text clipped - 4 lines]
>
> getColumnName(int column)

Thanks for your reply. Sorry it took me so long.

I did  'setAutoCreateColumnsFromModel(false);' and this worked
       


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.