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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.get Model()).a ddRow(newR ow);
...
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(
}
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.get
...
Change
>>
public class MyTableModel extends AbstractTableModel
>>
to
>>
public class MyTableModel extends DefaultTableModel
>>
and you should be fine
>>
public class MyTableModel extends AbstractTableModel
>>
to
>>
public class MyTableModel extends DefaultTableModel
>>
and you should be fine
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.NullPointerExcep tion
at jtabletestApp.MyTableModel .getRowCou nt(MyTable Model.java :16)
at javax.swing.table.DefaultT ableModel. setNumRows (DefaultTa bleModel.j ava:33
2)
at
javax.swing.table.DefaultT ableModel. <init>(Def aultTableM odel.java: 100)
at
javax.swing.table.DefaultT ableModel. <init>(Def aultTableM odel.java: 62)
at jtabletestApp.MyTableModel .<init>(My TableModel .java:11)
at jtabletestApp.Frame1.jbIni t(Frame1.j ava:51)
at jtabletestApp.Frame1.<init >(Frame1.j ava:40)
at jtabletestApp.AppClass.<in it>(AppCla ss.java:20 )
at jtabletestApp.AppClass.mai n(AppClass .java:49)
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.NullPointerExcep
at jtabletestApp.MyTableModel
at javax.swing.table.DefaultT
2)
at
javax.swing.table.DefaultT
at
javax.swing.table.DefaultT
at jtabletestApp.MyTableModel
at jtabletestApp.Frame1.jbIni
at jtabletestApp.Frame1.<init
at jtabletestApp.AppClass.<in
at jtabletestApp.AppClass.mai
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.
>>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?
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 :)
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.
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.
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
>>
public class MyTableModel extends AbstractTableModel
>>
to
>>
public class MyTableModel extends DefaultTableModel
>>
nothing works. I cant even add rows to the jTable..
/Peter
(DefaultTableModel)someTab
model.removeRow(someTable.