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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Retirieving cell data from a JTable

  • 10-03-2005 4: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