Results 1 to 7 of 7
  1. #1
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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;
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JTable Cell Color Follow Drag Event

    Use the methods that convert between view and model index

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: JTable Cell Color Follow Drag Event

    How would I use them?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JTable Cell Color Follow Drag Event

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

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

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

    Default 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)))))

  7. #7
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: JTable Cell Color Follow Drag Event

    That did it...thank you! I have been stuck on this for weeks.

Similar Threads

  1. Drag question: event not recurring
    By ehudfisher in forum AWT / Swing
    Replies: 2
    Last Post: 07-13-2011, 02:21 PM
  2. Replies: 2
    Last Post: 12-20-2010, 10:37 PM
  3. Replies: 5
    Last Post: 12-20-2010, 09:31 PM
  4. Color cell in JTable
    By ippacciani in forum AWT / Swing
    Replies: 3
    Last Post: 03-25-2009, 12:53 PM
  5. Event after comit the value in cell editor
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 0
    Last Post: 10-16-2008, 08:48 AM

Posting Permissions

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