Row selection in a JTable

Hi;
I have a panel with various textfields, buttons and a table. When I click on the search button, the table displays some data stored in my database.
When I select a row from the table, I want the data to appear in their various textfields. For example;
LastName = John, should appear in the LastName's textField.
FirstName = Christy, should appear in the FirstName's TextField.
e.t.c
I will really appreciate it if anyone out there can give me a hand. Thanks a lot in advance!!
Cheers,
Yinx

First I would define a function, maybe in the panel that contains the table and the cells that update the cells from the table, maybe
public void updateCellsFromTable();The I would define my own javax.swing.event.ListSelectionListener. It would looks something like this:
class UpdateListener extends ListSelectionListener
     protected TableCellPanel tcp;     //the panel that contains table and cells
     public UpdateListener(TableCellPanel tcp)
          this.tcp = tcp;
     public void valueChanged(ListSelectionEvent lse)
          tcp.updateCellsFromTable();
}You would then have to add this to your table when you created it. So in TableCellPanel or whatever:
     table = new JTable(...);
     cell = new JTextArea(...);
     table.getListSelectionModel().addListSelectionListener(
          new UpdateListener(this));Anyway that is one way to go about it...
     

Similar Messages

  • Row selection listener

    Does anybody know how can I listen the row selection in a JTable?
    Thanks,
    Maurizio

    Cant I override the valueChanged(..) method?From JavaDoc of JTable
    Invoked when the row selection changes -- repaints to show the new selection.
    Application code will not use these methods explicitly, they are used internally by JTable. It is always best to leave the default implementations as they are and use listeners of your own, else you may get to the point where you might break the default functionality of the component.
    ICE

  • How do I change the row selection by mouse movement

    How do I change the row selection on a jtable by mouse over events?
    Thanks, Randy

    got it thanks...

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

  • Custom JTable column (JCheckBox) not included in row selection.

    I am trying to use JCheckBox (for display only data) as one of my JTable columns. I have written custom cell renderer for the same. Every thing is working fine except that when I select a row, the new custom JCheckBox column is not included in selection. Here is the code that I am using.
        protected class CheckBoxColumnRenderer extends DefaultTableCellRenderer {
            JCheckBox ckb = new JCheckBox();
            public Component getTableCellRendererComponent(JTable table, Object value,
                        boolean isSelected, boolean hasFocus, int row, int column) {
                if (value instanceof Boolean) { // Boolean
                  ckb.setSelected(((Boolean) value));
                  ckb.setHorizontalAlignment(JLabel.CENTER);
                  ckb.setBackground(super.getBackground());
                  if (isSelected || hasFocus) {
                      ckb.setBackground(table.getSelectionBackground());
                return ckb;
        }How can I include the custom cell in the row selection.
    regards,
    nirvan.

    they have lots of dependencies and it is not always easy to strip out an SSCCE without a considerable effort.Exactly. And is the cause of the problem the dependencies or something else. The only way to know for sure is to strip out the code and simplify the problem, that way you truly understand what the problem is.
    The majority of time this can be done with minimal effort as in this case.
    Some times we are sure that the problem is with certain part of the code Is the problem the code or the way the code is invoked? How do we know the TableModel is created properly or that the column class is overwritten correctly when we can't see it?
    someone having a third look at it may actually find the enhancement required with ease.Exactly, but we need to see the big picture.
    So are you sure that I should post an SSCCE with every possible question where coding is involved ?In the majority of cases a SSCCE is easily created in 5-10 minutes, so if you want the fastest help then yes.
    Just twice this past week I was ready to ask a question on the forum and was preparing a SSCCE to post and sure enough both times the creation of the SSCCE caused me to look at the problem differently and I solved it. I would much rather solve a problem on my own then post a question and wait for hours (days) hoping someone else knows the answer.

  • Only allow x rows to be selected in a jtable

    hi all,
    i want to only allow a user to select x rows. where x is set in the code.
    for example if i only allow 2 rows to be selected....and the rows are:
    a
    b
    c
    d
    e
    then if the user selects a then b then c the selection is first ab, then bc (ie a is deselected when c is selected). kind of like a first in first out thing.
    so if the user then selected e, then you have c and e as the selected rows.
    any ideas of how i can do this...thanks everyone.
    Message was edited by:
    sand_samurai

    A JTable's row selection is handled by a ListSelectionModel object:
    http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTable.html#selectionModel
    You should be able to either use manipulate that object, or subclass it to implement you custom selection logic and assign your subclass to the table's selectionmodel.
    As far as implementing the logic, keep a queue of the selected rows. When a new row is selected, add it to the queue, and then reset the selection to the first x rows in the queue.

  • Disabling multiple row selection in JTable

    hi,
    I have a JTable and I want to disable the multiple row selection.
    I used
    getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);and this working fine with ctr+clicking on a row
    but if i am using shift key instead of Ctrl key multiple selection is possible....
    Any idea y?and how to resolve it??
    thnx
    ~neel

    Use table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);I don't know if it differs but I use it and I don't have the problems you describe. Might be a bug in your Java version?!
    Message was edited by:
    pholthuizen

  • JTable row selection

    What technique would you recommend to programmatically select one or more rows in a basic JTable?

    I used addRowSelectionInterval to get multiple selections. Your note directed my attention to the proper method(s). Naturally I overlooked it on several occasions. Thank you DrClap.

  • How can I make a row selected in JTable?

    I want to make a row selected and highlighted in JTable programmatically.
    But, I couldn't find method like setSelected(int row).
    How can I do this?

    try this ....
    private void makeRowVisible(JTable table, int row) {
         java.awt.Rectangle cellRect = table.getCellRect(row, 0, true);
         if (cellRect != null) {
              table.scrollRectToVisible(cellRect);
    }

  • About row selection in JTable

    This is also a question about JTable, when type the key "ENTER", the row selection will focus on the next line, how can I stop this default action? Can I make the default action of pressing "ENTER" to be activate a cell in the JTable to editing state?
    Thanks for any help.

    Sorry, I need to ask for help again, yes, in your case, the method works,
    but I am afraid this method does not help me, because I don't know HOW TO MAKE THE F2 FUNCTION in my case.
    I use the method you recommend, and yes I can stop the default response of pressing ENTER, it means when I stroke ENTER in my table, the selection won't go to the next line, but it just up to my half purpose, how can I use Enter to editing a cell then? My silly question is I even don't know how to make F2 works?

  • JTable - make 1st row selected by default

    hi,
    i have a JTable, how can i make the 1st row selected by default?
    thanks.

    try table.getSelectionModel().setSelectionInterval(0, 0)
    You need to check and make sure there is at least one row in the table before you do this, otherwise exception will throw.
    Hope this will help.

  • JTable row selection with tab key

    Helo.
    I'm having a problem selecting rows in a JTable. The JTable is with single row selection and without column and cell selection. When I press <tab> the selection changes to another cell, and I wanna it to change to the other row of the table, can anyone help me? I think this is a easy one and I haven�t found a way to do it. I�m using JBuilder for the development.

    The default Action for the Tab KeyStroke is to move focus to the next cell.
    The default Action for the Enter KeyStroke is to move focus to the next line.
    So this functionality is already available in JTable by default.
    If you want to Tab Key to work the same way as the Enter Key then you need to map the Tab Key to use the Action assigned to the Enter Key.
    This [url http://forum.java.sun.com/thread.jsp?forum=57&thread=505866]posting shows how you can reassign an Action for a given KeyStroke.
    For more information on this topic read this section from the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings.

  • A Listener For JTable Row Selection Changed?

    Never mind... Thought of a couple other things after I posted and found ListSelectionListener. This looks like it fulfills my requirement nicely. - cimmerian76
    >>>
    TableModelListener isn't what I am looking for here. Basically, I'm looking for something that behaves in a way that is analogous to
    TreeSelectionListener's valueChanged(...). Perhaps I'm looking in the wrong places, but the only things I've found were RowSet listener (not even close)
    and TableModelListener (not exactly what I'm looking for - I'm not concerned about data in the table changing).
    The tool I'm writing displays additional information about the object in the row selected in an adjacent panel.
    This display should change every time a user selects a different row.
    I can mimic the behavior I want using a mouse listener on the table, so this isn't an emergency.
    It's more about curiosity. If I can find something that produces this functionality directly, I would prefer to use that.
    <<<
    Message was edited by:
    cimmerian76

    Hi,
    you can use this to handle selection events for the table:
    yourTable.getSelectionModel().addListSelectionListener(... );
    Now e.g. the class containing your main can implement interface ListSelectionListener.

  • How prevent JTable row selection?

    there are three selection mode for JTable, MultiIntervalSelection, SingleIntervalSelection and SingleSelection. Is there a no selection mode?

    add selection listener and call table.clearSelection();or
    public class NullSelectionModel implements ListSelectionModel {
              public boolean isSelectionEmpty() { return true; }
              public boolean isSelectedIndex(int index) { return false; }
              public int getMinSelectionIndex() { return -1; }
              public int getMaxSelectionIndex() { return -1; }
              public int getLeadSelectionIndex() { return -1; }
              public int getAnchorSelectionIndex() { return -1; }
              public void setSelectionInterval(int index0, int index1) { }
              public void setLeadSelectionIndex(int index) { }
              public void setAnchorSelectionIndex(int index) { }
              public void addSelectionInterval(int index0, int index1) { }
              public void insertIndexInterval(int index, int length, boolean before) { }
              public void clearSelection() { }
              public void removeSelectionInterval(int index0, int index1) { }
              public void removeIndexInterval(int index0, int index1) { }
              public void setSelectionMode(int selectionMode) { }
              public int getSelectionMode() { return SINGLE_SELECTION; }
              public void setValueIsAdjusting(boolean valueIsAdjusting) { }
              public boolean getValueIsAdjusting() { return false; }
              public void addListSelectionListener(ListSelectionListener x) {}
              public void removeListSelectionListener(ListSelectionListener x) {}     
    table.setSelectionModel(new NullSelectionModel());no need to edit? simply use setEnabled(false);
    cheers !!
    //Ref: http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=2&t=017662

  • Selection Problem with JTable

    Hello,
    i have a selection problem with JTable. I want to allow only single cell selection and additionally limit the selection to the first column.
    I preffered the style from MS Outlook Express where you can select the email accounts to edit.
    It is a table like this:
    Account name  |   Type  |   ...
    --------------|---------|---------------------
    Hotmail       |   POP3  |
    GMX           |   IMAP  |The selection should be only avaibable at 'Hotmail' or 'GMX' - not at 'POP3', 'IMAP' or as complete row selection.
    Please help me!
    Thanks.
    Warlock

    Maybe this will helpimport java.awt.*;
    import javax.swing.*;
    public class Test3 extends JFrame {
      public Test3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        String[] head = {"One", "Two"};
        String[][] data = {{"R1-C1", "R1-C2"}, {"R2-C1", "R2-C2"}};
        JTable jt = new JTable(data, head);
        jt.getColumnModel().setSelectionModel(new MyTableSelectionModel());
        content.add(new JScrollPane(jt), BorderLayout.CENTER);
        jt.setCellSelectionEnabled(true);
        jt.setRowSelectionAllowed(false);
        jt.setColumnSelectionAllowed(false);
        setSize(300, 300);
        setVisible(true);
      public static void main(String[] arghs) { new Test3(); }
    class MyTableSelectionModel extends DefaultListSelectionModel {
      public void setSelectionInterval(int index0, int index1) {
        super.setSelectionInterval(0, 0);
    }

Maybe you are looking for

  • BO Report in 4.0 takes lot of time to open

    Hi We are having BO 4.0 environment. The underline database is SAP BW and BO reports are built on top of Bex queries. We found that it takes HELL a lot of time sometimes to open in BI launch pad/Infoview. Just for opening it takes lot of time we coun

  • Apple iPhone 3GS 8GB Blank Screen While Updating to IOS4.3.3

    Hi have iPhone 3GS 8GB. This phone I got as replacement from Apple for my Previous iPhone which got stuck in recovery. This replacement phone was working on IOS 4.2.1. A few days back Apple has released IOS 4.3.3 and while updating my iPhone via iTun

  • Two dimensional table

    Hi all, I need to know about two dimensional structure in pl/sql? And how many level of sub queries are optimum for a query?

  • Upload photo & MYSQL

    Hi I wanna create php code ton insert some info in MYSQL BDD, dreamweaver do that but I must upload image too with this, I choose file for this but it juste take name and I wanna upload photo in "images" directory with PHPMYADMIN, defaut value for im

  • Sql injection avoiding

    If someone enters sql commands into a text element for address or name, how does cfqueryparam help protect against sql injection ? Would a regular expression or something checking for dangerous key words help at least as much ?