Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can anyone suggest me a good tutorial on Abstract Table Model

  1. #1
    Junior Member Johnny Bravo's Avatar
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Can anyone suggest me a good tutorial on Abstract Table Model

    Hi I am thinking of making a spreadsheet like program with JTable using custom Table Model. I googled for tutorials but most of them are related with sql which I don't really understand at this level of learning java. I read the oracle's tutorials on jtable but those tutorials don't explain how to implement abstract table Model in much greater detail as you'd expect for a newbie.

    any help?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Can anyone suggest me a good tutorial on Abstract Table Model

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames = ...//same as before...
        private Object[][] data = ...//same as before...
     
        public int getColumnCount() {
            return columnNames.length;
        }
     
        public int getRowCount() {
            return data.length;
        }
     
        public String getColumnName(int col) {
            return columnNames[col];
        }
     
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
     
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
     
        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }
     
        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
        ...
    }

    All this code is from the Oracle tutorial, and it is pretty much all there is to using AbstractTableModel.
    The could should be made clearer with the @Override annotation to show that all those methods are inherited.

    setRowCount() etc are just meta data which need to be filled in.
    The area concerned with setting cell data is setValueAt(), where you update the data, then update the table using the inherited fireTable methods, which you do not need to fill in or alter.

    If you still don't get it, or have more specific issues, please say so.
    Last edited by newbie; August 31st, 2012 at 11:28 AM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Good Java Networking Recourse\Tutorial\Book
    By Dark Light in forum Java Networking
    Replies: 4
    Last Post: June 1st, 2024, 02:14 PM
  2. Good Swing Tutorial
    By NastyPasty in forum AWT / Java Swing
    Replies: 2
    Last Post: June 12th, 2012, 02:25 PM
  3. GUI error: is not abstract and does not override abstract method
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2011, 01:26 PM
  4. creating table model....
    By kollyisrealisaac in forum Java Theory & Questions
    Replies: 1
    Last Post: May 6th, 2011, 09:07 AM
  5. Good Beginner AWT tutorial
    By helloworld922 in forum AWT / Java Swing
    Replies: 2
    Last Post: August 4th, 2009, 10:31 AM