JTable help

Discussion in 'Java' started by barichards21@gmail.com, Jun 5, 2007.

  1. Guest

    I'm pretty new programming with swing and I'm running into a problem
    setting up a table. I'm using the DefaultTableModel instead of
    setting up my own because I don't need to do anything too complex with
    it. I'm having a problem though when I try to remove rows. For
    example if I remove the first row, the information shows up correctly
    on my table and shifts everything up. But if I then try to reference
    the information in the first row it gives me the information of the
    row I just deleted. I'm sure it's something stupid but I can't seem
    to figure it out. Here's the code I'm using at the moment:

    instantiating the table:

    tblPhoneNumbers = new JTable(new DefaultTableModel(
    new Object[][]{{"",""}},
    new Object[]{"Number", "Type"})
    );


    action to remove the selected row:

    protected void btnRemovePhoneActionPerformed(ActionEvent evt) {
    DefaultTableModel phoneDm = (DefaultTableModel)
    tblPhoneNumbers.getModel();
    int selectedRow = tblPhoneNumbers.getSelectedRow();
    int numRows = tblPhoneNumbers.getRowCount();
    if(selectedRow >= 0 && selectedRow < numRows-1)
    {
    phoneDm.removeRow(selectedRow);
    }
    }
     
    , Jun 5, 2007
    #1
    1. Advertising

  2. SadRed Guest

    On Jun 5, 11:49 pm, wrote:
    > I'm pretty new programming with swing and I'm running into a problem
    > setting up a table. I'm using the DefaultTableModel instead of
    > setting up my own because I don't need to do anything too complex with
    > it. I'm having a problem though when I try to remove rows. For
    > example if I remove the first row, the information shows up correctly
    > on my table and shifts everything up. But if I then try to reference
    > the information in the first row it gives me the information of the
    > row I just deleted. I'm sure it's something stupid but I can't seem
    > to figure it out. Here's the code I'm using at the moment:
    >
    > instantiating the table:
    >
    > tblPhoneNumbers = new JTable(new DefaultTableModel(
    > new Object[][]{{"",""}},
    > new Object[]{"Number", "Type"})
    > );
    >
    > action to remove the selected row:
    >
    > protected void btnRemovePhoneActionPerformed(ActionEvent evt) {
    > DefaultTableModel phoneDm = (DefaultTableModel)
    > tblPhoneNumbers.getModel();
    > int selectedRow = tblPhoneNumbers.getSelectedRow();
    > int numRows = tblPhoneNumbers.getRowCount();
    > if(selectedRow >= 0 && selectedRow < numRows-1)
    > {
    > phoneDm.removeRow(selectedRow);
    > }
    > }


    > if I then try to reference
    > the information in the first row

    I suspect the code is wrong.
     
    SadRed, Jun 5, 2007
    #2
    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. C-man
    Replies:
    12
    Views:
    3,754
    Roedy Green
    Oct 14, 2003
  2. T.R. Diaz
    Replies:
    0
    Views:
    1,603
    T.R. Diaz
    Nov 8, 2003
  3. Tivo Escobar
    Replies:
    1
    Views:
    7,555
    manusa
    Apr 12, 2007
  4. Tintin92
    Replies:
    1
    Views:
    1,813
    Andrew Thompson
    Feb 14, 2007
  5. Haircuts Are Important
    Replies:
    3
    Views:
    415
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page