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 / October 2005

Tip: Looking for answers? Try searching our database.

JTable addRow...

Thread view: 
6e - 20 Oct 2005 16:56 GMT
Hi,

I've been trying to dynamically add a row to an extended JTable class.
Basically when the user completes the previous row, I try to add a new
row.  I have the lojic in place, only I still can't figure out how to
successfully add a row to a jtable...

I've been trying to solve this problem for a while now, unfortunately
it seems I have hit a brick wall.  I've tried many things, but the
general line of thought is what is represented in the code.  That is to
convert it to the defaultTableModel and call addRow.  This does not
work for me. I recieve the following error:

"
Exception in thread "main" java.lang.ClassCastException:
javax.swing.JTable$1
    at myTable.myAddRow(myTable.java:17)
    at test.main(test.java:28)
"

The following code is compilable, and a simplified version of the
problem.

I would appreciate any advice that you may have on how to add and
display a new row to a Jtable without causing it to have a problem.

Thanks.

-------------myTable.java-------------------------
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

public class myTable extends JTable
    implements ListSelectionListener, TableModelListener {

    public myTable(Object[][] data, String[] columnNames){
        super(data,columnNames);
    }

    public void myAddRow (){
        ((DefaultTableModel)this.getModel()).addRow(new Vector());
    }
}

---------------------test.java  (main class)----------
import java.awt.Container;
import javax.swing.JFrame;

public class test {

    static JFrame gJfrWindow;

    public static void main(String[] args) {

        String[] columnNames = {"Format Label", "Role", "Name", "", "", "",
"", ""};
        Object[][] data = {
                    {" ", " ", " ", " ", " ",  " ", " ",  " "},
        };

        gJfrWindow = new JFrame("Jamaican Bobsled");
        Container contentPane = gJfrWindow.getContentPane();

        myTable x = new myTable(data,columnNames);
        //x.myAddRow();

        contentPane.add(x);

        gJfrWindow.setSize(800,575);
        gJfrWindow.setLocation(0,0);
        gJfrWindow.setVisible(true);
        gJfrWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        x.myAddRow();
   
    }
}
Oliver Wong - 20 Oct 2005 20:04 GMT
> Hi,
>
[quoted text clipped - 77 lines]
> }
> }

   The problem is that JTable's getModel() returns a TableModel, and not a
DefaultTableModel, which is why you get your class cast exception. The
TableModel interface doesn't define any way for you to add rows, so you
can't rely on whatever table model JTable is using in the background.
Instead, you should create your own TableModel, and then pass that in when
you construct a new JTable.

   There's a tutorial on creating table models at
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#data

   - Oliver


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



©2009 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.