Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Retirieving cell data from a JTable

Options
  • 10-03-2005 5:43pm
    #1
    Registered Users 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