Find a record in a Vector?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Find a record in a Vector?

  1. #1
    Lim Wing Hoe Guest

    Find a record in a Vector?

    OK,
    I've tried hiding the particular column from the JTable by setting the
    column's width to 0. However, the column still displayed.
    I'm going to try this, when a user clicks on a record, find out what's the
    rowIndex, and based on that do a search on the Vector that was used in
    building the records for the JTable.

    The question is, how can I search inside the Vector to get the required
    record?

    Codes please....

    Thanks.

    --



    Best Regards,
    Wing Hoe
    ---------------------------------------------------------------
    Email: winghoe@hotmail.com
    ICQ: 2213281
    www: http://pwp.maxis.net.my/winghoe
    Job Seeking Status : Active
    Availability : May 2001 (after degree course finish)
    ---------------------------------------------------------------





  2. #2
    Dave Lorde Guest

    Re: Find a record in a Vector?


    I just had the same problem, where I needed to keep a separate hidden column
    of objects 'in step' with the addition and deletion of table rows.

    I ended up making a table model with only the displayable columns but containing
    a separate vector for my hidden objects. The addRow() method takes an extra
    parameter for the hidden object, and adds it to the 'hidden' vector, and
    the deleteRow() also deletes an object from the 'hidden' vector. This ensures
    that the hidden object vector behaves just like a hidden table column and
    keeps in step with the other columns.

    To get the hidden items out of the table model, I implemented a separate
    getMyObject() method which takes a row index and returns the hidden object
    for that row:

    class MyTableModel extends AbstractTableModel() {
    vector hiddenColumn = new vector();
    ...
    public MyObject getMyObject(int row) {
    return (MyObject)hiddenColumn.elementAt(row);
    }
    ...
    }

    When the user clicks on a table row, I get the row index and then use that
    to pull the correct object out of the table model using the getMyObject()
    method.

    It all boils down to getting the object out of the vector with vector.elementAt(rowIndex).

    Dave


    "Lim Wing Hoe" <winghoe@hotmail.com> wrote:
    >OK,
    >I've tried hiding the particular column from the JTable by setting the
    >column's width to 0. However, the column still displayed.
    >I'm going to try this, when a user clicks on a record, find out what's the
    >rowIndex, and based on that do a search on the Vector that was used in
    >building the records for the JTable.
    >
    >The question is, how can I search inside the Vector to get the required
    >record?
    >
    >Codes please....
    >
    >Thanks.
    >
    >--
    >
    >
    >
    >Best Regards,
    >Wing Hoe
    >---------------------------------------------------------------
    >Email: winghoe@hotmail.com
    >ICQ: 2213281
    >www: http://pwp.maxis.net.my/winghoe
    >Job Seeking Status : Active
    >Availability : May 2001 (after degree course finish)
    >---------------------------------------------------------------
    >
    >
    >
    >



  3. #3
    Dave Lorde Guest

    Re: Find a record in a Vector?


    To hide a column in a normal table without changing the table model data,
    use JTable.removeColumn():

    myTable.removeColumn(myTable.getColumnModel().getColumn(x));

    To get an element out of a vector, use vector.elementAt(index).

    Dave

    "Lim Wing Hoe" <winghoe@hotmail.com> wrote:
    >OK,
    >I've tried hiding the particular column from the JTable by setting the
    >column's width to 0. However, the column still displayed.
    >I'm going to try this, when a user clicks on a record, find out what's the
    >rowIndex, and based on that do a search on the Vector that was used in
    >building the records for the JTable.
    >
    >The question is, how can I search inside the Vector to get the required
    >record?
    >
    >Codes please....
    >
    >Thanks.
    >
    >--
    >
    >
    >
    >Best Regards,
    >Wing Hoe
    >---------------------------------------------------------------
    >Email: winghoe@hotmail.com
    >ICQ: 2213281
    >www: http://pwp.maxis.net.my/winghoe
    >Job Seeking Status : Active
    >Availability : May 2001 (after degree course finish)
    >---------------------------------------------------------------
    >
    >
    >
    >



  4. #4
    Lim Wing Hoe Guest

    Re: Find a record in a Vector?

    So, David,
    you mean that when I remove that particular column from the table, the data
    in that particular column is still intact, just that the column is removed
    from the user's view?

    You see, this is what I'm trying to achieve. I have a record with fields
    like (id, subject, messagebody, sender, date). Theoretically, when I pump
    the data into the JTable, I should hide the field (messagebody) from the
    user. When the user clicks on the row of record, the messagebody will be
    shown in JTextArea() instead.

    My idea is to just get the row selected, get the value of the object at
    messagebody column, pass it to the JTextArea() field.

    JTable.removeColumn() method helps me to accomplish that? Or do I have to
    use the previous answer that you mentioned about creating another hidden
    Vector?

    --



    Best Regards,
    Wing Hoe
    ---------------------------------------------------------------
    Email: winghoe@hotmail.com
    ICQ: 2213281
    www: http://pwp.maxis.net.my/winghoe
    Job Seeking Status : Active
    Availability : May 2001 (after degree course finish)
    ---------------------------------------------------------------


    "Dave Lorde" <david.lorde@ft.com> wrote in message
    news:3a3e25eb$1@news.devx.com...
    >
    > To hide a column in a normal table without changing the table model data,
    > use JTable.removeColumn():
    >
    > myTable.removeColumn(myTable.getColumnModel().getColumn(x));
    >
    > To get an element out of a vector, use vector.elementAt(index).
    >
    > Dave





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center