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
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