JTable selecting a row....

I have tried many things to figure out how to select a row using jTable. Nothing works for me, can someone please help. Here are some samples of what I have tried. If you need more information please ask and I will post it immediately. Thanks
Attempt #1
void jTable1_mouseClicked(MouseEvent e) {
ListSelectionModel lm = jTable1.getSelectionModel();
int selectedRow1 = lm.getMinSelectionIndex();
if ((e.getModifiers() & InputEvent.BUTTON1_MASK)!=0) {
int row = jTable1.getSelectedRow();
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Attempt #2
void jButtonSelect_actionPerformed(ActionEvent e) {
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel lm = jTable1.getSelectionModel();
int selectedRow1 = lm.getMinSelectionIndex();
if ((e.getModifiers() & InputEvent.BUTTON1_MASK)!=0) {
int row = jTable1.getSelectedRow();
ListSelectionModel rowSM = jTable1.getSelectionModel();
rowSM.addListSelectionListener (new ListSelectionListener() {
public void valueChanged (ListSelectionEvent e) {
if (e.getValueIsAdjusting ()) return;
ListSelectionModel lsm = (ListSelectionModel ) e.getSource();
if(lsm.isSelectionEmpty()){
System.out.println("No rows are selected.");
else{
int selectedRow = lsm.getMinSelectionIndex();
System.out.println("Row "+selectedRow+" is now selected.");
Thank you once again :0)

Why do you want to know when a row is selected?
If you are just trying to edit data you are going about this the hard way. Just use the table model.
If you want to have some other action occur when a row is selected try something like this.
table.addMouseListener (new MouseAdapter()
public void mouseClicked (MouseEvent e)
Point origin = e.getPoint();
if ( e.getClickCount() == 2)
{ // double clicked
int row = table.rowAtPoint(origin);
if (row == -1)
return;
else
// Do something
else
// single click
int row = tabl.rowAtPoint (origin);
if (row > -1)
// Do something else

Similar Messages

  • JTable - Select entire Row

    Hi
    How is it possible to select the entire row of a JTable without the cell that`s selected, gaining a border.
    Any help would be appreciated.
    Thanks
    Kelly

    Could you explain what you need? You don't want the border of the selected row to be displayed?

  • JTable Selecting Column/Row

    import javax.swing.DefaultCellEditor;
    import javax.swing.JComboBox;
    import javax.swing.JTextField;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    public class TableDemo extends JPanel {
        private boolean DEBUG = false;
        public TableDemo() {
             super(new GridLayout(1,0));
             JTable table = new JTable(new MyTableModel());
             table.setPreferredScrollableViewportSize(new Dimension(500, 70));
             JScrollPane scrollPane = new JScrollPane(table); //Set up column sizes.
             initColumnSizes(table); //Fiddle with the Sport column's cell editors/renderers.
             setUpSportColumn(table, table.getColumnModel().getColumn(2)); //Add the scroll pane to this panel.
             add(scrollPane);
        private void initColumnSizes(JTable table) {
             MyTableModel model = (MyTableModel)table.getModel();
             TableColumn column = null;
             Component comp = null;
             int headerWidth = 0;
             int cellWidth = 0;
             Object[] longValues = model.longValues;
             TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
             for (int i = 0; i < 3; i++) {
             column = table.getColumnModel().getColumn(i);
             comp = headerRenderer.getTableCellRendererComponent( null, column.getHeaderValue(), false, false, 0, 0);
             headerWidth = comp.getPreferredSize().width;
             comp = table.getDefaultRenderer(model.getColumnClass(i)). getTableCellRendererComponent( table, longValues, false, false, 0, i);
         cellWidth = comp.getPreferredSize().width;
         if (DEBUG) {
         System.out.println("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth + "; cellWidth = " + cellWidth);
         column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    public void setUpSportColumn(JTable table, TableColumn sportColumn) {
         JComboBox comboBox = new JComboBox();
         comboBox.addItem("SnowBoarding");
         comboBox.addItem("Rowing");
         comboBox.addItem("Knitting");
         comboBox.addItem("Speed reading");
         comboBox.addItem("Pool");
         comboBox.addItem("None of the above");
         sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
         DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
         renderer.setToolTipText("Click for combo box");
         sportColumn.setCellRenderer(renderer);
    class MyTableModel extends AbstractTableModel {
         private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
         private Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)} };
         public final Object[] longValues = {"Sharon", "Campione", "None of the above", new Integer(20), Boolean.TRUE};
         public int getColumnCount() {
                   return columnNames.length;
    public int getRowCount() {
         return data.length;
    public String getColumnName(int col) {
         return columnNames[col];
    public Object getValueAt(int row, int col) {
         return data[row][col];
    public Class getColumnClass(int c) {
         return getValueAt(0, c).getClass();
    public boolean isCellEditable(int row, int col) {
         if (col < 2) {
         return false;
    else {
    return true;
    public void setValueAt(Object value, int row, int col) {
         if (DEBUG) {
    System.out.println("Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")");
         data[row][col] = value; fireTableCellUpdated(row, col);
         if (DEBUG) {
         System.out.println("New value of data:");
         printDebugData();
    private void printDebugData() {
         int numRows = getRowCount();
         int numCols = getColumnCount();
         for (int i=0; i < numRows; i++) {
         System.out.print(" row " + i + ":");
         for (int j=0; j < numCols; j++) {
              System.out.print(" " + data[i][j]);
         System.out.println();
         } System.out.println("--------------------------");
    private static void createAndShowGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("TableDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    TableDemo newContentPane = new TableDemo();
    newContentPane.setOpaque(true);
    frame.setContentPane(newContentPane);
    frame.pack();
    frame.setVisible(true);
    public static void main(String[] args) {
         javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() { createAndShowGUI();
    According to this Application the rendering and cell editing is done based on the column. And it displays the same column values for each row.
    I want to use the same application but want to change the[b] set of column values for each row.
    ex:
                    [u] Column values[/u]
    Row 0 =  { Snowboarding ,Rowing, Knitting.......}
    Row 2 = {  A,B,C,..........}
    Row 3 = {1,2,3}
    Help PLEASE!!!!
    thanks                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    This posting shows one way:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=446022

  • JTable-Selecting a row when i press an alphabet in a single column table

    hi,
    I have come across a issue where if i press a alphabet the row containig the leading alphabet should be selected.
    for eg: if i press "s" the first occurence of row containing string starting with "s" ie "stateful" sholud be selected.My table has 1 column only..
    please look in to it nad provide a solution.
    Looking fwd
    cheers,
    sharath

    Use a JList. It supports this functionality.
    Otherwise you would need to add a KeyListener to the table. Loop through the model using getValueAt(...) and compare the first character and then highlight the line.

  • How to select a row in Jtable at runtime

    how to select a row in Jtable at runtime.

    use
    setRowSelectionInterval(int fromRowIndex, int toRowIndex);example if your table has 10 rows then u want to select the rows from 4 to 8 then use
    setRowSelectionInterval(3, 7);if you want to select just one row for example 5 then use
    setRowSelectionInterval(5, 5);

  • JTable select row by double click, single click

    Hi all,
    I would like to double click on the row of the JTable and pop up a window for adding stuff, a single click for selecting the row and then in the menu bar I can choose to etither add, change or delete stuff. I try to use the ListSelectionModel but does not seems to distinguish between double or single click.
    Any idea, doc, samples?
    or I should not use ListselectionModel at all?
    thanks
    andrew

    Hi. I used an inner class. By using MouseAdapter u dont have to implement all methods in the interface..
    MouseListener mouseListener = new MouseAdapter()
    public void mouseClicked(MouseEvent e)
    if(SwingUtilities.isLeftMouseButton(e))// if left mouse button
    if (e.getClickCount() == 2) // if doubleclick
    //do something
    U also need to add mouselistener to the table:
    table.addMouseListener(mouseListener);
    As I said, this is how I did it.. There are probably alot of ways to solve this, as usual :). Hope it helps

  • Select whole row in a JTable

    I would like to know how I can fix so that when a user clicks a cell in my JTable the whole row gets selected. Not with the clicked cell as white and the rest of the row in blue, but with the whole row in blue. I would also like to stop the cell from beeing edited. The third thing I wonder is how I can hide the grid between the cells, and make it look like a list??
    Thanks for replies!

    Thanks for the help!
    I managed to get the frame around the selected cell also. Did it by extending the DefaultTableCellRenderer. Found some more help here:
    http://forum.java.sun.com/thread.jsp?thread=447758&forum=57&message=2028189

  • How to select a row in JTable for right mouse click ?

    Hi All,
    I need to select a row on JTable for right mouse click ?
    Can any one help me ?
    Thanks a lot.
    Seelam.

    Got solution...
    tabel.addRowSelectionInterval(..) works.
    thanks.

  • How to select a row in JTable

    Hello,
    I have a problem selecting a row in a JTable.
    I use
    mytable.getSelectionModel().setSelectionInterval(row, row);
    to select a row, but after that this row is only highlighted. After calling this method I cannot change the selection using the arrow keys.
    Can anybody give me a hint, how I can get the row selected, so that I can use the arrow keys immediately after selecting the row?
    Any help is welcome.
    Thanks,
    Fritz

    Hello,
    I got the solution:
    final int pRow = row;
    final int pCol = column;
    final JTable myTable = mytable;
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    myTable.requestFocusInWindow();
    myTable.changeSelection(pRow, pCol, true, true);

  • Select multiple rows on a JTable

    Hi!
    Given: JTable, multiple selection allowed
    Wanted: a way to be able to set multiple non-continuous rows selected
    eg. on a JTable with 5 rows,
    I want to set rows 1, 3 and 5 selected
    Anyone?
    Stoffel.

    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTE
    VAL_SELECTION);
    select rows using <ctrl> and/or <shift>Or if you want to set programaticly use this methods, see the api
    for explanation.
    table.changeSelecton(int row, int column, boolean toogle, boolean extend);
    table.addRowSelection(int index0, int index1);
    table.addColumnSelectionInterval(int index0, int index1)
    ...

  • Multiple selection of row in a jtable

    Hello All,
    I am working with this jtable 'tblSearch'. The application requirement is that the user should have the ability to select multiple rows using the control key. I am using the bold part of the code to color the selected row light gray. Can you someone help me to select multiple rows by holding the control key down.
        public void PopulateAS400(){
            cmbView.hidePopup();
            setCursor(hourglassCursor);
            int scrPos = scpSearch.getHorizontalScrollBar().getValue();
            oapprovalSQL = SQLFactory.createOrderApprovalSql();
            Vector FreightList = oapprovalSQL.getData(getAs400SearchString(), getOrderby(), AS400, AS400Overide.length, searchItems, rdoMatchAny.isSelected());
            columnNames = (Vector)FreightList.get(1);
            data = (Vector)FreightList.get(0);
            model = new DefaultTableModel(data,columnNames) {
                public Object getValueAt(int row, int col) {
                    return super.getValueAt(row,col);
                public boolean isCellEditable(int row, int col) {
                    if (row == 0){
                        getTblSearch().setColumnSelectionAllowed(true);
                        return true;
                    getTblSearch().setColumnSelectionAllowed(false);
                    return false;
               public Class getColumnClass(int c) {
                   if(c == 5 || c == 9){
                        return BigDecimal.class;
                   }else{
                        return String.class;
            JTable tmp = new JTable(model)
                private final KeyStroke tabKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
                private final KeyStroke shiftTabKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB,KeyEvent.SHIFT_DOWN_MASK);
                public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
                    AWTEvent currentEvent = EventQueue.getCurrentEvent();
                    if(currentEvent instanceof KeyEvent)
                        KeyEvent ke = (KeyEvent)currentEvent;
                        if(ke.getSource()!=this)
                            return;
                        if (KeyStroke.getKeyStrokeForEvent(ke).equals(tabKeyStroke))
                            if (rowIndex > 0)
                                rowIndex = 0;
                                columnIndex = 0;
                                toggle = false;
                                extend = false;
                                getTblSearch().setColumnSelectionAllowed(true);
                        else if (KeyStroke.getKeyStrokeForEvent(ke).equals(shiftTabKeyStroke))
                            if (rowIndex > 0)
                                rowIndex = 0;
                                columnIndex = getTblSearch().getColumnCount()-1;
                                toggle = false;
                                extend = false;
                                getTblSearch().setColumnSelectionAllowed(true);
                            else if (columnIndex == getTblSearch().getColumnCount()-1)
                                rowIndex = 0;
                                columnIndex = getTblSearch().getColumnCount()-1;
                                toggle = false;
                                extend = false;
                                getTblSearch().setColumnSelectionAllowed(true);
                    super.changeSelection(rowIndex, columnIndex, toggle, extend);
                public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColumnIndex)
                    Component c = super.prepareRenderer(renderer, rowIndex, vColumnIndex);
                    if ((vColumnIndex == 7) && (rowIndex > 0))
                        if ((model.getValueAt(rowIndex,7) != null) && (model.getValueAt(rowIndex,7).toString().equalsIgnoreCase("NMI")))
                            c.setBackground(Color.red);
                        else if ((model.getValueAt(rowIndex,7) != null) && (model.getValueAt(rowIndex,7).toString().equalsIgnoreCase("NMI ANSWERED")))
                            c.setBackground(Color.green);
                        else
                            c.setBackground(Color.white);
                    else
                        c.setBackground(Color.white);
                    **if (isRowSelected(rowIndex) && (rowIndex > 0)){**
                        **((JComponent)c).setBackground(Color.LIGHT_GRAY);**
                    if (rowIndex == 0 && isCellSelected(rowIndex, vColumnIndex)) {
                        c.setBackground(lightBlue);
                    return c;
            setTblSearch(tmp);
            getTblSearch().setAutoscrolls(true);
            getTblSearch().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            getTblSearch().setAutoCreateColumnsFromModel(false);
            getTblSearch().setColumnSelectionAllowed(false);
            getTblSearch().setRowSelectionAllowed(true);
            getTblSearch().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            JTableHeader header8 = getTblSearch().getTableHeader();
            ColumnHeaderListener colH8 = new ColumnHeaderListener();
            colH8.setCallFrom("OrderApproval");
            colH8.setOapproval(this);
            header8.addMouseListener(colH8);
            header8.setReorderingAllowed(false);
            header8.setResizingAllowed(false);
            getTblSearch().getColumn("Co #").setCellEditor(asCompCell);
            getTblSearch().getColumn("Reg").setCellEditor(asRegCell);
            getTblSearch().getColumn("Rep #").setCellEditor(asRepCell);
            packColumns(getTblSearch(), 1);
            getTblSearch().getColumnModel().getColumn(6).setPreferredWidth(240);
            for(int i=0; i<searchEntries8.length; i++) {
                    getTblSearch().setValueAt(searchEntries8,0,i);
    ((GenericTextEditor)getTblSearch().getCellEditor(0,i)).setCellEditorValue(searchEntries8[i]);
    scpSearch.add(new PopupContainer());
    popupMenu = new JPopupMenu();
    JMenuItem printFinalOrderMenu = new JMenuItem(PRINTFINALORDER_CMD);
    printFinalOrderMenu.addActionListener(new PrintFinalOrderMenuListener());
    popupMenu.add(printFinalOrderMenu);
    MouseListener popupListener = new PopupListener();
    getTblSearch().addMouseListener(popupListener);
    scpSearch.setViewportView(getTblSearch());
    scpSearch.getHorizontalScrollBar().setValue(scrPos);
    setCursor(normalCursor);
    Thank you all for your time n help.
    Edited by: anjan_dev on Jan 29, 2008 2:14 PM
    Edited by: anjan_dev on Jan 29, 2008 2:15 PM
    Edited by: anjan_dev on Jan 29, 2008 2:16 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    The issue is when I have one row already selected and then when I click on another row while holding the control key down. Our application needs an user to be able to select multiple rows at the same time.That is the default behaviour. I have no idea why it doesn't work for you.
    Get rid of all your custom KeyEvent logic and try it again.
    If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)", that demonstrates the incorrect behaviour.
    http://homepage1.nifty.com/algafield/sscce.html
    Don't forget to use the "Code Formatting Tags", so the posted code retains its original formatting.
    http://forum.java.sun.com/help.jspa?sec=formatting

  • Selecting multiple rows in JTable

    Hi
    I wonder if it is possible to select multiple rows in a JTable. I have written the following:
            table.setRowSelectionAllowed(true);
            table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);However, when I select a second row, my first selected row unselects. I have to shift-click manually to select both rows. Is there a way around this (ie. without the shift-click part)?

    Well I have done that without finding anything... can you provide a link?

  • How to select first row of JTable by default

    Hi,
    I have a JTable with 5 rows.
    When the software starts, none of the rows are selected.
    I wish to keep the first row selected by default.
    How do I do it?
    I couldnt find any method like setSelectedRow() !!!
    Does anyone have a sample code plz?
    Thanks.
    Anuj

    couldnt find any method like
    setSelectedRow()You need to use getSelectionModel() first.
    Another way is to use:
    table.changeSelection(0, 0, false, false);

  • JTable: Selecting rows in columns independently

    Dear all,
    I have a JTable with two columns. I want the user to be able to select cells in columns independently. At present, the entire row gets marked as selected. Is it possible at all to, for instance, select row1 1 to 3 in column 1 and rows 4 to 5 in column 2? If so, where's the switch? Thanks a lot in advance!
    Cheers,
    Martin

    Are you trying to use a seperate table for each column.
    Thats not a good idear.
    Here is what you have to do.
    1. Create a sub class of JTable
    2. You will have to redefine how the selection is done so. You will need some sort of a collection to store the list of selected cells indexes
    2.1 Selecting a cell is simply adding the coordinations of the cell to the selection
    2.2 de selecting is just removing it from the collection.
    2.3 Here is what you have to override
         setColumnSelectionInterval()
         setColumnSelectionInterval()
         changeSelection()
         selectAll()
         getSelectedColumns()
         getSelectedColumn()
         getSelectedRows()
         getSelectedRow() You migh also need few new methods such as
         setCellSelected(int row, int column, boolean selected);
         boolean isCellSelected(int row, int column);
         clearSelection();
         int[][] getSelectedCells();You will have to implement the above in terms of your new data structure.
    3. Handle mouse events.
    Ex:- when user cicks on a cell if it is already selected it should be deselected (see 2.2)
    other wise current selected should be cleared and the clicked cell should be selected
    if your has pressed CTRL key while clicking the the cell should be selected without deselecting the old selection.
    ---you can use above using a MouseListener
    When the user hold down a button and move the mouse accross multiple cell those need to be selected.
    --- You will need a MouseMotionListener for this
    You might also need to allow selection using key bord. You can do that using a KeyListener
    4. Displaying the selection
    You have to make sure only the selected cells are high lighted on the table.
    You can do this using a simple trick.
    (Just override getCellEditor(int row, int column) and getCellRenderer(int row, int column) )
    Here is what you should do in getCellRenderer(int row, int column)
    public TableCellRenderer getCellRenderer(int row, int column)
      TableCellRenderer realRenderer = super.getCellRenderer(int row, int);
      return new WrapperRenderer(realRenderer,selectedCellsCollection.isCellSelected(row,column));
    static class WrapperRenderer implements TableCellRenderer{
        TableCellRenderer realRenderer;
        boolean selected;
        public WrapperRenderer(TableCellRenderer realRenderer, boolean selected){
           this.realRenderer = realRenderer;
           this.selected = selected;
        public Component getTableCellRendererComponent(JTable table,
                                                   Object value,
                                                   boolean isSelected,
                                                   boolean hasFocus,
                                                   int row,
                                                   int column){       
            return realRenderer.getTableCellRendererComponent(table,value,selected,hasFocus,row,column);
    }What the above code does is it simply created wrapper for the Renderer and when generating the rendering component it replaces the isSeleted flag with our on selected flag
    and the original renderer taken from the super class will do the rest.
    You will have to do the same with the TableCellEditor.
    By the way dont use above code as it is becouse the getCellRenderer method create a new instance of WrapperRenderer every time.
    If the table has 10000 cells above will create 10000 instances. So you should refine above code.
    5. Finnaly.
    Every time the selection is changes you should make the table rerender the respective cells in or der to make the changes visible.
    I'll leave that part for you to figure out.
    A Final word
    When implementing th above make sure that you do it in the java way of doing it.
    For the collection of selected cells write following classes
    TableCellSelectionModel  // and interface which define what it does
    DefaultTableCellSelectionModel //Your own implementation of above interface the table you create should use thisby default
    //To communicate the selection changes
    TableCellSelectionModelListener
    TableCellSelectionModelEventif you read the javadoc about similer classes in ListSelectionModel you will get an idear
    But dont make it as complex as ListSelectionModel try to keep the number of methods less than 5.
    If you want to make it completly genaric you will have to resolve some issues such as handling changes to the table model.
    Ex:- Rows and colums can be added and removed in the TableModle at the run time as a result the row and column indexes of some cells might change
    and the TableCellSelectionModel should be updated with those changes.
    Even though the todo list is quite long if you plan your implementation properly the code will not be that long.
    And more importantly you will learn lots more by trying to implementing this.
    Happy Coding :)

  • ViewObject selecting no rows

    Hi,
    In my JClient operation I use an Application Module which has multiple ViewObjects. Some ViewObjects are detail ViewObjects of other ViewObjects using ViewLinks. Certain ViewObjects are displayed in a JTree, some in a JTable others in a JList etc.
    When BC4J initializes the application module / panel binding it executes the queries of the master ViewObject(s) and loads a list of records in the JTree, JTable or JList (etc.). It also always selects the first record in the list.
    I don't mind BC4J to load data into the lists but I don't want a row selected in the ViewObjects until the user selects a row. The only way this seems possible is by resetting the ViewObject data. But this means, although it keeps displaying the data, it will lose all data and needs to perform a new query to really do something with the data.
    Is there another way to select nothing in a ViewObject which doesn't have the problem described above? And can this be set prior to loading (and selecting) data the first time? Or must it be set just after?
    Regards,
    Peter

    User please tell us yor jdev version!
    This should be no problem as long as your VO is build as road only based on a query like
    select distinct department_id from job_historyThis VO can be used without a PK, however some function which are working on PKs won't work. The query works on hte HR schema.
    Timo

Maybe you are looking for