Focus stays on cell when I select column in JTable

Hi,
I am using a JTable and I have defined a function key cntrl-D to perform a function on a selected column. This works fine if I have not yet edited one of the cells in the column, but if I edit any cell and then click the column header and hit cntrl-D, then the column is deselected, the top cell enters edit mode, and my function is called with only one cell selected (always the top cell) instead of the whole column.
When I debug this, I find that my function is called first, and processKeyEvent in the cell editor is called later. The editor doesn't handle it and calls super().
I think that somehow the cell is keeping the focus even though I try to deselect it when the column is selected in my column header listener. Any suggestions welcome.
Here is the code I use to select the column in the column header listener. I have verified that it is called when I click the header:
public void mouseClicked(MouseEvent e)
table.requestFocusInWindow();
Point thePoint = e.getPoint();
int theColumnIndex = table.columnAtPoint(thePoint);
int theLastRowIndex = table.getRowCount() - 1;
if (theLastRowIndex > 0 && theColumnIndex > 0 && theColumnIndex < table.getColumnCount() )
if ( e.isShiftDown() && table.getSelectedRowCount() == table.getRowCount() )
table.editingStopped(null);
if ( table.isEditing() )
return;
if ( theColumnIndex <= mColumnStart )
table.setColumnSelectionInterval( theColumnIndex, mColumnStart);
else if ( theColumnIndex >= mColumnStart )
table.setColumnSelectionInterval( mColumnStart, theColumnIndex );
else if ( e.isControlDown() && table.getSelectedRowCount() == table.getRowCount() )
table.editingStopped(null);
if ( table.isEditing() )
return;
if (table.isColumnSelected(theColumnIndex))
table.removeColumnSelectionInterval(theColumnIndex, theColumnIndex);
else
table.addColumnSelectionInterval(theColumnIndex, theColumnIndex);
else
table.editingStopped(null);
if ( table.isEditing() )
return;
table.clearSelection();
table.setEditingRow(-1);
table.setEditingColumn(-1);
table.setRowSelectionInterval(0, theLastRowIndex);
table.setColumnSelectionInterval(theColumnIndex, theColumnIndex);
mColumnStart = theColumnIndex;
SampleManager.getInstance().setupMenus();

I found the answer. This is a feature that worked in 1.3.1 but broke in 1.4.1. It turns out that there is a new feature in JTable that jumps into edit mode when a key event is detected, and that causes the column selection to be canceled. To inactivate this feature I put the following line into the constructor of my JTable subclass:
putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);

Similar Messages

  • How do I get a  value of a  cell when user selects in a JTable?

    Hi everybody,
    I need some help in order to complete my task. I 've a JTable. When user selects any particular column . I should able to get user selected value. I added a MouseListener to the table. And I 've written the following code - in mousePressed()
    JTable table = (JTable) e.getSource();
    iint col =JTablePartNumbers.getInstance().getSelectedColumn();
    int row = JTablePartNumbers.getInstance().getSelectedRow();
    System.out.println("row col value" +JTableModelPartNumbers.getInstance().getValueAt(row,col));
    I'm getting col and row value but when I'm using TableModel.getValue(row,col) - it is giving me null value.
    And my getValueAt() method is defined as
    public Object getValueAt( int nRow, int nCol )
         System.out.println("the nRow" nRow "And nCol " nCol "And row count" + getRowCount());
         int rowCount = getRowCount();
         System.out.println("the row count" +rowCount);
    if ( ( nRow < 0 ) || ( nRow >= getRowCount() ))
         return null;
              // Taking
              StartNewRevisedGenericSGMLFragmentsModel row = (StartNewRevisedGenericSGMLFragmentsModel) partNumberDetails.elementAt( nRow );
         //     System.out.println("row value" nRow "Col value" +nCol);
              switch ( nCol )
                   case 0:
                   return row.getParNo();                               case 1:
                        return row.getRev(); // gets model data
                   case 2:
                        return row.getDoneDate( );// gets revision data
              Vector rowVec = (Vector) partNumberDetails.get(nRow);
         return (String) rowVec.get(nCol);
    so please help me out resolving this problem.
    Thanks in advance
    purnima

    try calling jtable.getSelectionModel().addListSelectionListener(...)
    by the way, any good design of a generic UI Table must have a feature to do what you're talking about in an easy way. If it didn't, why would anyone use it?

  • Color a cell when i select THAT cell

    Hi!
    I searced for something about my question, but i didn't find anything about it :/
    I have to color one cell of a JTable when i select it and make some operation.
    I tried with this:
    java.awt.Component cell = jTable1.getComponentAt(jTable1.getSelectedColumn(),jTable1.getSelectedRow());
                     cell.setForeground(java.awt.Color.red);But it color all my table and not THE cell that i've selected.
    If i've understood, the overriding of the metods prepareRenderer (or similar, i don't remember its exact name) sets the color of cells under particulare condition, but i think i don't need it: i have to color a cell only when i select it.
    Thank you!

    but i think i don't need it: i have to color a cell only when i select it. Why don't you think you need it. You have a special requirement that you only want to give the cell a specific color when it is selected. So the prepareRenderer() method should do what you want.
    If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.
    Don't forget to use the [Code Formatting Tags|http://forum.java.sun.com/help.jspa?sec=formatting], so the posted code retains its original formatting.

  • Select columns in jtable

    Hello,
    I have a JTable component with 4 columns. I only allow single row selection. If I select a row not all columns are selected. The one you clicked on does not have the selection color. Ho can you select all columns with the selection color ?
    regards
    Johan

    Just guessing because I had a hard time wading though all your code. you probably have a custom renderer on that column. Your renderer needs to change the background color if the cell is selected.

  • Interective Report - Select columns takes forever - APEX32

    I have an interactive report with a query joining two tables. Both tables may have sever hundreds (not many) rows.
    The query runs fast on sql but when on interactive report is run based on same query, and when I click on "Select Columns" to display selected columns, it simply gives that scrolling round circle and just stays there without displaying the select columns wizard window.
    Any Idea to resolve this issue?
    Thanks,
    R

    Hi,
    This might not be problem, but do you see any javascript errors from page ?
    If you create report to totally new page, does it work ?
    Br,Jari

  • Tab focus behavior when searching selected text

    In the previous versions of Firefox, when I selected some text on a webpage, right-clicked the text to get the context menu, and clicked the "Search" option (e.g. Search Google for "my text selection") the currently focused tab would stay focused and a new tab would be launched to handle the appropriate search in the background. Ever since I updated to Firefox 13, the new tab now receives focus when I do a search on selected text.
    How can I switch back to the old behavior? This change in functionality is driving me crazy. When reading a webpage, I often like to select words and phrases about which I want to learn more and launch searches to Google and/or Wikipedia via the context menu. I almost always like to finish reading the current page before going off to read the new tabs. Even if I did intend to stop reading the current page and go to the new tab, it often takes many seconds for the new tab to load and I would rather keep reading the current page and switch to the new tab manually than stare at a blank tab while I wait for it to load.
    In the "Tabs" tab of the options window, there is a checkbox for "When I open a link in a new tab, switch to it immediately." Is there anything like this for new tabs launched by a context menu search?
    Thanks!

    hey bob, you can enter '''about:config''' into the location bar of firefox, confirm the info notification (in case it shows up), search for the preference named '''browser.search.context.loadInBackground''' & toggle it to true by double-clicking it. that should bring back the old behaviour...

  • I am using Windows XP and just installed Elements 12. When I try to select a different brush or shape the drop down list will not stay open long enough to select a different item. I have used Elements 9 for awhile and have not had this problem

    I am using Windows XP and just installed Elements 12. When I try to select a different brush or shape the drop down list will not stay open long enough to select a different item. I have used Elements 9 for awhile and have not had this problem

    grannybel
    It would appear that you are destined for the Adobe Photoshop Elements Forum since your question is related to Photoshop Elements and not Premiere Elements which is the focus program where you have posted.
    But, while the arrangements are being made, here are a few things for you to think about if you have not already
    1. Reset Tools - Select Brush Tool, then
    2. Delete Settings File
    Go to Full Editor/Edit Menu/Preferences/General and click on the option "Reset Preferences on Next Launch".
    We will be watching for your progress.
    Thank you.
    ATR

  • Shading cells when selected by mouse

    How can i get the cells I've selected to become a particular shade? Every time I select a cell and drag over multiple cells the headings change to gray, but the cells stay the same. I would like to be able to see the cells shade into a color when selected by the mouse.

    dodiscom wrote:
    How can i get the cells I've selected to become a particular shade? Every time I select a cell and drag over multiple cells the headings change to gray, but the cells stay the same. I would like to be able to see the cells shade into a color when selected by the mouse.
    Happily, it's not sufficient that one user wish a feature that the designer introduce it.
    You may try to:
    _Go to "Provide Numbers Feedback" in the "Numbers" menu_, describe what you wish.
    Then, cross your fingers, and wait _at least_ for iWork'09
    Yvan KOENIG (from FRANCE jeudi 14 août 2008 12:55:33)

  • (JTable) How can i get the first columns cell in a selected row?

    Hello. I am trying to figure out how i can get the first columns cell within a selected row no matter what cell is selected in that row. I have a class that extends AbstractTableModel which represents the table. Now i have another class that extends DefaultSelectionModel. Each model is added to the JTable via setModel(TableModel dataModel), setSelectionModel(ListSelectionModel newModel). Now i don't understand what i have to return from getMaxSelectionIndex(). Any ides? Thanks.
      class xTableModel extends AbstractTableModel{
         private static String[] cols;
         private Object[][] data;
            public void setTableModel(Object data[][], String[] cols){
             this.cols = cols;
             this.data = data;
            public String getColumnName(int col){
             return cols[col];
            public int getRowCount() {
             return data.length;
            public Object getValueAt(int row, int col) {
             return data[row][col];           
            public void setValueAt(Object value,int row,int col){
             data[row][col]=value;
            public int getColumnCount(){
             return cols.length;
    class ColumnListenerModel extends DefaultListSelectionModel{
         public int getMaxSelectionIndex(){}

    int row = table.getSelectedRow();
    Object data = table.getValueAt(row, 0);

  • Numbers switches sheets when I select cell.

    I've have a problem with Numbers '08, ver 1.0.3 that has persisted across the last two OSX upgrades.  I have a multi-sheet file, and when I select a sheet using the Sheets sidebar, it changes to the selected sheet without a problem.  But when I click on any cell on the selected sheet, Numbers instantly switches me back to the previous sheet.  After I've switched back to the desired sheet a second time, I can select and modify cells as usual.  I have not found any way to get around this problem.
    Thanks, David
    MacBook Pro (13-inch, Mid 2012)
    OSX 10.10.2

    Hi David,
    I don't know the answer your problem but I am amazed that '08 is working at all in Yosemite. If you have files that you care about and want to use in the future I want to encourage you to upgrade to '09. I stuck with '08 for longer than I needed to and was happy when I finally upgraded. '08 files cannot be read by the current Numbers and some people are finding it harder to locate '09 to upgrade their files (Still on Amazon at last report). '09 has strong support on these forums with many preferring it to 3. Make sure you upgrade it to the most current version.
    quinn

  • How to make a cell or column in JTable non focusable.

    I have created a table which contains 2 rows and 2 columns. My requirement is that 2nd column should be non editable and non focusable. Making any cell non editable is very easy but how should i avoid focus on particular cell.i.e., suppose focus is on first cell and now if u press right arrow button on the keyboard 2nd cell should not get highlighted and focus should remain on first cell.
    Thanks in advance

    Override the default Tab Action. Here is an example:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=657819

  • The options to copy/paste/add comment are not appearing when I select a cell, any idea what I have done?

    I was working on a spreadsheet, adding comments (yellow comment area), then I decided to see what these arrows do (< > ) I can see they are used for skipping through the comments. But now when I try to add another comment, the option doesn't appear, as well as when I select a cell, the options to delete, copy, cut, paste, etc. do not appear either...
    Does anyone know what I've done and how to fix it?!

    You may have a malware infection.
    Do a malware check with a few malware scan programs.<br />
    You need to use all programs because each detects different malware.<br />
    Make sure that you update each program to get the latest version of the database.
    *http://www.malwarebytes.org/mbam.php - Malwarebytes' Anti-Malware
    *http://www.superantispyware.com/ - SuperAntispyware
    *http://www.safer-networking.org/en/index.html - Spybot Search & Destroy
    *http://www.lavasoft.com/products/ad_aware_free.php - Ad-Aware Free
    *http://www.microsoft.com/windows/products/winfamily/defender/default.mspx - Windows Defender: Home Page
    See also "Spyware on Windows": http://kb.mozillazine.org/Popups_not_blocked and [[Searches are redirected to another site]]
    If you can't fix it with the above listed scanners then you need to ask advise on one of the forums that specialize in malware removal mentioned in the <i>Popups_not_blocked</i> article.

  • Selecting cell when invoke popup

    I have a table where the user can select the table cells with a mouse click, if they do a right click its brings up a PopupMenu but this doesnt effect what cells have been selected.
    Ive looked at Microsoft Spreadsheet and if a right click is done over a cell that has been selected, all the cells selected remains but if it is clicked over a cell that is not selected then the cell that has just been clicked on becomes the current cell instead.
    This seems a better way of doing it., so does anyone know how I can achieve this prefereably in a way that is both OS independent and input device independent.
    Thanks Paul

    Thanks
    Ive posted my soln based on your info here. Its maybe more complicated then would uusually be required because my table allows row and column sorting.
    protected void maybeShowPopup(MouseEvent e)
            /** Get selected cells
             *  have we clicked over area covered by selected cells
             *  if not we then reset to currently covered cell
            if (e.isPopupTrigger())
                try
                    int row = table.rowAtPoint(e.getPoint());
                    int col = table.columnAtPoint(e.getPoint());
                    CellLocation cell
                        = new CellLocation( ( (TableSorter) table.getModel()).getRowAt(row),
                                           table.getColumnModel().getColumn(col).getModelIndex());
                    ArrayList cells = ( (TableSorter) table.getModel()).getSelectedCells();
                    if (!cells.contains(cell))
                         table.changeSelection(row, col, false, false);
                catch (Exception ex)
                    ex.printStackTrace();
                popup.show(e.getComponent(), e.getX(), e.getY());
    class CellLocation
         int row;
         int col;
         public CellLocation(int row, int col)
             this.row = row;
             this.col = col;
         public int getRow()
             return row;
         public int getCol()
             return col;
         public boolean equals(Object o)
             CellLocation otherCell = (CellLocation)o;
             if((otherCell.getRow()==this.getRow())&&
                (otherCell.getCol()==this.getCol())
                 return true;
             return false;
    Table Sorter methods ........
    /** Returns the underlying model row index for a given table row index */
        public int getRowAt(int aRow)
            return  ((Integer) indexes.get(aRow)).intValue();
        /* The mapping only affects the contents of the data rows.
         * Pass all requests to these rows through the mapping array: "indexes".
        public Object getValueAt(int aRow, int aColumn)
            checkModel();
            return model.getValueAt( ( (Integer) indexes.get(aRow)).intValue(), aColumn);
        /** Retrieve a list of the selected cells */
        public ArrayList getSelectedCells()
            ArrayList selectedCells = new ArrayList();
            try
                int[] rows = table.getSelectedRows();
                for (int i = 0; i < rows.length; i++)
                    int[] cols = table.getSelectedColumns();
                    for (int j = 0; j < cols.length; j++)
                        selectedCells.add(new CellLocation( ( (Integer) indexes.get(rows)).intValue()
    , table.getColumnModel().getColumn(cols[j]).getModelIndex()));
    catch (Exception e)
    e.printStackTrace();
    return selectedCells;

  • When sorting a column of alphanumerical, how do I get them to stay sorted?  When I go to the next column, the first column mixes up.

    How do I get a column of alphanumerical to stay sorted?  When I go to the next column, my first column gets mixed up.

    For my purpose, I'll have to go back to Excel.
    What is your purpose?  Note that Numbers 3.0 does not have a multicolumn sort interface the way you have in Excel (or Numbers 2.3). But it is generally easy to accomplish what you need to do.
    Say you have a column A on which you want an alphanumerical sort and within that you want to sort by B (the equivalent of Excel's "Sort by" A "Then by" B).
    First do a single column sort by B. Then do a single column sort by A.
    SG

  • How can I eliminate grid lines of selected cells when printing, in iOS numbers

    How can I eliminate grid lines of selected cells when printing, using iOS numbers

    Hi Cmlgfx,
    Thanks for visiting Apple Support Communities.
    You can style a table to remove grid lines and only show an "outline" around the table itself.
    See this article for help with table styles:
    Numbers for iOS (iPad): Place and style tables
    http://support.apple.com/kb/PH3365
    To show or hide the table name, table border, and grid lines, alternate row colors, or adjust fonts, tap Table Options at the bottom of the Table window (you may need to scroll up in the window to see it), and then set your preferences using the options in the Table Options window. (When you’re done, tap at the top of the window to see other options.)
    All the best,
    Jeremy

Maybe you are looking for

  • Error message when I try to Forward mail. Please help.

    I tried forwarding an email to my wife, but couldn't, and I received the following error message: "The message could not be appended to the outbox." This is the first time I've had this happen, and first time I've ever seen this message. I have no id

  • How to migrate sites and blogs made with iWeb 1.1.2 into MobileMe?

    How to republish or migrate sites and blogs made with iWeb 1.1.2 into MobileMe? I can not find instructions for that, and when trying to publish into .Mac, I consistently get an error message. The sites would look better as they are, but ...

  • Time out error while fetching records from table BKPF

    Hi, I am fetching records from table BKPF using BUKRS & AWKEY in where clause. Query is as follows:     SELECT BELNR  XBLNR  AWKEY     FROM   BKPF     INTO TABLE L_I_BKPF_TEMP     PACKAGE SIZE 500     WHERE BUKRS LIKE L_C_EG     AND   AWKEY IN L_R_AW

  • Rotate in Slideshow

    I'd like to be able to use an auto-advance slideshow for reviewing my photos. I've got one set up, and from it I can add star ratings to the images as I see them. However, the rotate keys don't work. Instead, they rotate the currently selected pictur

  • JNI Method to find the Windows app that owns a serial port?

    Im trying to write a native method that will tell me the app. name or PID of the Windows application that owns a com port. Any Ideas?