Link to home
Start Free TrialLog in
Avatar of volavy
volavy

asked on

Delete a focued row in jTable

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

DefaultTableModel model =
  (DefaultTableModel)someTable.getModel();
model.removeRow(someTable.getRowSelected());
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of volavy
volavy

ASKER

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);
...
Change

>>
public class MyTableModel extends AbstractTableModel
>>

to

>>
public class MyTableModel extends DefaultTableModel
>>

and you should be fine
Avatar of volavy

ASKER

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)

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.
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?
Avatar of volavy

ASKER

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:
https://www.experts-exchange.com/questions/20403098/jTable-in-JBuilder.html

Regards
/Peter

Ps. I see its more complicated than I thought and raise some points :)
>>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.
Avatar of volavy

ASKER

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