Removing Selected rows in JTable????

Discussion in 'Java' started by Delali Dzirasa, Jan 19, 2004.

  1. 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
    Delali Dzirasa, Jan 19, 2004
    #1
    1. Advertising

  2. Delali Dzirasa

    Guest

    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" <> wrote in message news:<buhnnf$qhb$>...
    > 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
    , Jan 22, 2004
    #2
    1. Advertising

  3. Delali Dzirasa

    ekowus

    Joined:
    Aug 10, 2007
    Messages:
    1
    Location:
    Iowa
    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());



    ekowus, Aug 11, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tivo Escobar
    Replies:
    1
    Views:
    7,309
    manusa
    Apr 12, 2007
  2. Ricardo Patricio

    Get Rows of JTable

    Ricardo Patricio, May 24, 2004, in forum: Java
    Replies:
    1
    Views:
    7,614
    Corey Brown
    May 24, 2004
  3. steve

    hiding rows in a jtable

    steve, Jan 16, 2005, in forum: Java
    Replies:
    0
    Views:
    898
    steve
    Jan 16, 2005
  4. Tintin92
    Replies:
    1
    Views:
    1,693
    Andrew Thompson
    Feb 14, 2007
  5. Haircuts Are Important
    Replies:
    3
    Views:
    312
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page