Change selectedRow in JTable

Is there anyone can tell how to programmatically change or select a row in JTable so that I can get the selected row using getSelectedRow() method ??

Try it!
setRowSelectionInterval( row, row );
or
ListSelectionModel rsm = table.getSelectionModel();
rsm.setLeadSelectionIndex( row );

Similar Messages

  • How to change data in  jtable?

    Hai.I have a problem to change data in jtable.Can anbody help me to slove this?
    Think you in advance

    Did you want to change individual cells?
    For example, to change cell (2, 1) to "new data":
    yourTable.setValueAt("new data", 1, 2)

  • JTable change SelectedRow automatically

    Hi,
    I have a JTable which displays the search results from the database base . When I click on any row in the table .. it displays the related data for that record in a textArea.
    Now I have kept 2 buttons <<,>> which can be used to traverse through the JTable data.. in the sense when I click >> it will display the data related to the next record in the Jtable.. Similarly << will show the previous.. All this works fine .. But I need an indicator to show which record is being displayed .. when using the buttons ..Because if I click on any row on the JTable and then use one of the buttons to change . the selection .. still the old selection is l highlighted in the JTable ..
    Is there any way to change the Selected Index of the Jtable from the button
    Will appreciate any help Thanks in advance .

    Alternatively:
    table.changeSelection(table.getSelectedRow()+1, -1, false, false); //select next row

  • Changing properties in JTable

    Hi
    New to Java but with extensive Borland Delphi/C++ experience.
    Using NetBeans I've added a form to my project and added a JTable to the form. However I cannot seem to be able to alter the rowCount or the columnCount in a JTable's properties....why is that?
    And how do change their values?

    in netbeans just right-click on one of the created rows in jtable
    not on the empty space in jtable because that would take you to the
    properties of jscrollpane(netbeans automatically put jtable on jscrollpane). After doing that just reduce the number of rows and
    columns.

  • Reacting on selection-changes in a JTable...

    Hello,
    I created an AbstractTableModel which is the basic for my JTable. Now I want to react on changes of the selection in the table. So I implemented a ListSelectionListener and overwrote the valueChanged()-method. But there is no reaction when the selection changes...is there a special trick with this method, or what else could be the reason for my problem ?
    Thanx,
    Findus

    Did you register your listener?
    yourTable().getSelectionModel().addListSelectionListener(yourListener);

  • Problem of Change Color in JTable ....

    Hi, i have a JTable contain a lot of data, HOW i change the color of some font of the data in the JTable ??
    Thank You.
    Regards,
    Kenny

    What is the criteria for changing font/colours ?? Is it everything in one column ? Everything in one row depending upon the value in one column ?
    I posted something similar to this, with a solution, not too long ago if you want to search the forum.
    Garry.

  • How can data change dynamically between jtable

    hi,
    in my database i have 3 tables:
    university, etablissement, department.
    in one frame i want to create 3 jnavigationbars and 3 jtables each one represents a table.
    i want when i navigate in the jtable of university data in etablisement and department will be dynamically changed.
    can anyone help me.
    thanks

    Hi,
    make sure that the university, etablissement and department tables are related in the database when creating the ADF BC model. Then create a hierarchy of university, etablissement and department. Create the first table from university VO, the second from etablissement VO which is below university in teh VO hierarchy and eventually create a table on departmentVO below the etablissementVo
    Synchronization is handled automtically
    Frank

  • Change FocusTraversalKeys in JTable

    Hi!
    Is there any way I can change the default FocusTraversal key in a JTable from TAB to ENTER (or just add ENTER as a FocusTraversal key so that the TAB behavior remains the same)?
    I've already done this for several components but it seems that JTable is a bit more complicated and that it handles focus traversal between cells internaly..
    thanks!

    thnx...but I already solved my problem. In case someone has the same problem:
    final InputMap im = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    final KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
    im.put(key, "selectNextColumnCell");

  • Dynamically changing rows in JTable don't re-render themselves

    Hi all,
    I've created a customized JTable and component editors/renderers. I also created a class that dynamically inserts new rows into the JTable and can also move entire rows up and down in the JTable. Dynamic insertion is working fine, but I'm having a problem with dynamically moving the rows up and down. One of my implementations has a table with three columns, the first column is always a JLabel in a JPanel, the second column is a variety of custom editors, and the third column is a custom editor for a custom JButton. If I change the value of either column 2 or 3 and then try to change the rows position in the table (up/down), then the cell that was edited remains blank until I click anywhere in the row that has the blank cell. But if I edit either column 2 or 3 and then click on the cell for column 1 in the same row and dynamically change its position (up/down) then the cells are rendered correctly.
    I've tried numerous ways to fix this problem, including fireTableDataChanged() and repaint() on the component in the cell. Any help would be greatly appreciated,
    amlandis

    So I tried updating the UI, but that didn't fix it.
    Do you explictly call editor.stopCellEditing() ? That sets the new value to your model. I override all stop editing methods and make them call their cancelEditing counterparts... and during debugging I watch the model and it has the correct data. I think my table sometime gets stuck in an isEditing() == true state. But is there no way to say setEditing(false)?
    From the BasicTableUI below... it seems like if my table gets into its editing state, the cells won't be rendered.
        private void paintCell(Graphics g, Rectangle cellRect, int row, int column) {
            if (table.isEditing() && table.getEditingRow()==row &&
                                     table.getEditingColumn()==column) {
                Component component = table.getEditorComponent();
             component.setBounds(cellRect);
                component.validate();
            else {
                TableCellRenderer renderer = table.getCellRenderer(row, column);
                Component component = table.prepareRenderer(renderer, row, column);
                rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y,
                                            cellRect.width, cellRect.height, true);
        }

  • Replicate column changes into other JTable(s)?

    I have a set of tabs in a JTabbedPane which show the results of a database query from a number of (allegedly) identical databases. Each tab contains a JScrollPane which contains a JTable. All the JTables share the same table model and the data is not editable.
    If the user changes a column (e.g. resizing or moving it) in one JTable I would like the change to be automatically replicated on the equivalent column in the other JTables.
    Is there a simple way to do this?

    Hi,
    Every change in one table produces special event , you should add a "listener" to reflect changes.

  • URGENT !!! HOW CAN CHANGE DATA IN JTABLE!!!

    My Program ....
    pLinhas.dataModel.addTableModelListener(new TableModelListener() {
    public void tableChanged(TableModelEvent e) {
    int row = e.getFirstRow();
    int column = e.getColumn();
    Object data = pLinhas.dataModel.getValueAt(row,column);
    Object valor = pLinhas.dataModel.getValueAt(row,6);
    System.out.println("Quantidade -" + data.toString());
    System.out.println("Valor a Cobrar -" + valor.toString());
    setValueAt(data,row,6);
    public void setValueAt(Object value, int row, int col) {
    System.out.println("Mudou ou nao");
    pLinhas.dataModel.fireTableCellUpdated(row, col);
    this dont work. When the method setValue() run, the java break.
    the message are :
    # An EXCEPTION_STACK_OVERFLOW exception has been detected in native code outside the VM.
    # Program counter=0x77e94aa0
    how can i change my data for a new data in jtable.
    tx for help.

    I had a problem similar to this. I was writing my own table model which would work sometimes and at other times fail similar to what you describe. I ended up printing out the source code to the DefaultTableModel and taking a look at it and then deciding to subclass that class and add my additional functionality as opposed to writing a completely new model and trying to duplicate the DefaultTableModel. My needs were relatively simple and the subclassing fixed the problem. I can change data with no problem in the displayed table. If you're not already subclassing and trying to write your own table model you may want to give this a try. I'd struggled with the problem for several days thinking I'd fixed it and having it come up again. I invested 2 hours doing the subclassing and the problem has never appeared since then. I know I just missed doing something in the table model I was trying to write but my schedule was tight and the subclassing offered a quick fix. Hope this helps!

  • How to listen for cell selection changes within a JTable

    Problem: my table has 8 columns, with 4 of them containing very large text (up to 1000 chars). Solution is to set the initial size these columns so that the 1st 20 chars are visible, and if one of these columns gains focus/selection via mouse-clicking/tabbing/arrow keys, the entire text is shown in a JTextArea below the table.
    I added a ListSelectionListener to the table, but this only informs me when the row selection has changed.
    I added a FocusListener to the table, but this only informs me when the table gains/loses focus, not when it's been changed within.
    I added a TableColumnModelListener to the TableColumnModel, but this only informs me when the column selection has changed.
    I didn't bother with MouseListener as this won't handle change of selection via tabbing.
    The LSL got me half way there, and the TCML got me the other half. Any ideas on how to combine?
    Or is there something obvious that I'm missing?

    Walter Laan wrote:
    Use both and call the same method which updates the text area based on the current selected (or focused == lead selection index) cell.Yeah - that's what I figured. I just didn't know if there was a magic bullet (i.e., a single listener) out there that I was missing.
    While you are at it, also add a table model listener which calls the same method if the selected cell is updated.I'll keep that in mind, but it's not necessary in this particular case. The table cells in question will be uneditable. The text area will have supporting Edit/Save/Cancel buttons.

  • Changing color in JTable objects

    Hi all,
    I'm writing an application which shows tabular view of events of some outcome. Now I want to give color to each row depending on the severity of particular parameters in the table. I've written application using swings and using components JTable. Is there any way to give color to each depending on value so that I can give colors to each row Red, Blue and Green like that and all
    Waiting for your precious replies,
    With best regards,
    harry_sai

    Yes, you use a TableCellRenderer. Rather than explain further I'll just refer you to the tutorial about JTable.

  • Behavior of ENTER arbitrarily changes in my JTable

    The setup: basic, non-exotic JTable in a 1.6.0_02 application running on Red Hat Enterprise 3.
    Normal behavior: hitting enter causes the table to select the next cell row.
    My behavior: randomly, I've been noticing that hitting enter causes the cell to start editing! Once in this state, the original behavior can't be restored without restarting the application. I can't figure out what causes this behavior to start. It only happens a small percentage of the time.
    Any idea what the deal is here? It seems doubtful that the input map for the table could get whacked. The table still responds as you'd expect to other mouse and key events. Again, I don't have any fancy editors or anything... just some basic text type editors that extend the default. None of them do anything funky with key events.
    Unfortunately this issue was noticed in production and it is bugging the users who do a lot of data entry. So I'm very grateful for any suggestions.

    Good thought, I checked this out just to make sure. My table's data is in fact updated upon receipt of network messages, however, all the work is done through an invokeLater() and thus happens on the ED thread.
    Without seeing the code and a process to reproduce the problem, I know I'm not going to get an exact answer, but if anyone else has any ideas of potential causes or places to look, I'm all ears.

  • Dynamically changing/updating a JTable

    I have a blank JTable that will add rows as the user clicks the "add rows". My dilemma is some of the objects in the rows are comprised of JCheckBoxes. I need to know how to create an actual checkbox in those spots, without creating a sublass the extends DefaultTableModel. Also i can get everything to display on the table when the button is pushed but i just cant render the components.
    I do know how to add the check boxes in the rows if the JTable is setup and never chaned from the beginning but I dont understand how to over come this problem for a dynamic table.
    Thanks in advance
    Chris
    Some code snippets.
    private static DefaultTableModel model = new DefaultTableModel();
    private JTable jTable = new JTable(model);
    //below is what is called when the "add row" button has just been pushed
    public static void setPricingTab()
        model.addRow(getRowInfo());
      }//end public static void setPricingTab()
      public static Object[] getRowInfo()
        list[0]=ConsecCheckBox.getBoxName(); //get most recent selected edition
        list[1]=new Boolean(false);
        list[2]="Monday";
        list[3]="Tuesday";
        list[4]="Wednesday";
        list[5]="Thursday";
        list[6]="Friday";
        list[7]="Saturday";
        list[8]=new JTextArea("Price",10,10);
        list[8]=new Boolean(false);
        list[10]=null;
        return list;
    //im sure i should be rendering here or in the setPricingTab()
    //but this is where i am stuck
      }

    Since you only post snippets of code its hard to tell whats wrong. Here is a simple example that I think does what you want:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    public class TableBoolean extends JFrame
        private final static String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        JTable table;
        DefaultTableModel model;
        public TableBoolean()
            //  Create table
            Object[][] data = { {"A", new Boolean(false)}, {"B", new Boolean(true)} };
            String[] columnNames = {"String","Boolean"};
            model = new DefaultTableModel(data, columnNames);
            table = new JTable(model)
                //  Returning the Class of each column will allow different
                //  renderers to be used based on Class
                public Class getColumnClass(int column)
                    return getValueAt(0, column).getClass();
                public boolean isCellEditable(int row, int column)
                    return true;
            //  Add table and a Button panel to the frame
            JScrollPane scrollPane = new JScrollPane( table );
            getContentPane().add( scrollPane );
            JButton button = new JButton( "Add Row" );
            button.addActionListener( new ActionListener()
                public void actionPerformed(ActionEvent e)
                    Object[] newRow = new Object[2];
                    int row = table.getRowCount() + 1;
                    newRow[0] = LETTERS.substring(row-1, row);
                    newRow[1] = (row % 2 == 0) ? new Boolean(true) : new Boolean(false);
                    model.addRow( newRow );
            getContentPane().add( button, BorderLayout.SOUTH );
        public static void main(String[] args)
            TableBoolean frame = new TableBoolean();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setVisible(true);
    }

Maybe you are looking for

  • PCI-e wireless card causes lock ups Z87 MPower MAX

    I've already asked this question once, but was part of an epic essay that I've decided to break down into smaller more manageable chunks (as unsurprisingly that one got no replies). I've got an ASUS PCE-A66 wireless ac pci-e card. Had absolutely no i

  • Launching an external file from jsfl script

    I've written a jsfl script which exports the frames of a Flash movie as pngs. I've also written a Photoshop .jsx script which takes the pngs and processes them. I was wondering if there was a command available in jsfl that could launch the .jsx file

  • AudioSubscriber.getNetStreamInfo error/bug BEWARE-DINGOS

    Not sure if this is a bug or just me abusing things. I get the following error when I do what I describe below: TypeError: Error #1010: A term is undefined and has no properties.     at com.adobe.rtc.collaboration::AudioSubscriber/getNetStreamInfo()[

  • HP Envy 4500 "Out of Paper" error

    My wireless HP Envy 4500 says "Out of Paper" when I try to print from my HP Pavilion.  The printer does work (also wireless) from a Dell laptop and if I use the Copy function. Any ideas why the printer stopped working from the Pavilion only?

  • 10.5.4. Error Message

    I am trying to install the update. I get the following: You cannot install MacOSXUpd10.5.4Patch on this volume. This system doesn't meet the file content requirements. HELP ME PLEASE!