Results 1 to 10 of 10
- 07-09-2010, 01:16 PM #1
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?
- 07-09-2010, 08:47 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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
- 07-10-2010, 02:28 AM #3
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); } }
- 07-10-2010, 04:33 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
In your case you will get an error because RXTable is not part of the method signature. Just use JTable like you had before.Java Code:@Override public Component getTableCellRendererComponent (RXTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int col)
- 07-10-2010, 05:33 AM #5
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.
- 07-10-2010, 04:20 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 07-10-2010, 05:04 PM #7
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)
- 07-10-2010, 05:13 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 07-10-2010, 05:37 PM #9
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();
- 07-10-2010, 06:01 PM #10
Similar Threads
-
Selection in JTable
By tremon in forum JDBCReplies: 1Last Post: 06-11-2010, 08:57 PM -
Problem in row selection in JTable
By simmi in forum AWT / SwingReplies: 3Last Post: 03-23-2010, 04:30 PM -
Jtable selection question
By casid in forum New To JavaReplies: 2Last Post: 02-04-2010, 07:11 PM -
Problem in JTable row selection
By shanssat in forum AWT / SwingReplies: 1Last Post: 02-04-2009, 07:50 AM -
Multiple selection on JTable
By hendrix79 in forum New To JavaReplies: 2Last Post: 01-30-2009, 06:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks