JTable - set Selected row?

How do you set the current selected row and highlight it?

You use the public ListSelectionModel getSelectionModel()
method of your JTable. Once you have the ListSelectionModel you can use public void setSelectionInterval(int index0, int index1) to select one or more rows.
Depending on your selection mode you may or may not be able to select multiple rows. This is set by the ListSelectionModel's public int getSelectionMode() and public int setSelectionMode(int SelectionMode) methods.
Hope it helps.
Greg

Similar Messages

  • Setting selected row in JTable

    I'm currently doing a JTable with button functions like Move Up and Move Down.
    I use the following code to swap the details of the 2 rows.
    int row = table.getSelectedRow();
                         int newRow = row -1;
                         Object date = table.getValueAt(row, 0);
                         Object time = table.getValueAt(row, 1);
                         Object isFree = table.getValueAt(row, 2);
                         Object date2 = table.getValueAt(newRow,0);
                         Object time2 = table.getValueAt(newRow,1);
                         Object isFree2 = table.getValueAt(newRow,2);
                                //Object [][] data = { {date, time, isFree} };
                            //Object [][] data2 = { {date2, time2, isFree2} };
                         table.setValueAt(date, newRow, 0);
                         table.setValueAt(time, newRow, 1);
                         table.setValueAt(isFree, newRow, 2);
                         table.setValueAt(date2, row, 0);
                         table.setValueAt(time2, row, 1);
                         table.setValueAt(isFree2, row, 2);my question is how do I make it so that the row I selected to move up, remains selected?
    Right now my problem is:
    When I select row 3 and click move up, the selected row remains at row 3.
    I want this:
    When I select row 3 and click move up, the selected row changes to row 2.
    I know something like the setRowSelectionInterval thing, but I don't know how to use it and if it actually helps. Any help here would be greatly apprecited

    I think I got it.
    public void setRowSelectionInterval(int index0,
    int index1)
    and then
    table.setRowSelectionInterval(newRow, newRow);
    am I right?

  • How to find out if JTable's selected row is visible?

    Hello there,
    Given:
    a JTable is inserted into a JScrollPane and the number of rows in the table is greater than the vieport size.
    A random row within the table gets programmatically selected.
    How to find out if the selected row is visible in a JTable visible area?
    Your help will be greatly appreciated.
    Tim

    That will make the row visible, but not answer whether it was visible
    in the first place. Try something like:
    public boolean isRowVisible( JTable table, int row ) {
        Rectangle rect = table.getBounds();
        int rowHeight = table.getRowHeight();
        int viewHeight = table.getParent().getHeight();
        int max = rect.y - viewHeight + 1;
        int rowPos = - rowHeight * row;
        return ( rect.y >= rowPos && rowPos > max );
    }assuming all rows have the same height.
    : jay

  • ALV - How to set selected rows into internal table

    Hi all,
    I am tying to set with an 'X' flag the selected rows in my ALV using an internal table that contains all rows showed.
    More exactly I have tried to follow these steps.
    1) I have added the filed SEL (type SEL) to my ALV structure zrt_bo_slabsend and I defined my internal table in this way
         DATA: gt_report TYPE STANDARD TABLE OF zrt_bo_slabsend,
                    gw_report TYPE zrt_bo_slabsend.
    2) I have set gw_layo-box_fname = 'SEL' to gw_layo (ALV layout)
    Pressing the "delete button" that I have insert on the top of the ALV, I intercept correctly my user command and I call a form (named "delete_lines") where I have this abap code
    FORM delete_lines.
        DATA: l_subrc          LIKE sy-subrc,
            lw_grid_settings LIKE lvc_s_glay.
    gw_layo-box_fname         = 'SEL'.
                                     "set field name to store row selection
      gw_layo-edit              = 'X'. "makes whole ALV table editable
      gw_layo-zebra             = 'X'.
    gv_repid = sy-repid.
      call function 'REUSE_ALV_GRID_DISPLAY_LVC'
      exporting
          i_callback_program       = gv_repid
         i_callback_pf_status_set = gc_status
         i_callback_user_command  = gc_user_command
         is_layout_lvc            = gw_layo
         it_fieldcat_lvc          = gw_fkat
         i_grid_settings          = lw_grid_settings
          i_save                   = 'X'
          is_variant               = variant
          it_events                = gt_events
        TABLES
          t_outtab                 = gt_report
        EXCEPTIONS
          program_error            = 1
          OTHERS                   = 2.
      LOOP AT gt_report ASSIGNING <report> WHERE SEL = 'X'.
        DELETE gt_report.
      ENDLOOP.
    ENDFORM.
    I'd like to select many rows in my ALV report, than by pressing the delete button I'd like to see a refresh of my ALV, without the selected rows. I want to save it only at the end of my action, only by pressing the save button.
    Any suggestion about the abap code will be well appreciated.
    Thanks in advance for your kind help.
    Regards.
        Giovanni

    Hi Giovanni,
    I am using the method:-
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
    *   I_INTERFACE_CHECK                 = ' '
    *   I_BYPASSING_BUFFER                =
    *   I_BUFFER_ACTIVE                   =
       I_CALLBACK_PROGRAM                = gd_REPID
       I_CALLBACK_PF_STATUS_SET          = 'UDIT'
       I_CALLBACK_USER_COMMAND           = 'USER_COMMAND'
    *   I_CALLBACK_TOP_OF_PAGE            = ' '
    *   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
    *   I_CALLBACK_HTML_END_OF_LIST       = ' '
    *   I_STRUCTURE_NAME                  =
    *   I_BACKGROUND_ID                   = ' '
       I_GRID_TITLE                      = 'Mainatin cell entry'
    *   I_GRID_SETTINGS                   =
       IS_LAYOUT_LVC                     = GS_LAYOUT
       IT_FIELDCAT_LVC                   = I_FIELDCAT[]
    *   IT_EXCLUDING                      =
    *   IT_SPECIAL_GROUPS_LVC             =
    *   IT_SORT_LVC                       =
    *   IT_FILTER_LVC                     =
    *   IT_HYPERLINK                      =
    *   IS_SEL_HIDE                       =
       I_DEFAULT                         = 'X'
    *   I_SAVE                            = 'X'
    *   IS_VARIANT                        =
       IT_EVENTS                         =
       IT_EVENT_EXIT                     =
    *   IS_PRINT_LVC                      =
    *   IS_REPREP_ID_LVC                  =
       I_SCREEN_START_COLUMN             = 30
       I_SCREEN_START_LINE               = 14
       I_SCREEN_END_COLUMN               = 120
       I_SCREEN_END_LINE                 = 25
    *   IT_EXCEPT_QINFO_LVC               =
    *   I_HTML_HEIGHT_TOP                 =
    *   I_HTML_HEIGHT_END                 =
    * IMPORTING
    *   E_EXIT_CAUSED_BY_CALLER           =
    *   ES_EXIT_CAUSED_BY_USER            =
      TABLES
        T_OUTTAB                          = IT_ZCP_DEMAND_SYS1
    * EXCEPTIONS
    *   PROGRAM_ERROR                     = 1
    *   OTHERS                            = 2
    IF SY-SUBRC <> 0.
    * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    I have made five buttons on my toolbar (add, delete, save, back, exit).
    Kindly tell me how can I catch the sy-ucomm of these buttons.
    NOTE: this FM is written inside an user-exit, so I cannot make forms.
    Thanks in advance.

  • JTable set single row color

    I have alternating colors for my jtable, and when a row is selected i pop up another window and clear the selection. i want to change the color of all selected rows, but if i dont clear the selection, it doesn't allow me to re-open the same record because of the way the selectionListener works. so basically i dont want to mark a row as selected, i jus want to change its color. anyone know how to do this?

    looks promising.. i'm going to give it a shot. why dont they just have a method like.. table.setRowColor (rowIndex), because im implementing a selectionlistener, so i already know the rowIndex that was selected, jus have to change its color. maybe coz that would be tooo easy lol. thanks

  • Help to set selected row in adf table!

    Hello,
    I have a problem with adf table, when I select one row in table, I want to set selecte to next row or first row , or end row . . .
    How can I do that.
    If anyone know it, please help me
    Thanks in advance.

    <af:table value="#{bindings.BuddyView1.collectionModel}" var="row" partialTriggers="id"
    rows="#{bindings.BuddyView1.rangeSize}"
    emptyText="#{bindings.BuddyView1.viewable ? 'No data to display.' : 'Access Denied.'}"
    fetchSize="#{bindings.BuddyView1.rangeSize}"
    rowBandingInterval="0"
    selectedRowKeys="#{bindings.BuddyView1.collectionModel.selectedRow}"
    *selectionListener="#{bindings.BuddyView1.collectionModel.makeCurrent}"*
    rowSelection="single" binding="#{backing_untitled1.t1}"
    id="t1">
    You will have to create your own selection listener method.
    Cheers
    Venkat
    Edited by: Venkat81 on Jun 10, 2010 11:12 AM

  • JTable and selected row

    Hi,
    I'm new to Java, I'm writing a simple application using Swing, I've almost finished, almost, becose I have a jTabbedPane on 1st pane some labels and text fields, and on the second one jTable, I have to do 2 things:
    When row in JTable is selected, then the same user data should bevisible in JTabbedPane/Panel1 and if I select someon in panel1 then also the person in the jTable should be selected, what should I do? What's the function name? Please help.

    http://java.sun.com/docs/books/tutorial/uiswing/events/eventsandcomponents.html

  • JTable: re-select rows

    I have a JTable with many rows and X number of cols.
    The user selects some rows.
    I call getSelectedRows() to find out what rows have been selected.
    I do some stuff and sort the rows. This loses the selection and the selected rows can be different numbers now.
    How can I re-select the right rows....there is no setRowSelection(Object o).

    You'll need to figure out how to map from original to new row indices. Then use javax.swing.JTable.addRowSelectionInterval(int, int) to reselect. The Java tutorial provides a class called TableSorter that handles sorting and maintains an index map.

  • How to set selected row on startup of an alv tree build with cl_salv_tree

    HI,
    I have a cl_salv_tree and want the tree structure to open a specific node when starting the report. My coding is something like that:
      DATA: lr_selections       TYPE REF TO cl_salv_selections_tree,
            lr_nodes            TYPE REF TO cl_salv_nodes,
            lr_node             TYPE REF TO cl_salv_node,
            lv_node_index(12)   TYPE c,
            lt_nodes            TYPE salv_t_nodes,
            ls_nodes            TYPE salv_s_nodes,
            lr_item             TYPE REF TO cl_salv_item.
    lr_selections = lr_tree->get_selections( ).
    lt_nodes = lr_selections->get_selected_nodes( ).
    lr_item = lr_selections->get_selected_item( ).
    IF lr_item IS NOT BOUND.
      lr_nodes = lr_tree->get_nodes( ).
      lr_node = lr_nodes->get_node( lv_node_index ).  "lv_node_index is the index of the node i want to be open on startup
      ls_nodes-key = lv_node_index.
      ls_nodes-node = lr_node.
      APPEND ls_nodes TO lt_nodes.
      lr_selections->set_selected_nodes( lt_nodes ).
    ELSE.
      ls_nodes-node = lr_item->get_node( ).
      ls_nodes-key = ls_nodes-node->get_key( ).
      APPEND ls_nodes TO lt_nodes.
      lr_selections->set_selected_nodes( lt_nodes ).
    ENDIF.
    This coding is also used, when the report is running and someone clicks an a row. So thats why I check if a item is marked
    or the total row.
    But the tree always starts without opening the node.
    Do I use the right methods or am I totally wrong ????
    Thx for your help in advance
    dinodini
    Edited by: dinodini on Nov 16, 2010 6:51 PM

    What you are looking for is the similar functionality which could be achieved by the method EXPAND_NODE of the ALV Tree created using CL_GUI_ALV_TREE. In SALV Tree, you can use the method EXPAND of the class CL_SALV_NODE to show all the subnodes of that node, not by using the SELECTION.
    Once you add the Node using the method ADD_NODE, you should call the method EXPAND to expand that subtree.
                lr_nodes  = lr_tree->get_nodes( ).
                lr_node =  lr_nodes->ADD_NODE( ... )
                lr_node->EXPAND( COMPLETE_SUBTREE = 'X' ).
    Regards,
    Naimesh Patel

  • How to set selected row in alv display?

    Hi,
    How do I select some particular row in alv - say at first display i want to highlight 4th row. How can i do that?
    Thanks,
    Sheel

    try to use method set_selected_rows just after method for first display.

  • How to select rows in the inner JTable rendered in an outer JTable cell

    I have wrriten the following code for creating cell specific renderer - JTable rendered in a cell of a JTable.
    table=new JTable(data,columnNames)
    public TableCellRenderer getCellRenderer(int row, int column)
    if ((row == 0) && (column == 0))
    return new ColorRenderer();
    else if((row == 1) && (column == 0))
    return new ColorRenderer1();
    else
    return super.getCellRenderer(row, column);
    ColorRenderer and ColorRenderer1 are two inner classes, which implement TableCellRenderer to draw inner JTable on the outer JTable cell, having 2 rows and 1 column each.
    Now what is happening the above code keeps executing continously, that is the classes are being initialised continously, inner JTables are rendered (drawn) continously, and this makes the application slow after some time. It throws java.lang.OutOfMemoryException.
    WHY IS IT SO??? I can't understand where's the bug..
    Any advice please???
    Moreover i want selections in inner tables and not on outer table, how can this be possible.
    I am working on this since a long time but have not yet found a way out...

    With your help i have overcome the problem of continous repeatition.
    The major problem which I am facing is, in selecting rows in the inner rendered JTables.
    I have added listener on outer JTable which select rows on the outer JTable, hence the complete inner JTable which being treated as a row, gets selected.
    The thing is i need to select the rows of inner rendered JTables,not the outer JTable.
    How to go about it??
    I have even added listener to inner rendered JTables, but only first row of every table gets selected.
    Please help....
    Thanks in advance.

  • Change selected row when press tab

    Hi
    I use JDev11.1 with ADF, i have grid in my jsf page, this grid are updatable, i need when i press tab and go to next record set selected row that cursor on it, how can i do that?
    Thanks

    Hi,
    one option is to add a clientListener to the input text fields, intercept the tab key and then use a serverListener component to reference a managed bean method that
    - sets the row currency to the next row
    - PPRs the table so it shows the selection change
    Frank

  • How to set the Selected row and Column in JTable

    Hi,
    i have a problem like the JTable having one Method getSelectedRow() and getSelectedColumn to know the Selected row and Column but if i want to explicitly set the Selected Row and Column,then there is no such type of Methods.If anybody has any solution then i will be thankful to him/her.
    Praveen K Saxena

    Is that what you're looking for? :myTable.getSelectionModel().setSelectionInterval(row, row);
    myTable.getColumnModel().getSelectionModel().setSelectionInterval(column, column);

  • How to set special rows in jtable not selectable

    Hello programmers,
    anybody knows how to set special rows(p.E. row 0) in jtable not selectable.
    in advance thanks for your answers

    table = new JTable(...)
         public void changeSelection(int row, int column, boolean toggle, boolean extend)
              if (row == 0)
                   return;
              else
                   super.changeSelection(row, column, toggle, extend);
    };

  • Overriding the SelectionForegroundColor of a selected row in a JTable

    Hi
    First, apologize for my poor English.
    I looked for a similar topic in the forum, but coudn't find one.
    What I want to achieve is this :
    upon data contained in a column, I want the foreground color of the row to be gray. To do this, I write my own TableCellRenderer and it works perfectly except for the unique selected row of the table (single selection model) whose foreground color is the table's selectionForegroundColor.
    I found a workaround...but the code is awful and leads to OutOfMemoryErrors.
    Here is the code :
    <
    public Component getTableCellRendererComponent(JTable table,
    Object value,
    boolean isSelected,
    boolean hasFocus,
    int row,
    int column) {
         boolean isGrayColored = false;
         // some process to set whether the foreground color should be gray
         if (isGrayColored) {
              //table.setSelectionForeground(Color.gray);
              setForeground(Color.gray);
         } else {
              //table.setSelectionForeground(Color.black);
              setForeground((isSelected) ? table.getSelectionForeground() : table.getForeground());
         if (table != null && value == null && isSelected && hasFocus) {
              return this;
         } else {
              return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    >
    If I uncomment the lines "table.setSelectionForeground(..", I got the expected result.
    Thanks for your help
    Regards,

    Try this:
          public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
             if (isSelected && hasFocus) {
                setForeground(Color.black);
             } else {
                setForeground(Color.gray);
             setText((value==null)?"":""+value);
             return this;
          };o)
    V.V.
    PS: If it helps, don't forget the dukes!

Maybe you are looking for

  • Cross-reference auto-page numers?

    Can anyone tell me why i cannot get cross-references to work with  automatic page numbering?  Is there some fundamental error i am  making?  I have the master files setup with auto-page numbering based on  'current page.'  All of my pages are numbere

  • Why is my mac book pro slower to boot up and browse since upgrading to OS Yosemite

    Help a new Apple user, I purchased a new mac pro from Best Buy and learn how to use it pretty quickly and actually started to believe it was a great decision to switch from windows.  Then the mac pro gave the upgrade to the OS Yosemite, I upgraded it

  • Deprecated methods in Interfaces

    Hi all, I'm implementing an interface containing some deprecated methods. These deprecated methods have to be implemented, of course. If I compile my implementation I get deprecation warnings for these implemented methods. Does anyone know how I can

  • Condition records on header

    Hi experts I am working on contracts, I need to get the condition records on the header tab of the contract. i was able to get the condition records of items from table KONV but not the header. Is there a function module to get the condition records

  • Trouble mirroring root pool onto larger disk

    Hi, I have Solaris 11 Express with a root pool installed on a 500 GB disk. I'd like to migrate it to a 2 TB disk. I've followed the instructions on the ZFS troubleshooting guide (http://www.solarisinternals.com/wiki/index.php/ZFS_Troubleshooting_Guid