Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Retirieving cell data from a JTable

  • 10-03-2005 04:43PM
    #1
    Registered Users, Registered Users 2 Posts: 1,345 ✭✭✭


    Im having trouble getting data from JTable cells,

    Heres some of the relevant code,

    Table creation ---

    //Creates a custom table model
    TableModel model = new DefaultTableModel(bookArray,headers)
    {
    //Makes the table read-only
    public boolean isCellEditable(int x, int y)
    {
    return false;
    }
    };

    //instantiates table using custom model
    JTable table = new JTable(model);

    // Set selection to first row
    ListSelectionModel selectionModel table.getSelectionModel();

    selectionModel.setSelectionInterval(0, 0);

    //Allow only one item to be selected at once
    selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    //Adds a selection listener to the table
    selectionModel.addListSelectionListener(this);

    ---Handler
    // Handler for list selection changes
    public void valueChanged( ListSelectionEvent event )
    {
    ListSelectionModel lsm = (ListSelectionModel)event.getSource();

    if (lsm.isSelectionEmpty()) {
    JOptionPane.showMessageDialog(null,"Empty");
    }
    else
    {
    int selectedRow = lsm.getMinSelectionIndex();
    JOptionPane.showMessageDialog(null,selectedColumn)
    }

    K so that all works ok, its returning the selected rows index but when i try to take the data from the 3 cells in the row nothing happens(well actually the command prompt fills with lots of ugly looking error code, first line says array out of bounds error)

    Ive tryed a few different techniques

    String author = (String)table.getModel().getValueAt(selectedRow,1);
    String author = (String)table.getValueAt(selectedRow,1);

    etc

    But none of them seem to work. Any suggestions?


Advertisement