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

    Default Programatically call Render

    I have a JTable renderer that colors cell backrgounds based on their text, but when I drag a column left or right the colored cells do not move. They stay at the same index. How do I call my custom renderer after a drag event?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Programatically call Render

    Your best bet here is to show us compilable and runnable code. We don't want to see your whole program, especially if it's larger than 60 lines long, but rather you should condense your code into the smallest bit that still compiles and runs, has no extra code that's not relevant to your problem, but still demonstrates your problem, in other words, an SSCCE (Short, Self Contained, Correct (Compilable), Example). For more info on SSCCEs please look here:

    SSCCE

    Again, if the code is compilable and runnable more people will be able to help you.

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

    Default Re: Programatically call Render

    Thanks for the response, but I'm not sure what code to show. The renderer? That works just fine (on the initial render). I'm not having a compilation problem of any sort. I just don't know how to approach this specific problem. I assume it will be of the form
    Java Code:
    protected void tblMyTable[ColumnDragged]([DragEvent] e)
    {
    myTable.reRender()
    }
    My questions are, what event should I listen for? And how do I tell the table to to reRender based on the CustomRenderClass that it already uses?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Programatically call Render

    You didn't read the link



    Please have a look at my SSCCE link above before replying again as we will be much better able to help you if you put the effort into making it easier for us to understand and reproduce your problem.
    Last edited by Fubarable; 11-18-2011 at 09:46 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Programatically call Render

    For instance, in my SSCCE, the colored cell moves when I drag my column:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class RendererEg {
       public static void main(String[] args) {
          Integer[][] data = { { 1, 2, 3 }, { 4, 5, 6 }, { -7, -8, -9 },
                { -10, -11, -12 }, { 13, 14, 15 }, { -16, -17, -18 } };
          String[] titles = { "A", "Rendered", "C" };
          DefaultTableModel model = new DefaultTableModel(data, titles) {
             @Override
             public Class<?> getColumnClass(int columnIndex) {
                return getValueAt(0, columnIndex).getClass();
             }
          };
    
          JTable table = new JTable(model);
    
          table.getColumnModel().getColumn(1).setCellRenderer(new MyCellRenderer());
    
          JOptionPane.showMessageDialog(null, new JScrollPane(table));
       }
    
       private static class MyCellRenderer extends DefaultTableCellRenderer {
          public MyCellRenderer() {
             setHorizontalAlignment(SwingConstants.RIGHT);
             setOpaque(true);
          }
    
          @Override
          public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
             Integer intValue = (Integer) value;
             if (intValue < 0) {
                setBackground(Color.pink);
             } else {
                setBackground(null);
             }
             return super.getTableCellRendererComponent(table, value, isSelected,
                   hasFocus, row, column);
          }
       }
    }
    Note that I'm adding my cell renderer to a specific column.
    Last edited by Fubarable; 11-18-2011 at 11:10 PM. Reason: code updated to use Default model

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

    Default Re: Programatically call Render

    Again, thank you for the reply. Sorry I didn't get back to you on Friday, but they don't pay me to stay past 4:00 lol. I have put together a SSCCE that demonstrates my problem.
    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][2];
    		Object[] headers = new Object[2];
    		
    		data[0][0]="1";
    		data[1][0]="1";
    		data[2][0]="1";
    		data[0][1]="1";
    		data[1][1]="2";
    		data[2][1]="2";
    		
    		headers[0]="column1";
    		headers[1]="column2";
    		
    		((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);
    		if(row>0)
    			{
    			String curValue = table.getModel().getValueAt(row, column).toString();
    			String prevValue = table.getModel().getValueAt(row-1, column).toString();
    			if(!curValue.equals(prevValue))
    			{
    				c.setBackground(Color.RED);
    			}
    			else
    			{
    				c.setBackground(Color.WHITE);
    		
    			}
    		}
    		return c;
    	}
    }
    If I use the value variable as oppose table.getModel().getValueAt(row, column).toString() then the color does follow when I drag. Unfortunately, the point of this renderer is to color any change in red, so I need to access previous rows. ANy help would be appreciated!
    Last edited by joeyvitoro; 11-21-2011 at 02:05 PM.

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

    Default Re: Programatically call Render

    And how do I specify java code in this forum? I looked at the link in your signature and it only shows how to specify php...

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Programatically call Render

    Your problem is that when dealing with a changing view, you must convert your column index to the view's index. For instance, please see the changes in your renderer:

    Java Code:
    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);
          if (row > 0) {
             int viewColumn = table.convertColumnIndexToView(column);
             
             //String curValue = table.getModel().getValueAt(row, column).toString();
             //String prevValue = table.getModel().getValueAt(row - 1, column).toString();
    
             // use the view's column index
             String curValue = table.getModel().getValueAt(row, viewColumn).toString();
             String prevValue = table.getModel().getValueAt(row - 1, viewColumn).toString();
             if (!curValue.equals(prevValue)) {
                c.setBackground(Color.RED);
             } else {
                c.setBackground(Color.WHITE);
    
             }
          }
          return c;
       }
    }

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

    Default Re: Programatically call Render

    Thanks...works great!

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

    Default Re: Programatically call Render

    Unfortunately, I spoke too soon. It appeared to work at first, but I noticed some odd behavior. The background color will now follow 1 row in either direction, but no further. This code demonstrates the behavior...

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

Similar Threads

  1. programatically fix HEAP SPACE error
    By nin888 in forum New To Java
    Replies: 1
    Last Post: 11-14-2011, 08:47 AM
  2. Replies: 1
    Last Post: 10-26-2011, 11:46 PM
  3. Create an event programatically
    By dalu in forum New To Java
    Replies: 3
    Last Post: 07-13-2011, 12:46 PM
  4. How to create Table programatically in JSF.
    By JavaEmpires in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 06-25-2008, 11:27 AM
  5. Creating an appointment programatically
    By coatsy in forum New To Java
    Replies: 3
    Last Post: 01-08-2008, 06:18 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
  •