Results 1 to 7 of 7
- 11-28-2011, 06:55 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
JTable Cell Color Follow Drag Event
I asked this before and mistakenly marked it as solved. Turns out I was wrong. The following code makes a simple table. If you drag the first column one spot to the right, the cell color follows the cells. If you move farther than 1 column, though, the color gets messed up. Any ideas? Thanks!
Java Code:import java.awt.*; import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellRenderer; public class Table extends JFrame { public Table() { initComponents(); } private void initComponents() { scrollPane1 = new JScrollPane(); table1 = new JTable(); Container contentPane = getContentPane(); contentPane.setLayout(null); { scrollPane1.setViewportView(table1); } contentPane.add(scrollPane1); scrollPane1.setBounds(new Rectangle(new Point(0, 0), scrollPane1.getPreferredSize())); { Dimension preferredSize = new Dimension(); for(int i = 0; i < contentPane.getComponentCount(); i++) { Rectangle bounds = contentPane.getComponent(i).getBounds(); preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width); preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height); } Insets insets = contentPane.getInsets(); preferredSize.width += insets.right; preferredSize.height += insets.bottom; contentPane.setMinimumSize(preferredSize); contentPane.setPreferredSize(preferredSize); } pack(); setLocationRelativeTo(getOwner()); } private JScrollPane scrollPane1; private JTable table1; public static void main(String[] args) { Table myTable = new Table(); myTable.setVisible(true); Object[][] data = new Object[3][4]; Object[] headers = new Object[4]; data[0][0]="1"; data[1][0]="1"; data[2][0]="1"; data[0][1]="1"; data[1][1]="2"; data[2][1]="2"; data[0][2]="1"; data[1][2]="2"; data[2][2]="2"; data[0][3]="1"; data[1][3]="2"; data[2][3]="2"; ((DefaultTableModel)myTable.table1.getModel()).setDataVector(data,headers); TableCellRenderer renderer = new CustomTableCellRenderer(); myTable.table1.setDefaultRenderer(Object.class, renderer); myTable.table1.repaint(); } } class CustomTableCellRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); int viewColumn = table.convertColumnIndexToView(column); if(row >0 && (!(table.getModel().getValueAt(row, viewColumn).equals(table.getModel().getValueAt(row-1, viewColumn))))) { c.setBackground(Color.PINK); } else { c.setBackground(Color.WHITE); } return c; } }
- 11-28-2011, 07:02 PM #2
Re: JTable Cell Color Follow Drag Event
Use the methods that convert between view and model index
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-28-2011, 08:34 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: JTable Cell Color Follow Drag Event
How would I use them?
- 11-30-2011, 07:59 AM #4
Re: JTable Cell Color Follow Drag Event
Why do they call it rush hour when nothing moves? - Robin Williams
- 11-30-2011, 01:17 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: JTable Cell Color Follow Drag Event
I did, to one other site, 36 hours later when I was still stumped. I do appreciate the response you provided, but I'm not sure what it means. If you could provide a bit more explanation I would be very greatful.
- 11-30-2011, 05:29 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: JTable Cell Color Follow Drag Event
Renderers always use the view indexes. The problem with the code is that sometimes you access the model and sometimes you access the view.
Java Code:// int viewColumn = table.convertColumnIndexToView(column); // this already is the view index // if(row >0 && (!(table.getModel().getValueAt(row, viewColumn).equals(table.getModel().getValueAt(row-1, viewColumn))))) // access the data via the table and it will do the view/model conversions for you if(row >0 && (!(table.getValueAt(row, column).equals(table.getValueAt(row-1, column)))))
- 11-30-2011, 06:36 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
Drag question: event not recurring
By ehudfisher in forum AWT / SwingReplies: 2Last Post: 07-13-2011, 01:21 PM -
I Only need the color of a cell to change not the entire row
By RickAintree in forum New To JavaReplies: 2Last Post: 12-20-2010, 09:37 PM -
How to change color of a cell in a Jtable based on a criteria
By RickAintree in forum New To JavaReplies: 5Last Post: 12-20-2010, 08:31 PM -
Color cell in JTable
By ippacciani in forum AWT / SwingReplies: 3Last Post: 03-25-2009, 11:53 AM -
Event after comit the value in cell editor
By Gajesh Tripathi in forum AWT / SwingReplies: 0Last Post: 10-16-2008, 07:48 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks