Delete a focued row in jTable

volavy
volavy used Ask the Experts™
on
Hi,

I would like to klick on a row in a jTable and then delete this row (the focued row) by klicking a button.

I guess I have to find what row is focued and then delete it. How is this done in java (JBuilder)

/Peter
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
DefaultTableModel model =
  (DefaultTableModel)someTable.getModel();
model.removeRow(someTable.getRowSelected());
Top Expert 2016
Commented:
>>model.removeRow(someTable.getRowSelected());

should be

model.removeRow(someTable.getSelectedRow());

Author

Commented:
Hi,

I used a TableModel purposed by another expert (gkern) and in this tablemodel I cant delete a row in the way you wrote CEHJ. Should I use another tablemodel to be able to delete a row? (I'm pretty new to Java)

 /Peter

gkern gave me this solution on a previous question (only addring rows):


_______________________________________
Hi,
first add JScrollPane to your JFrame/JDialog so u can see the headers.
second use jTable.setModel() method to add your table model (do it in the jbInit() method after initializing the table). use your own TableModel class (this is the best way) - ie implements TableModel interface or extend AbstractTableModel (both in javax.swing.table package).
add addRow(<arguments>) to your model and use it in order to add rows to the table.
this is a small code example:
the table model class:

import javax.swing.tabl.*;

public class MyTableModel extends AbstractTableModel {

 protected Vector data;

 protected int colCount = 3;

 public MyTableModel() {
   data = new Vector();
 }

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

 public int getColumnCount() {
   return colCount;
 }

 public Object getValueAt(int r, int c) {
   return ((Object[])data.elementAt(r))[c];
 }

 public void addRow(Object[] newRow) {
   data.addElement(newRow);
   this.fireTableDataChanged();
 }

} // MyTableModel

in your jbinit() method:

private void jbInit() throws Exception {
...
 jTable1.setModel(new MyTableModel()); // after initializing jTable1
...
}

when adding new row:
...
Object[] newRow = new Object[3]; // an example data row
newRow[0] = "Value 1";
newRow[1] = "Value 2";
newRow[2] = "Value 3";
((MyTableModel)jTable1.getModel()).addRow(newRow);
...
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Top Expert 2016

Commented:
Change

>>
public class MyTableModel extends AbstractTableModel
>>

to

>>
public class MyTableModel extends DefaultTableModel
>>

and you should be fine

Author

Commented:
With the risk of sounding very novice: it didn't work :(

I got a chain of exceptions (se below). Should I maybe solve the tablemodel thing in another way?

Mayby I have to read up a little about tables :/

/Peter

java.lang.NullPointerException
 at jtabletestApp.MyTableModel.getRowCount(MyTableModel.java:16)
 at javax.swing.table.DefaultTableModel.setNumRows(DefaultTableModel.java:33
2)
at
javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:100)
 at
javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:62)
 at jtabletestApp.MyTableModel.<init>(MyTableModel.java:11)
 at jtabletestApp.Frame1.jbInit(Frame1.java:51)
 at jtabletestApp.Frame1.<init>(Frame1.java:40)
 at jtabletestApp.AppClass.<init>(AppClass.java:20)
 at jtabletestApp.AppClass.main(AppClass.java:49)

Top Expert 2016

Commented:
Not quite sure why that's happening at the moment actually. As a temporary measure, try changing

>>protected Vector data;

to

protected Vector data = new Vector();

and comment out your constructor.
Top Expert 2016

Commented:
Forget that - it doesn't work. Can you remind me of why you got this code in the first place - perhaps you can post the link to the original?

Author

Commented:
I actually didn't have any requirements on the functionality except that I want to add rows, and then delete a specific focused row. I use JBuilder, maybe thats complicates it, or?

The first question was posted on:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20403098.html

Regards
/Peter

Ps. I see its more complicated than I thought and raise some points :)
Top Expert 2016

Commented:
>>I use JBuilder, maybe thats complicates it, or?

No.

I looked back through the other link. If you'd used the code i posted there together with DefaultTableModel and my recent code, all should work.

Author

Commented:
Ok but I really cant make it work. When I change:

>>
public class MyTableModel extends AbstractTableModel
>>

to

>>
public class MyTableModel extends DefaultTableModel
>>

nothing works. I cant even add rows to the jTable..


/Peter

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial