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?

Similar Messages

  • 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);
    };

  • 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

  • Selected row in JTable should

    Hi
    In my JTable i'am hilighting the selected row with blue color and the Selected row will be changing dynamically during run time, this is working fine but the problem is:-
    when selected row crosses the visible space it's Hilighted but unless i manually scroll down it's not visible, so i
    Can any one tell me how to set the scrollbar to adjust so that selected row is always visible with in vissible space and i neednot scroll down to see
    Thank's in Advance for any advice/suggestions

    Well this method is taking Rectangle as the parameter so i tried the following :-
    table.scrollRectToVisible(new Rectangle(table.getSelectedRow(),table.getSelectedColumn(),1,1));
    Am not sure about it but still i'll give a try
    Anyways Thank's a lot for the Replay, Atleast i got a hint to give a try
    THANK AGAIN

  • URGENT - Change color for a set of rows in JTable

    Hello all,
    How can i set color for a particular set of row which are all having same values in a cell. If any other rows contains same value that should also be in different color.
    Example:
    I have 10 rows containing itemcode and taxes in JTable.
    1st, 2nd and 4 th row contains same value of Taxes -- It should be in a set of color. red
    6th and 8th row containing same value of Taxes -- it should be in a set of color blue
    other are in normal color. Kindly send the coding for the above. Advance thanks.
    Regards,
    N.A. Ramasubramani / Chennai.

    Hi,
    Here is how I do it... create a Vector of Vector or an array[row][col] containing colors for every cell (row,col). Create a custom Cell renderer in which you pass the color data variable. Then in the renderer you get the Row and Col and set the appropriated color for the background.
    JRG

  • Looping the selected row in JTable

    By default, when the first row in a JTable is selected and the up arrow is pressed, the first row remains selected. What I need in this case is the selected row to change to the last row in the table, in effect creating a circular selection. I might have missed something in the API, but is there a way to (easily) change the default selection behavior?
    I have thought of adding a listener that would select the last row when the up arrow is pressed and the first row is selected, but it seems like a hack. Searching the web has provided little to no help.
    Does anyone know of an elegant way to do this? ...or can anyone provide any links that could be of help?
    Thanks!
    Glenn

    Thanks for the help! I made some minor changes to the code you provided because it was not allowing me to select the first and last row (it just skips them). Also, the row that is selected must be in edit mode in order for it to work.
    Here is the modified code. For simplicity, I just put the operations for the up and down arrow in the same method.
      protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
                                          int condition, boolean pressed) {
        // if pressed == false, no looping is needed, so perform default action
        if (!pressed)
          return super.processKeyBinding(ks, e, condition, pressed);
        // up arrow
        if (e.getKeyCode() == KeyEvent.VK_UP) {
          // if the current selected row is 0 or less, then loop.
          if (this.getSelectedRow() <= 0) {
            // if the cell is in edit mode, stop editing
            if (this.getCellEditor() != null)
              this.getCellEditor().stopCellEditing();
            int rowCount = this.getModel().getRowCount();
            this.getSelectionModel().setSelectionInterval(rowCount-1, rowCount-1);
            return true;
          } else {
            return super.processKeyBinding(ks, e, condition, pressed);
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
          // if the selected row is the last row, then loop
          if (this.getSelectedRow() >= (this.getModel().getRowCount() - 1) ) {
            // if the cell is in edit mode, stop editing
            if (this.getCellEditor() != null)
              this.getCellEditor().stopCellEditing();
            // loop the selection to the first row
            this.getSelectionModel().setSelectionInterval(0, 0);
            return true;
          } else {
            return super.processKeyBinding(ks, e, condition, pressed);
        } else {
          return super.processKeyBinding(ks, e, condition, pressed);
      }The above code does not attempt to restore the editor component when a new row is selected like the code you provided.
    Thanks again for providing such a thorough example!

  • Selecting Row in JTable

    Dear friends,
    When i select a whole row in JTable it gets selected but the background color of the row selected does not change. i.e. Its not highlighted as in MS-Excel.
    How can i overcome this problem?
    Kindly reply
    Thanks.

    Hi,
    Thanx for ur inputs. This is what the code i wrote. When u run this code,
    If we select row1 column1 the cells highlighted will be on row 2, 3 and 4. If we select row1 column 2 the selected cells will be on row 1, 3 and 4. I want the row to be selected fully when we select coumn 1 only.
    For eg. if i select row 200 from column 1 the 200 th row should only be fully highlighted
    Anyway i'll try the above code and check
    Here is the code
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class table1 extends JFrame
         // Variables declaration
         private JTable jTable1;
         private JScrollPane jScrollPane1;
         private JPanel contentPane;
         // End of variables declaration
         public table1()
              super();
                this.setVisible(true);
         private void initializeComponent()
              jTable1 = new JTable();
              jScrollPane1 = new JScrollPane();
              contentPane = (JPanel)this.getContentPane();
              jTable1.setModel(new DefaultTableModel(4, 4));
              jScrollPane1.setViewportView(jTable1);
              contentPane.setLayout(null);
              addComponent(contentPane, jScrollPane1, 58,65,301,207);
              this.setTitle("table1 - extends JFrame");
              this.setLocation(new Point(0, 0));
              this.setSize(new Dimension(473, 539));
         private void addComponent(Container container,Component c,int x,int
    y,int width,int height)
              c.setBounds(x,y,width,height);
              container.add(c);
         } Thanks again

  • 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.

  • Selecting row of Jtable

    Hi, this is my first post in forum.
    I want to know how can I selct the row in Jtable, and store all the enteries of that row in other variable;
    for further operation.
    For e.g-
    suppose I have a row in Jtable having contents.
    Shubham   21    NewDelhi   India
    I want to elect this row and store contents of row in other variables
    Thanks..

    Read in the tutorial about User Selections: [http://java.sun.com/docs/books/tutorial/uiswing/components/table.html]
    To get the value of a specific cell use getValueAt method.

  • 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

  • Setting Non-Editable property for selected Rows in jTable

    Hi,
    I want particular rows to be set non-editable within a jTable. For example. If I have seven rows as follows:
    1
    2 - (Non-editable)
    3
    4
    5 - (Non-editable)
    6
    7
    I want to set non-Editable property to the second & Fifth Rows(say) alone. Other Rows, I may edit.
    How can I do this?
    Thanks in adv.

    TableModel has method
    boolean isCellEditable(int rowIndex,
    int columnIndex)
    so you need to make your table model return false for these rows.

  • 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.

  • 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

  • Delete row in JTable

    Hello I am new To JAVA SWING
    I have 2 questions related to JTable
    1) How do i remove selected row in JTable ...for instance, I click on the row to delete (It is selected) , later I press the delete button, and then row is removed .... any hints ?
    2) If I would like to save the rows in a JTable into a database , .... How can iterate through the rows in the JTable..each row .... the rows are saved in a List object...and a List Object is what I pass as a parameter in a class method in order to be saved into Database.
    Thanks,
    deibys

    1) use the removeRow(...) method of the DefaultTableModel
    20 use table.getModel().getValueAt(...) to get the data from the model and create your list Object.

Maybe you are looking for

  • Converting PDF to Word changes the text to symbols. How to correct?

    Hi. I'm using Acrobat XI Pro and converting PDF's to Word docs changes the main text to symbols. I've tried this several times and always get the same result. Any idea as to how to correct? Thanks.

  • Weblogic core dump

    I have encountered a core dump problem on weblogic server 6.1 SP5 the error message as below, I am using weblogic oci driver for oracle "libweblogicoci37.so" anyone can help? An unexpected exception has been detected in native code outside the VM. Un

  • Weird numbers in Color panel

    I've just had to force quit twice. I'm working on a Gradient Mesh file (in CS3), but it's not that complicated. Anyway, I noticed this before I force quit: Negative 2147 -- That's really white! Should I delete my prefs or something?

  • Substitution Variable on Forms

    Hi All, I can not use Substitution Variable which is set to range,for example &FcstPeriods (Apr:Dec) on the form. In a form I need member1's data for Historical months (jan to Mar) and member2's data for FutureMonth's (apr to Dec). Instead of updatin

  • ORA-29516: Aurora assertion failure: Assertion failure at eox.c:232

    Hi, While exceuting following statement: SELECT dbms_java.longname(object_name), status, object_type, owner FROM all_objects WHERE object_type LIKE '%JAVA%' ;I am getting following error: ORA-29516: Aurora assertion failure: Assertion failure at eox.