Results 1 to 10 of 10
  1. #1
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default Cell selection not working in JTable

    I have a JTable in my application that, for the most part, is working as expected, but cell selection is not. When I click on, or tab into, a cell it appears to be selected (background color change), but when I start typing the new information is appended to the existing rather than replacing it. I have setCellSelection set to true, and thought that would handle everything. Is there something else that I need to do, or is this just something that JTable can't handle?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    Cell selection has nothing to do with "selecting the text in the editor". It is just used to highlight the current cell that will receive key strokes. This is not supported by JTable so you need custom code:

    Table Select All Editor

  3. #3
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Rob, while the code worked to select the cell, it doesn't work with my number formatting. All of the formats below (mf, cf, df) are ignored.

    Java Code:
    public class CustomTableCellRenderer extends DefaultTableCellRenderer
    {
    	public Component getTableCellRendererComponent (RXTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int col)
    	{
    		NumberFormat cf  = NumberFormat.getCurrencyInstance();
    		DecimalFormat mf = new DecimalFormat ("000,000");
    		DateFormat df    = DateFormat.getDateInstance(DateFormat.MEDIUM);
    
    		if (row % 2 == 0) setBackground(Color.lightGray);
    		else              setBackground(null);
    
    		if (col == clmnInvoice || col == clmnCost)
    			setHorizontalAlignment(SwingConstants.RIGHT);
    		if (col == clmnMileage)
    			return super.getTableCellRendererComponent (table, mf.format(obj), isSelected, hasFocus, row, col);
    		if (col == clmnSvcDate)
    			return super.getTableCellRendererComponent (table, df.format(obj), isSelected, hasFocus, row, col);
    		if (col == clmnCost)
    			return super.getTableCellRendererComponent (table, cf.format(obj), isSelected, hasFocus, row, col);
    		return super.getTableCellRendererComponent (table, obj, isSelected, hasFocus, row, col);
    	}
    }

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    Whenever you override a method you should use the Override tag. You will get a compiler error if you don't override and existing method.
    Java Code:
    @Override
    public Component getTableCellRendererComponent (RXTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int col)
    In your case you will get an error because RXTable is not part of the method signature. Just use JTable like you had before.

  5. #5
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    I didn't get a compile error without the @Override. When I executed the app the fields just weren't rendered properly.

    If I compiled using the @Override I get an error that the "method does not override or implement a method from a supertype"

    If I compile the renderer using JTable, then I get the rendering, but your selection doesn't work.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    Renderers and editors are different. Using a custom renderer should not affect the selection of the text in the editor. I don't know why you are having problems.

    Its works fine for me when I use a Table Format Renderer.

  7. #7
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    I'm not sure either, but it seems to be my way. :)

    My call to the renderer is similar to yours (see code below). I added a println command at the start of the renderer code itself, and it never displayed. From that it seems as though the renderer is not being called when I use RXTable. Also below I put code where I use RXTable instead of JTable...am I using it in the wrong places?

    Java Code:
    TableColumnModel tcol = table.getColumnModel();
    tcol.getColumn(clmnMileage).setCellRenderer(new CustomTableCellRenderer());
    tcol.getColumn(clmnSvcDate).setCellRenderer(new CustomTableCellRenderer());
    tcol.getColumn(clmnShop).setCellRenderer(new CustomTableCellRenderer());
    tcol.getColumn(clmnInvoice).setCellRenderer(new CustomTableCellRenderer());
    tcol.getColumn(clmnLabor).setCellRenderer(new CustomTableCellRenderer());
    tcol.getColumn(clmnParts).setCellRenderer(new CustomTableCellRenderer());
    tcol.getColumn(clmnCost).setCellRenderer(new CustomTableCellRenderer());
    Java Code:
    table = new RXTable(model);
    table.setAutoResizeMode(RXTable.AUTO_RESIZE_OFF);
    .
    .
    private RXTable table;
    .
    .
    public Component getTableCellRendererComponent (RXTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int col)

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    The basics area:

    a) first you create the JTable, which in turn will create the default TableColumnModel
    b) then you add custom renderers to each TableColumn

    Also, there is no need to 7 instances of the renderer. You only need to create 1 instance.

  9. #9
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Ok, I thought that's what I was doing, at least I am doing it how I learnt it. I made the change you suggested at the end, but don't understand what the difference is between what I am doing and what you are suggesting.

    Java Code:
    // Add Service Table to panel
    table = new RXTable(model);
    table.setAutoResizeMode(RXTable.AUTO_RESIZE_OFF);
    
    // Build the Service Table
    PopulateTable();
    
    add (new JScrollPane(table), gbcTablField);
    table.setAutoCreateRowSorter(true);
    table.setFillsViewportHeight(true);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setCellSelectionEnabled(true);
    table.getSelectionModel().addListSelectionListener(new RowListener());
    table.getSelectionModel().addListSelectionListener(new ColumnListener());
    
    TableColumnModel tcol = table.getColumnModel();
    TableCellRenderer tcr = new CustomTableCellRenderer();
    tcol.getColumn(clmnMileage).setCellRenderer(tcr);
    tcol.getColumn(clmnSvcDate).setCellRenderer(tcr);
    tcol.getColumn(clmnShop).setCellRenderer(tcr);
    tcol.getColumn(clmnInvoice).setCellRenderer(tcr);
    tcol.getColumn(clmnLabor).setCellRenderer(tcr);
    tcol.getColumn(clmnParts).setCellRenderer(tcr);
    tcol.getColumn(clmnCost).setCellRenderer(tcr);
    
    TableColumnAdjuster tca = new TableColumnAdjuster(table);
    tca.adjustColumns();

  10. #10
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

Similar Threads

  1. Selection in JTable
    By tremon in forum JDBC
    Replies: 1
    Last Post: 06-11-2010, 08:57 PM
  2. Problem in row selection in JTable
    By simmi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2010, 04:30 PM
  3. Jtable selection question
    By casid in forum New To Java
    Replies: 2
    Last Post: 02-04-2010, 07:11 PM
  4. Problem in JTable row selection
    By shanssat in forum AWT / Swing
    Replies: 1
    Last Post: 02-04-2009, 07:50 AM
  5. Multiple selection on JTable
    By hendrix79 in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 06:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •