JTable and getting the selected Row to display

Hi,
I am pasting a code below, in this code i am highlighting one row when loading the table, but since this Row is last, it does not get displayed unless the user scrolls down,
how can i make this row displayed, when i load the table first
I m using jdk1.4.1
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestTableSelect extends JFrame
     public TestTableSelect(String input)
     super("Table select");     
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     getContentPane().add(getTable(input));
     setSize (650, 250);
setVisible(true);
          private JPanel getTable(String input)
          JPanel panel = new JPanel();
          panel.setLayout(new BorderLayout());
          panel.setPreferredSize(new Dimension(100, 250));
          Vector data = dataVector();
          Vector column = headVector();
          JTable table = new JTable(data, column);
          table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          table.getTableHeader().setReorderingAllowed(false);
          table.getTableHeader().setResizingAllowed(false);
          table.setSelectionBackground(Color.cyan);
          table.setRowSelectionInterval(0,getSelectedIndex(data, input));
          javax.swing.JScrollPane scrollPane = new JScrollPane(table);
          table.setPreferredScrollableViewportSize(new Dimension(650, 150));
          panel.add(scrollPane, BorderLayout.NORTH);
          return panel;
     private Vector dataVector()
          Vector data = new Vector();
          for ( int i = 0; i < 20; i++)
               Vector data1 = new Vector();
               data1.add("ABC");
               data1.add("This is testing");
               data1.add(" ");
               data1.add(" ");     
               data.add(data1);
               Vector data1 = new Vector();
               data1.add("DEF");
               data1.add("This is testing");
               data1.add("1");
               data1.add("1");     
               data.add(data1);
          return data;
     private Vector headVector()
          Vector column = new Vector();
          column.add("Name");
          column.add("Description");
          column.add("Default");
          column.add("Firm");
          return column;
     private int getSelectedIndex(Vector data, String key)
          for(int i = 0; i < data.size(); i++)
               Vector data1 = (Vector)data.get(i);
               if(((String)data1.get(0)).trim().equalsIgnoreCase(key))
               return i;     
          return 0;
     public static void main(String args[])
     new TestTableSelect("DEF");

table.scrollRectToVisible(table.getCellRect(table.getSelectedRow(), 0, true);Put this after your table.setRowSelectionInterval(0,getSelectedIndex(data, input)) .
//David

Similar Messages

  • Getting the Selected Row from a JTable

    hi,
    how Can i get the Selected row from a JTable
    thanks...

    You know that JTable class? Well, you see those methods in it called "getSelectedRow()" and "getSelectedRows()"...?

  • How can I get the selected rows from two ALV grids at the same time?

    I have a program that uses two ALV grids in one dialog screen. I'm using the OO ALV model (SALV* classes).
    The user can select any number of rows from each grid. Then, when a toolbar pushbutton is pressed, I'd have to retrieve the selected rows from both grids and start some processing with these rows.
    It is no problem to assign event handlers to both grids, and use the CL_SALV_TABLE->GET_SELECTIONS and CL_SALV_SELECTIONS->GET_SELECTED_ROWS methods to find out which rows were marked by the user. Trouble is, this only works when I raise an event in each grid separately, for instance via an own function that I added to the grid's toolbar. So, I can only see the selected rows of the same grid where such an event was raised.
    If I try to do this in the PBO of the dialog screen (that contains the two grids), the result of CL_SALV_SELECTIONS->GET_SELECTED_ROWS will be empty, as the program does not recognize the marked entries in the grids. Also, an event for grid1 does not see the selected rows from grid2 either.
    As it is right now, I can have an own button in both grid's toolbar, select the rows, click on the extra button in each grid (this will tell me what entries were selected per grid). Then, I'd have to click on a third button (the one in the dialog screen's toolbar), and process the selected rows from both grids.
    How can I select the rows, then click on just one button, and process the marked entries from both grids?
    Is it somehow possible to raise an event belonging to each grid programmatically, so that then the corresponding CL_SALV_SELECTIONS->GET_SELECTED_ROWS will work?
    Thanks.

    Hello Tamas ,
    If I try to do this in the PBO of the dialog screen (that contains the two grids), the result of CL_SALV_SELECTIONS->GET_SELECTED_ROWS will be empty, as the program does not recognize the marked entries in the grids. Also, an event for grid1 does not see the selected rows from grid2 either.--->
    is it possible to  have a check box in each grid  & get the selected lines in PAI of the screen ?
    regards
    prabhu

  • Check box als column in a standard table, how to get the selected row

    Dear experts,
    I habe standard tablt with check box as column. Now I want to get the current selected row structure and do some changes. How could I solve this problem? till now I just know to get the structure via lead selection.
    lo_node->get_element().
    lo_element = lo_node->get_static_attributes ( static_attributes = ls_row).
    How could I get the element through check-box in stead of lead selection. Many thanks!

    check this code
    To get the selected row number
    data: lr_element type ref to if_wd_context_element,
              lv_index type i.
      lr_element = wdevent->get_context_element( name = 'CONTEXT_ELEMENT'  ).
      lv_index = lr_element->get_index( ).
    Thanks
    Bala Duvvuri

  • Getting the selected row (selected using radio button in dataTable)

    I have problem getting the selected row.
    If not mistaken, the way used in BalusC's website sets the value to the backing bean using the valueChangeListener.
    Can I set the value to the DTO instead?
    using something like
    private boolean selected;
    public void setSelectedItem(ValueChangeEvent e){
    this.selected = true;
    }  If i use this way it throws NoSuchMethodException. I have no idea why. I did this for the checkbox and it works fine. I don't know why it doesn't work for radio buttons.

    The DataTable is below :
    <h:panelGrid columns="1" columnClasses="table_align">
              <t:dataTable id="table1" value="#{sysMgrBean.locationList}"
                   var="location" rows="5" styleClass="table" headerClass="tableHeader"
                   rowClasses="tableRowOdd, tableRowEven" width="500">
                   <t:column>
                        <f:facet name="header">
                             <h:outputText styleClass="lbl" value="#{lbl['no']}" />
                        </f:facet>
                        <h:outputText value="#{location.id}" />
                   </t:column>
                   <t:column>
                        <f:facet name="header">
                             <h:outputText styleClass="lbl" value="#{lbl['slct']}" />
                        </f:facet>
                        <h:selectOneRadio  value="#{location.selected}"
                             onfocus="dataTableSelectOneRadio(this);"
                             onclick="enableInput(this.form,this)" valueChangeListener="#{location.setSelectedItem}">
                             <f:selectItem itemValue="" value="true" />
                        </h:selectOneRadio>
                   </t:column>
                   <t:column>
                        <f:facet name="header">
                             <h:outputText styleClass="lbl" value="#{lbl['cd']}" />
                        </f:facet>
                        <h:outputText value="#{location.loccd}" />
                   </t:column>
                   <t:column>
                        <f:facet name="header">
                             <h:outputText styleClass="lbl" value="#{lbl['desc']}" />
                        </f:facet>
                        <h:inputText id="Description" value="#{location.descr}" disabled="#{location.disabled}" />
                   </t:column>
              </t:dataTable>In the DTO :
    private boolean selected;
    public void setSelectedItem(ValueChangeEvent e){
    this.selected = true;
    }

  • How to get the selected rows & columns in the table?

    hi everybody,
                         In my application the table is kept inside the event structure.I select the cells  in the table (using mouse) on running time.How to get the selected number of rows & columns in that table?

    Hello,
    You can fill selected values of the table by writing to it or the corresponding property using a property node - the table is just a 2D array of strings.  I think for your "disable" question you are referring to the shortcut menu (when you right click).  If you are using LabVIEW 8.x, you can edit or disable that shortcut menu - just right click on your table at edit time and choose Advanced >> Run-Time Shortcut Menu.
    Best Regards,
    JLS
    Best,
    JLS
    Sixclear

  • How to get the selected rows in a table

    Hi,
    How to get the ids of all the selected rows. On Page load a query is executed that shows the data in a table with a checkbox in the first column to select the rows and delete. Now if a user select multiple rows how do I get the ids of selected rows in the backend code.
    Thanks

    Please search the forum before posting questions.
    refer following thread for table selection.
    Re: Record selection with MessageCheckBox and print the selected record.
    --Prasanna                                                                                                                                                                                                                                                                                                                                                                           

  • Getting the selected row from a datatable -- URGENT

    Hi,
    I am facing the following problem with a datatable. The datatable does not have any Input components like CommandLink, Buttons, Radio Buttons and Checkbox. Even I cannot use the hidden Command Link.
    One solution is to use a input hidden variable which can hold the curent row Id. But in this case too, how to get the control in a backing bean method is not clear to me.
    Please guide me how to get the data of the selected row in the backing bean.
    Thanks in advance......
    VJain.

    I again explain my problem:
    I need to get the Current Selected Row onclick of any row in a Datatable. But the datatable is restricted not to have any CommandLink or Input component so that I can generate the action to go to any method on a backing bean as shown below.
    <h:dataTable id="dTable" value="#{Bean.myList}" var="currentRow" >
    <h:column id="row1">
    <h:outputText value="# currentRow.productNumber}"></h:outputText>
    </h:column>
    <h:column id="row2">
    <h:outputText value="# currentRow.productName}"></h:outputText>
    </h:column>
    </h:dataTable>
    As shown above I cannot use any commandButton or CommandLink to submit the page. All I can do is to generate any onclick function on Table and do something with it. But not sure how to do it.
    Now I want the productNumber of the selected row in any method say getCurrentRowId of the Bean.
    I hope the problem must be clear now....
    Thanks for your quick reply..
    VJ

  • [UIX-ADF] How to get the selected row in my ViewObjImpl.java

    Hi,
    I have a uix table with a select column. I dragged my method from my ViewObjImpl
    and dropped it on the select column (radiobutton) as a submitbutton.
    If I run my application and select a row and press my button it always reads the first
    row. Why?
    I also tried including a param in my method, for the record id, but I dont know what to
    pass to my action binding as a param, where can I find the selected row?
    This is what I want, my method in ViewObjImpl.java
    public void doDelete() {
      ViewObjRowImpl pRow = (ViewObjRowImpl)this.getCurrentRow();
      try {
        pRow.setIsDeleted("Y");
        //this.executeQuery();
        this.getDBTransaction().commitAndSaveChangeSet();
        //this.validate();
      } catch(Exception e) { System.out.println("ERROR doDelete" + e);  }
    }It should set the IsDeleted field to 'Y' for the selected row. Perhaps im not doing the
    right thing for this?
    Can anyone give me some pointers?
    Thanks in advance
    Ido

    Thanks. So even if you select a row and then press the button, your method still only finds the first row?!
    Are you sure that the selection mechanism of your table is working? Your tableSelection fires a select event and your page contains a select handler?
    When you wire your page using drag and drop the binding should always use the default iterator of the ViewObject. And getCurrentRow() should always give you the current row of that iterator. So my feeling is that the selection doesn't take place.
    Or could it be that something resets the currency in the ViewObject from another place?
    Sascha

  • How to populate listbox on a screen and get the selected record.

    I have place a edit box and converted to listbox .
    It should diplay T001-Bukrs values.
    When the user selects a value I should get the selected value ...
    How can I do that?
    The screen is a '0100'.  not the normal 1000 selection screen.
    Regards.
    Erkan.

    Hi
    You can try something like this to populate the listbox
    type-pools: VRM.
      data: l_t_values  type  vrm_values with header line,
            l_wa_ce_conc type  pa0001.
      clear: l_wa_ce_conc,
             l_t_values.
      refresh: l_t_values.
      select  cod_ce_conc
              des_ce_conc
              into    corresponding fields of l_wa_ce_conc
              from    pa0001
        l_t_values-key         = l_wa_ce_conc-cod_ce_conc.
        l_t_values-text        = l_wa_ce_conc-des_ce_conc.
        append l_t_values.
      endselect.
      call function 'VRM_SET_VALUES'
           exporting
                id              = 'p0061-cod_ce_conc' <=== Your Dynpro field
                values          = l_t_values[]
           exceptions
                id_illegal_name = 1
                others          = 2.
      if sy-subrc <> 0.
        message e398(00) with text-001.
      endif.

  • V v urgent ...  how to get the selected rows in the vl02n transaction

    Hi ,
    I have requirement that i have selected the line items in the vl02n transaction.
    If I select only one row there is option gs_get_cursor-line.
    If I select more than one line item at a time I am getting only the last value in to gs_get_cursor-line.
    Can anybody let me know how to get the multiple line items  that are selected
    It is very urgent ..
    Plz Help

    Hi,
    can you check the E_row_id .
    v_row type LVC_S_ROW
    read table itab index E_ROW-index.
    regards,
    Ajay
    Edited by: Ajay on Feb 14, 2008 6:22 PM

  • How to dynamic construct checkbox  and get the selected status

    Hello. I'm new to the JSF development. I need to dynamically generate the table data based on the selection of database table name . The first column is the checkbox which will allow user to select one or more rows for modification, deletion, etc. Here is the partial code that i wrote to add checkbox to the first column of the table in the backing bean. I would like to generate the same checkbox binding as we did from Design Palette.
    <webuijsf:checkbox binding="#{UpdateTable.checkbox}" id="checkbox" selected="#{UpdateTable.selectedRow}"/>
    Checkbox checkbox = new Checkbox();
    checkbox.setValueBinding("name", getApplication().createValueBinding("#{currentRow.value['ROWID_VALUE']}"));
    checkbox.setValueBinding("selected", getApplication().createValueBinding("#{UpdateTable.selectedRow}"));
    tableColumn1.getChildren().add(checkbox);
    Once user checked the box, the backing bean should be able to catch the event.
    * Records whether or not the current row should be marked as selected,
    * based on the state of the checkbox.
    public void setSelectedRow(boolean b) {
    System.out.print("click");
    TableRowDataProvider rowdp = (TableRowDataProvider) getBean("currentRow");
    RowKey rowKey = rowdp.getTableRow();
    if (checkbox.isChecked()) {
    selectedRows.add(rowKey);
    } else {
    selectedRows.remove(rowKey);
    But it's not working as I thought. I'm not sure if I can binding selected with backing bean attribute like I did above. Please help!!!
    Edited by: askMore on Aug 6, 2009 12:25 PM

    dt cust min max
    1   a 100 200
    1   b 300 400
    1   c 500 600
    2   a 200 300
    2   b 400 500
    2   c 600 700
    CREATE OR REPLACE FUNCTION scott.rowtocol(p_slct IN VARCHAR2,p_dlmtr IN VARCHAR2 DEFAULT
    RETURN VARCHAR2 AUTHID CURRENT_USER
    AS
      TYPE c_refcur IS REF CURSOR;
      lc_str VARCHAR2 (4000);
      lc_colval VARCHAR2 (4000);
      lc_colval2 VARCHAR2 (4000);
      c_dummy c_refcur;
    BEGIN
      OPEN c_dummy FOR p_slct;
      LOOP
        FETCH c_dummy
        INTO lc_colval,lc_colval2;
        EXIT WHEN c_dummy%NOTFOUND;
        lc_str := lc_str || p_dlmtr || lc_colval || '=' || lc_colval2;
      END LOOP;
      CLOSE c_dummy;
      RETURN SUBSTR (lc_str, 2);
    END;
    SQL> SELECT DISTINCT a.dt,
                    rowtocol
                    ( 'SELECT cust , min  FROM tst WHERE dt = '
                      || ''''
                      || a.dt
                      || ''''
                    ) AS min ,
                   rowtocol
                    ( 'SELECT cust , max  FROM tst WHERE dt = '
                      || ''''
                      || a.dt
                      || ''''
                    ) AS max
    FROM tst a;
      2    3    4    5    6    7    8    9   10   11   12   13   14 
         DT MIN                         MAX
          1 a=100,b=300,c=500               a=200,b=400,c=600
          2 a=200,b=400,c=600               a=300,b=500,c=700

  • How can I get the selected row's index of a Jlist wrapped with JScrollpane?

    the problem is that I can use getSelectedIndex() only if the JList is not wrapped in a JScrollPane.
    but in my program the JList IS wrapped in a JScrollPane.
    when I select one item of the jscrollpane I can't catch the event.
    please F1 me.
    Lior.

    What difference does it make if the list is inside a scroll pane or not? If you have a member varialbe that references the list you can call getSelectedIndex when ever you need.
        myList = new JList (...)
        someComponent.add (new JScrollPane (myList));
        //  Then later
        int sel = myList.getSelectedIndex ();And you can always be up on any selection chagnes in the list by supplying it with a ListSelecitonListener.

  • How can I get the index of the selected row, ALV

    In alv I need to get the selected row id and then calling a screen to show the detailed infomation on this row and providing the ability to update the row.
    I know how to do but do not know how to get the selected index of the alv grid.
    If it was desired based on just double click, it was ok but I have to provide a button and when it is clicked the detailed screen will come with the selected row details.
    Thanks everyone.
    Deniz.

    Hi Deniz,
    While defining the internal table which u pass in ALV_GRID function module, define a field (suppose CHECK type c) in the structure of that internal table.
    and while defining field layout, pass the value 'CHECK' to the field BOX_NAME of the layout.
    Now, when you execute the report, a selection box will be displayed infront of every record. If you select that record and press the button on application tool bar, the field 'CHECK' in the internal table will be having the value 'X'... By looping throught this internal table where check = 'X', you can achieve your functionality.
    Example:
         Structure for output details
    TYPES: BEGIN OF GS_OUTPUT.
            INCLUDE STRUCTURE ZAS_WATER_QLTY_REP.
    TYPES: CHECK(1) TYPE C,
           END OF GS_OUTPUT.
        Internal    table for output details
    DATA: GT_OUTPUT   TYPE STANDARD TABLE OF GS_OUTPUT.
    Before calling the function module to display ALV
    grid, define a layout usine
    Local workarea
      DATA: LW_LAYOUT  TYPE SLIS_LAYOUT_ALV.
    Local constants
      CONSTANTS: LC_UFORM  TYPE SLIS_FORMNAME
                        VALUE 'ALV_USER_COMMAND'.
    LW_LAYOUT-BOX_FIELDNAME = 'CHECK'.    "Field for checkbox
    This function module outputs an internal table with whatever
    structure in the form of a formatted single- order multi-line list
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
           EXPORTING
                I_CALLBACK_PROGRAM       = LV_REPID
                I_CALLBACK_PF_STATUS_SET = G_FORM_SET_PF_STAT
                I_CALLBACK_USER_COMMAND  = LC_UFORM
                I_STRUCTURE_NAME         = LV_ZAS_WATER_QLTY_REP
                IS_LAYOUT                = LW_LAYOUT
                IT_FIELDCAT              = G_FIELDCAT_TAB[]
                I_DEFAULT                = GC_X
                I_SAVE                   = LC_A
                IS_VARIANT               = G_VARIANT
                IT_EVENTS                = G_EVENTS_TAB[]
                IT_EVENT_EXIT            = G_EVENT_EXIT_TAB[]
                IS_PRINT                 = G_PRINT
                I_SCREEN_START_COLUMN    = G_SCREEN_START_COLUMN
                I_SCREEN_START_LINE      = G_SCREEN_START_LINE
                I_SCREEN_END_COLUMN      = G_SCREEN_END_COLUMN
                I_SCREEN_END_LINE        = G_SCREEN_END_LINE
           TABLES
                T_OUTTAB                 = GT_OUTPUT
           EXCEPTIONS
                PROGRAM_ERROR            = 1
                OTHERS                   = 2.
    and in the form alv_user_command
    FORM ALV_USER_COMMAND USING UCOMM SELLINE TYPE SLIS_SELFIELD.
    case ucomm.
    Lets hope that the user command of the push button in * PUSH
    when 'PUSH'.
    loop at gt_output where check = 'X'.
    You can write your code here...
    endloop.
    endcase.
    ENDFORM.
    Hope this helps...
    Best Regards,
    Phani.
    Message was edited by:
            Sivapuram Phani Kumar

  • Getting the contents of the selected row in table webitem

    Hi,
    If i have a table web item in a web template,how do i get
    the selected row and row contents of the table.
    regards
    kaushik

    Just incase if you have not looked at the following documents:
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/6eb89190-0201-0010-89bf-ad46d1503ef9
    http://help.sap.com/saphelp_nw04/helpdata/en/99/08629bd3e41d418530c6849df303c9/frameset.htm

Maybe you are looking for