Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable

 
Tom Pelly
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anybody know how to remove a row from a JTABLE?
 
Bala Hari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To remove a row from a Jtable use the flg. method
yourJTable.removeRow(rownum);
 
Jennifer Garrett
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for the help.
 
Tom Pelly
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

-Nate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic