CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2003
    Location
    Athens, Greece
    Posts
    1,094

    JTable cell Editing

    Hi gurus,
    I am new to the Java forum (I have C++ background) and I am a bit confused on a swing issue.
    I need to open a JFrame window containing a JTable and have a specific cell open for editing.
    I tried the following code in jbInit()
    Code:
               jTableHighScores.editCellAt(newRank - 1, 2);
                jTableHighScores.getEditorComponent().requestFocus();
    but the specified cell is not open for editing.
    I have to move the mouse on it and doubleclick to open it. What do I miss?

    Also I need to capture the <enter> when the user finishes editing with the cell and then do smthng with the new value. How do I go about it?

    Thank you
    Extreme situations require extreme measures

  2. #2
    Join Date
    Jun 2005
    Posts
    33

    Re: JTable cell Editing

    You can edit the table cells in the following way :-
    Code:
    // Create table
        int rows = 10;
        int cols = 5;
        JTable table = new JTable(rows, cols);
        
        // Enable the ability to select a single cell
        table.setColumnSelectionAllowed(true);
        table.setRowSelectionAllowed(true);
        
        // Set the cell on the 2nd row, 4th column in edit mode
        int row = 1;
        int col = 3;
        boolean success = table.editCellAt(row, col);
        if (success) {
            // Select cell
            boolean toggle = false;
            boolean extend = false;
            table.changeSelection(row, col, toggle, extend);
        } else {
            // Cell could not be edited
        }
    Then capture the 'enter' key event with the below code:

    Code:
    table.getModel().addTableModelListener(new MyTableModelListener(table));
        
        public class MyTableModelListener implements TableModelListener {
            JTable table;
        
            // It is necessary to keep the table since it is not possible
            // to determine the table from the event's source
            MyTableModelListener(JTable table) {
                this.table = table;
            }
        
            public void tableChanged(TableModelEvent e) {
                int firstRow = e.getFirstRow();
                int lastRow = e.getLastRow();
                int mColIndex = e.getColumn();
        
                switch (e.getType()) {
                  case TableModelEvent.UPDATE:
                    if (firstRow == TableModelEvent.HEADER_ROW) {
                        if (mColIndex == TableModelEvent.ALL_COLUMNS) {
                            // A column was added
                        } else {
                            // Column mColIndex in header changed
                        }
                    } else {
                        // The rows in the range [firstRow, lastRow] changed
                        for (int r=firstRow; r<=lastRow; r++) {
                            // Row r was changed
        
                            if (mColIndex == TableModelEvent.ALL_COLUMNS) {
                                // All columns in the range of rows have changed
                            } else {
                                // Column mColIndex changed
                            }
                        }
                    }
                    break;
                }
            }
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured