Removing Selected rows in JTable????

D

Delali Dzirasa

This is a newbie question, This is the first time that I am using a JTable
and want to know how I can capture the selected rows on the table and them
perform a action such as removing them from the table when a button is
pressed.


I have a GUI that has a JTable on it, also on it is a set of 3 buttons (Add,
Clear, and Remove) the Add adds a new row, the Clear removes all rows, and
the remove is supposed to remove all of the selected rows, which is the one
I am not sure how to implement.



This is a portion of what is found in my main GUI class.

*************************MAIN GUI
CLASS******************************************
final TableRender tablePanel = new TableRender();
...
jPanel7.add(tablePanel, null);


....
//ADD A ROW......works fine
void jButton_Add_actionPerformed(ActionEvent e) {
tablePanel.myTable.AddNewRow();
}

//CLEAR ALL ROWS......works fine
void jButton_Clear_actionPerformed(ActionEvent e) {
tablePanel.myTable.clearRows();
}


//CLEAR Selected Rows......not sure how to implement
void jButton_Remove_actionPerformed(ActionEvent e) {
tablePanel.myTable.removeSelectedRows();
}
*************************MAIN GUI
CLASS******************************************

Need assistance with "removeSelectedRows()" found below
*************************TABLE RENDER
CLASS*************************************
public class TableRender extends JPanel {
private boolean DEBUG = false;
MyTableModel myTable;
JTable table;


public TableRender() {
super(new GridLayout(1,0));
myTable = new MyTableModel();

//Points class is just a class that contains 5 string members....
Vector temp = new Vector();
Points t1 = new Points("name1", "conv1", "stats1", "pd1", "state1",
"fmt1");
Points t2 = new Points("name2", "conv2", "stats2", "pd2", "state2",
"fmt2");

temp.addElement(t1);
temp.addElement(t2);

myTable.update(temp);
JTable table = new JTable(myTable);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

//Add the scroll pane to this panel.
add(scrollPane);
}

class MyTableModel extends AbstractTableModel {
String[] columnNames={};
Vector rows = new Vector();


public int getColumnCount() {
return columnNames.length;
}

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

public String getColumnName(int col)
{
//works fine...
}


public Object getValueAt(int row, int col) {
//works fine...
}

public boolean isCellEditable(int row, int col) {
// works fine....
}

public void setValueAt(Object value, int row, int col) {
// works fine......
}



public void update(Vector PointsData) {
//Works fine
}


public void AddNewRow()
{
Vector item = new Vector();
item.addElement("");
item.addElement("Eng");
item.addElement("None");
item.addElement("");
item.addElement("");
item.addElement("");
rows.addElement(item);

fireTableStructureChanged();
}

public void clearRows()
{
rows.clear();
fireTableStructureChanged();
}

public removeSelectedRows()
{
//???????? not sure how to capture selected rows
//once caputured how do I remove them?
}
}
}
*************************TABLE RENDER
CLASS*************************************


Any sugguestions on how to implement? or can anyone point me to a good
tutorial on this exact matter?


Thanks!

Delali
 
A

adhirmehta41

Hi
u don't need to do that much. u can do it using following steps
1) use int [] getSelectedRows() method of jtable class.
2) whenever your delete or clear button is pressed call above method.
3) after getting the number of selected rows recreate the jtable
excluding the selected one.

this is what i did.

Delali Dzirasa said:
This is a newbie question, This is the first time that I am using a JTable
and want to know how I can capture the selected rows on the table and them
perform a action such as removing them from the table when a button is
pressed.


I have a GUI that has a JTable on it, also on it is a set of 3 buttons (Add,
Clear, and Remove) the Add adds a new row, the Clear removes all rows, and
the remove is supposed to remove all of the selected rows, which is the one
I am not sure how to implement.



This is a portion of what is found in my main GUI class.

*************************MAIN GUI
CLASS******************************************
final TableRender tablePanel = new TableRender();
...
jPanel7.add(tablePanel, null);


...
//ADD A ROW......works fine
void jButton_Add_actionPerformed(ActionEvent e) {
tablePanel.myTable.AddNewRow();
}

//CLEAR ALL ROWS......works fine
void jButton_Clear_actionPerformed(ActionEvent e) {
tablePanel.myTable.clearRows();
}


//CLEAR Selected Rows......not sure how to implement
void jButton_Remove_actionPerformed(ActionEvent e) {
tablePanel.myTable.removeSelectedRows();
}
*************************MAIN GUI
CLASS******************************************

Need assistance with "removeSelectedRows()" found below
*************************TABLE RENDER
CLASS*************************************
public class TableRender extends JPanel {
private boolean DEBUG = false;
MyTableModel myTable;
JTable table;


public TableRender() {
super(new GridLayout(1,0));
myTable = new MyTableModel();

//Points class is just a class that contains 5 string members....
Vector temp = new Vector();
Points t1 = new Points("name1", "conv1", "stats1", "pd1", "state1",
"fmt1");
Points t2 = new Points("name2", "conv2", "stats2", "pd2", "state2",
"fmt2");

temp.addElement(t1);
temp.addElement(t2);

myTable.update(temp);
JTable table = new JTable(myTable);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

//Add the scroll pane to this panel.
add(scrollPane);
}

class MyTableModel extends AbstractTableModel {
String[] columnNames={};
Vector rows = new Vector();


public int getColumnCount() {
return columnNames.length;
}

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

public String getColumnName(int col)
{
//works fine...
}


public Object getValueAt(int row, int col) {
//works fine...
}

public boolean isCellEditable(int row, int col) {
// works fine....
}

public void setValueAt(Object value, int row, int col) {
// works fine......
}



public void update(Vector PointsData) {
//Works fine
}


public void AddNewRow()
{
Vector item = new Vector();
item.addElement("");
item.addElement("Eng");
item.addElement("None");
item.addElement("");
item.addElement("");
item.addElement("");
rows.addElement(item);

fireTableStructureChanged();
}

public void clearRows()
{
rows.clear();
fireTableStructureChanged();
}

public removeSelectedRows()
{
//???????? not sure how to capture selected rows
//once caputured how do I remove them?
}
}
}
*************************TABLE RENDER
CLASS*************************************


Any sugguestions on how to implement? or can anyone point me to a good
tutorial on this exact matter?


Thanks!

Delali
 
Joined
Aug 10, 2007
Messages
1
Reaction score
0
Remove selected JTable rows

You don't even need to recreate the jTable:

DefaultTableModel model = jTable.getModel();
int numRows = jTable.getSelectedRows().length;
for(int i=0; i<numRows ; i++ ) model.removeRow(jTable.getSelectedRow());



Hi
u don't need to do that much. u can do it using following steps
1) use int [] getSelectedRows() method of jtable class.
2) whenever your delete or clear button is pressed call above method.
3) after getting the number of selected rows recreate the jtable
excluding the selected one.

this is what i did.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top