File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable" Watch "JTable" New topic


Tom Pelly

Joined: Jun 29, 2001
Posts: 14
Does anybody know how to remove a row from a JTABLE?
Bala Hari

Joined: Nov 14, 2001
Posts: 7

To remove a row from a Jtable use the flg. method
Jennifer Garrett

Joined: Oct 31, 2001
Posts: 13
I think you need to use DefaultTableModel for adding and removing rows. Your code might be something like this:
String[] mycolumns = {"column 1 title", "column 2 title"};
String[][] mydata = {{"foo", "bar"}, {"aaa", "bbb"}};
DefaultTableModel mytableModel = new DefaultTableModel(mydata, mycolumns);
JTable mytable = new JTable(mytableModel);
JScrollPane myscrollpane = new JScrollPane(mytable);
// ... somehwere later, whenever you want to remove a row...
int rowNumber = 0;
mytableModel.removeRow(rowNumber); // remove the first row
Tom Pelly

Joined: Jun 29, 2001
Posts: 14
Thanks alot for the help.
Tom Pelly

Joined: Jun 29, 2001
Posts: 14
Need a bit more help on this JTable please...
Is there any way to set the widths of the colums? So if I wanted the first colum to start up twice the width of the second (without manually having to drag it), how would I do that?
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

You'll need to mess with the TableColumnModel.

First, get the TableColumnModel.

Then, get the width of the first column.

Then, set the width of the second column.

And you're done!


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: JTable
It's not a secret anymore!