Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Apply a renderer on a cell of a JTable

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Apply a renderer on a cell of a JTable

    Hi all

    I want to apply a renderer on a cell of my JTable, to do so I created a class named myRenderer :

    import java.awt.Component;
     
    import javax.swing.ImageIcon;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;
     
    public class MyRenderer extends DefaultTableCellRenderer {
     
      public Component getTableCellRendererComponent(JTable table, ImageIcon icon) {
     
        setIcon(icon);
        return this;
      }
    }


    And I use this piece of code to apply the renderer on the cell :

    MyRenderer renderer = new MyRenderer();
     
    renderer.getTableCellRendererComponent(table, icon);
     
    table.getColumnModel().getColumn(6).setCellRenderer(renderer);

    The problem is that the rebderer is applied on all the cells in the column 6, and I want it to be applied on one cell only (row/column) but I don't know how to do so ?

    Thanks in advance
    Last edited by Ben03; May 9th, 2014 at 03:38 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Apply a renderer on a cell of a JTable

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Apply a renderer on a cell of a JTable

    Hi, I'm glad to join this forum

    I'll have a look at the link you posted

    And for my request, I solved it already

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Apply a renderer on a cell of a JTable

    Glad you solved it!

    You can mark the thread "Solved" using the "Thread Tools" pulldown at the top of the thread. Marking it solved prevents others from spending more time on it and also indicates that the thread may contain a solution to the problem. Since your thread does not currently provide the solution, you may add the solution you discovered to help someone else.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Apply a renderer on a cell of a JTable

    I solved the problem without the use of a renderer.
    I just created a model for the JTable like this :

    DefaultTableModel model = new DefaultTableModel(new Object[][] {},new String[] {"Col1", "Col2", "Col3", "Col4", "Col5", "Col6", "Col7"}) {
    		    @Override
    		    public Class<?> getColumnClass(int column) {
    		        switch (column) {
    		            case 0: return ImageIcon.class;
    		            default: return String.class;
    		        }
    		    }
    		};
     
     
    		table = new JTable(model);


    So I just have to use the addRow(...) method with my model to add a row containing Strings and a ImageIcon as described in the model.

    Hopefully this will help somebody.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Apply a renderer on a cell of a JTable

    Excellent! Thank you for sharing the result.

Similar Threads

  1. Trying to Render a JTable Cell for an Image
    By AndyF in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2013, 11:24 AM
  2. JTable edit cell when focus is gained
    By TheQuickBrownFox in forum AWT / Java Swing
    Replies: 0
    Last Post: December 7th, 2012, 09:24 PM
  3. JTable with JButton in a cell
    By kpat in forum AWT / Java Swing
    Replies: 0
    Last Post: April 9th, 2012, 12:17 PM
  4. Table cell renderer
    By dibyayan in forum AWT / Java Swing
    Replies: 1
    Last Post: October 17th, 2011, 07:34 AM
  5. JTable prepareRenderer changes all cell colors
    By BrettStuart in forum AWT / Java Swing
    Replies: 9
    Last Post: July 21st, 2010, 02:51 AM