Results 1 to 16 of 16
Like Tree5Likes
  • 1 Post By tomtaila
  • 1 Post By Norm
  • 2 Post By DarrylBurke
  • 1 Post By tomtaila

Thread: Why is my custom cell renderer 'looping'?

  1. #1
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Why is my custom cell renderer 'looping'?

    I'm trying to create my own cell renderer for a JList. For some reason though it seems to be 'looping.' Instead of grabbing a single index from my list its grabbing all of them! I have no idea why its doing this and I've tried a million and one things and still am no closer to solving it. Any ideas?

    Heres the code for the custom renderer class:


    Java Code:
    package gui;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    
    import notePackage.Note;
    import notePackage.NoteBook;
    
    import javax.swing.DefaultListCellRenderer;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.ListCellRenderer;
    
    
    public class NoteListRenderer extends JPanel implements ListCellRenderer{
    
    	
    	Note note;
    	JLabel jl_titleLabel;
    	JLabel jl_dateLabel;
    	
    	//DIMENSION CONSTANTS//////
    	private static final Dimension jl_titleLabelSize = new Dimension(274, 20);
    	private static final Dimension jl_dateLabelSize = new Dimension(274, 10);
    	private static final Dimension cellSize = new Dimension(274, 75);
    	
    	
    	//COLOR CONSTANTS///////
    	private static Color background;
        private static Color foreground;
    	
    	
        public NoteListRenderer() {
            setOpaque(true);
        }
        
    	@Override
    	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    		
    		note = (Note) value;
    		setPreferredSize(cellSize);
    		setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    		
    		jl_titleLabel = new JLabel(note.getTitle());
    		jl_titleLabel.setPreferredSize(jl_titleLabelSize);
    		
    		//jl_dateLabel = new JLabel(note.dateCreatedToString());
    		//jl_dateLabel.setPreferredSize(jl_dateLabelSize);
    		
    		add(jl_titleLabel);
    		//add(jl_dateLabel);
    		
    
    		
    		// check if this cell represents the current DnD drop location
            JList.DropLocation dropLocation = list.getDropLocation();
            if (dropLocation != null
                    && !dropLocation.isInsert()
                    && dropLocation.getIndex() == index) {
    
                background = Color.BLUE;
                foreground = Color.WHITE;
    
            // check if this cell is selected
            } else if (isSelected) {
                background = Color.RED;
                foreground = Color.WHITE;
    
            // unselected, and not the DnD drop location
            } else {
                background = Color.WHITE;
                foreground = Color.BLACK;
            };
    
            setBackground(background);
            setForeground(foreground);
    
    		
    		
    		return this;
    	}
    
    }

    and heres the relevant code for my GUI class (not that I think this bit is important):

    Java Code:
    user = new User("Tom", "TomTaila@Hotmail.com");
    		user.addNoteBook(new NoteBook("Book A"));
    		user.addNoteBook(new NoteBook("Book B"));
    		user.addNoteBook(new NoteBook("Book C"));
    		user.addNoteBook(new NoteBook("Book D"));
    		user.getNoteBooks().get(0).addNote("NOTE 1");
    		user.getNoteBooks().get(0).addNote("NOTE 2");
    		user.getNoteBooks().get(0).addNote("NOTE 3");
    		user.getNoteBooks().get(0).addNote("NOTE 4");
    		
    		
    		
    		jls_noteBooksList = new JList(user.getNoteBookTitles().toArray());
    		jls_noteBooksList.setBackground(mainLeftPanelColor);
    		jls_noteBooksList.setFixedCellWidth(jls_noteBooksListCellWidth);
    		jls_noteBooksList.setFixedCellHeight(jls_noteBooksListCellHeight);
    		jls_noteBooksList.setSelectionBackground(new Color(165, 209, 209));
    		
    		//jls_noteList = new JList(user.getNoteBooks().get(0).getAllTitles().toArray());
    		jls_noteList = new JList(user.getNoteBooks().get(0));
    		jls_noteList.setCellRenderer(new NoteListRenderer());

    And finally heres a print screen of whats happening when I run the program:

    Why is my custom cell renderer 'looping'?-screen-shot-2012-09-17-16.21.18.png
    Last edited by tomtaila; 09-17-2012 at 05:25 PM.

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

    Default Re: Why is my custom cell renderer 'looping'?

    Moved from New to Java.

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Not all your code.

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

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

    Default Re: Why is my custom cell renderer 'looping'?

    Quote Originally Posted by tomtaila View Post
    I'm trying to create my own cell renderer for a JList. For some reason though it seems to be 'looping.' Instead of grabbing a single index from my list its grabbing all of them! I have no idea why its doing this and I've tried a million and one things and still am no closer to solving it. Any ideas?
    I thought that this was normal behavior for the renderer since after all it must render all of the cells. In addition to Darryl's great suggestion, please describe the purported misbehavior in greater detail.

  4. #4
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Re: Why is my custom cell renderer 'looping'?

    Quote Originally Posted by Fubarable View Post
    I thought that this was normal behavior for the renderer since after all it must render all of the cells. In addition to Darryl's great suggestion, please describe the purported misbehavior in greater detail.
    Sure it must render all of the cells, but the array consists of 4 elements: "Note 1", "Note 2", "Note 3" and "Note 4".
    So what its doing is rendering the entire array in each cell, as appose to associating the first index of the array (In this case "Note 1") with the first cell, the second index of the array with the second cell etc..


    Also, I did actually post a lot of unnecessary code there sorry, really the only relevant code is the "NoteListRenderer" class.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Why is my custom cell renderer 'looping'?

    the only relevant code is the "NoteListRenderer" class.
    How can the code be compiled and executed for testing? Can you post some code that will compile, execute and show the problem?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Re: Why is my custom cell renderer 'looping'?

    My first post has a screen shot of whats happening, I selected one of the cells (the one with the red background) to highlight how each cell looks when I compile the program. I'll post the code you asked for below, the panel most concerned is the 'jp_mainCenterPanel' and the JList in question is named 'jls_noteList'

    Java Code:
    public class GUI {
    	private User user;
    
    //FRAMES AND PANELS/////////////
    	/** Frame / main window of the application */
    	JFrame jf_frame = new JFrame();
    	/** Main Panel */
    	JPanel jp_mainPanel =  new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    	/** Main left panel */
    	JPanel jp_mainLeftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    	/** Main right panel */
    	JPanel jp_mainRightPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    	/** Main top panel */
    	JPanel jp_mainTopPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    	/** Main center panel */
    	JPanel jp_mainCenterPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    	/** Main left panel */
    	JPanel jp_topLeftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    	
    	//LABELS////////
    	JLabel jl_noteBooksLabel = new JLabel("\tNOTEBOOKS");
    	JLabel jl_notesLabel = new JLabel("\tNOTES");
    	
    	//LISTS///////
    	JList jls_noteBooksList;
    	JList jls_noteList;
    	
    	
    	
    	
    	//DIMENSION CONSTANTS
    	private static final Dimension jf_frameSize = new Dimension(1200, 768);
    	private static final Dimension jp_mainPanelSize = jf_frameSize;
    	private static final Dimension jp_mainTopPanelSize = new Dimension(1200, 118);
    	private static final Dimension jp_mainLeftPanelSize = new Dimension(200, 650);
    	private static final Dimension jp_mainCenterPanelSize = new Dimension(275, 650);
    	private static final Dimension jp_mainRightPanelSize = new Dimension(725, 650);
    	private static final Dimension jp_topLeftPanelSize = new Dimension(200, 118);
    	
    	private static final Dimension jl_noteBooksLabelSize = new Dimension(200, 30);
    	private static final Dimension jl_notesLabelSize = new Dimension(275, 30);
    	
    	private static final int jls_noteBooksListCellWidth = 199;
    	private static final int jls_noteBooksListCellHeight = 20;
    	
    	private static final int jls_noteListCellWidth = 274;
    	private static final int jls_noteListCellHeight = 75;
    	
    	
    	//COLOR CONSTANTS
    	private static final Color mainTopPanelColor = new Color(224,224,224);
    	private static final Color mainLeftPanelColor = new Color(220,238,238);
    	private static final Color mainCenterPanelColor = new Color(240,248,248);
    	private static final Color borderShadowColor = new Color(134, 134, 134);
    	
    	//BORDER CONSTANTS
    	private static final Border bottomBorder = new MatteBorder(0, 0, 1, 0, borderShadowColor);//only has a bottom border
    	private static final Border rightBorder = new MatteBorder(0, 0, 0, 1, borderShadowColor);//only has a right side border
    	
    	
    	
    	
    	
    	/** Starts the GUI application */
    	public static void main(String[] args)
    	{
    		GUI gui = new GUI();
    		gui.start();
    	}
    
    
    /** Sets up the GUI */
    	public void start()
    	{
    		user = new User("Tom", "TomTaila@Hotmail.com");
    
                    user.addNoteBook(new NoteBook("Book A"));
    		user.addNoteBook(new NoteBook("Book B"));
    		user.addNoteBook(new NoteBook("Book C"));
    		user.addNoteBook(new NoteBook("Book D"));
    
                   //NOTES///////
    		user.getNoteBooks().get(0).addNote("NOTE 1");
    		user.getNoteBooks().get(0).addNote("NOTE 2");
    		user.getNoteBooks().get(0).addNote("NOTE 3");
    		user.getNoteBooks().get(0).addNote("NOTE 4");
    		
    		//NOTEBOOKS LIST//////////
    		jls_noteBooksList = new JList(user.getNoteBookTitles().toArray());
    		jls_noteBooksList.setBackground(mainLeftPanelColor);
    		jls_noteBooksList.setFixedCellWidth(jls_noteBooksListCellWidth);
    		jls_noteBooksList.setFixedCellHeight(jls_noteBooksListCellHeight);
    		jls_noteBooksList.setSelectionBackground(new Color(165, 209, 209));
    		
    		//NOTES LIST//////////
    		jls_noteList = new JList(user.getNoteBooks().get(0));
    		jls_noteList.setCellRenderer(new NoteListRenderer());
    	
    		//SET UP MAIN FRAME AND PANEL////////
    
    		setUpFrame();
    		setUpMainPanel();
    		
    		//MAIN TOP PANEL//////
    		
    		setUpMainTopPanel();
    		
    		//MAIN LEFT PANEL//////
    		
    		setUpMainLeftPanel();
    		
    		//MAIN CENTER PANEL//////
    		
    		setUpMainCenterPanel();
    			
    		//MAIN RIGHT PANEL//////
    		
    		setUpMainRightPanel();
    		
    	}
    	
    	//SETUP MAIN_PANEL///
    	public void setUpMainPanel()
    	{
    		jp_mainPanel.setMinimumSize(jf_frameSize);
    		jp_mainPanel.setSize(jf_frameSize);
    		jp_mainPanel.setBackground(Color.BLACK);
    		jp_mainPanel.add(jp_mainTopPanel);
    		jp_mainPanel.add(jp_mainLeftPanel);
    		jp_mainPanel.add(jp_mainCenterPanel);
    		jp_mainPanel.add(jp_mainRightPanel);
    	}
    	
    	
    	public void setUpFrame()
    	{
    		jf_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		jf_frame.setSize(jf_frameSize);
    		jf_frame.setResizable(false);
    		jf_frame.getContentPane().add(jp_mainPanel);
    		jf_frame.setVisible(true);
    	}
    	
    	public void setUpMainTopPanel()
    	{
    		jp_mainTopPanel.setBackground(mainTopPanelColor);
    		jp_mainTopPanel.setPreferredSize(jp_mainTopPanelSize);//main left panel is one quarter of entire frame width
    		jp_mainTopPanel.setBorder(bottomBorder);
    		jp_mainTopPanel.add(jp_topLeftPanel);
    		jp_topLeftPanel.setBorder(new MatteBorder(0, 0, 1, 1, borderShadowColor));//gives it bottom + right borders TODO remove right border
    		jp_topLeftPanel.setBackground(mainTopPanelColor);
    		jp_topLeftPanel.setPreferredSize(jp_topLeftPanelSize);
    	}
    	
    	public void setUpMainLeftPanel()
    	{
    		jp_mainLeftPanel.setBackground(mainLeftPanelColor);
    		jp_mainLeftPanel.setPreferredSize(jp_mainLeftPanelSize);//main left panel is one fifth of entire frame width
    		jp_mainLeftPanel.setBorder(rightBorder);
    		
    		jl_noteBooksLabel.setPreferredSize(jl_noteBooksLabelSize);
    		jl_noteBooksLabel.setBorder(new MatteBorder(0, 0, 1, 0, borderShadowColor));
    		jp_mainLeftPanel.add(jl_noteBooksLabel);
    		jp_mainLeftPanel.add(jls_noteBooksList);
    	}
    	
    	
    	public void setUpMainCenterPanel()
    	{
    		jp_mainCenterPanel.setBackground(mainCenterPanelColor);
    		jp_mainCenterPanel.setPreferredSize(jp_mainCenterPanelSize);//main left panel is one fifth of entire frame width
    		jp_mainCenterPanel.setBorder(rightBorder);
    		
    		jl_notesLabel.setPreferredSize(jl_notesLabelSize);
    		jl_notesLabel.setBorder(new MatteBorder(0, 0, 1, 0, borderShadowColor));
    		jp_mainCenterPanel.add(jl_notesLabel);
    		jp_mainCenterPanel.add(jls_noteList);
    	}
    	
    	
    	public void setUpMainRightPanel()
    	{
    		jp_mainRightPanel.setBackground(Color.WHITE);
    		jp_mainRightPanel.setPreferredSize(jp_mainRightPanelSize);//main right panel is 4 fifths of entire frame width
    	}
    	
    	
    	
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Why is my custom cell renderer 'looping'?

    The posted code needs import statements to compile.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Re: Why is my custom cell renderer 'looping'?

    Whoops! Sorry I didn't think you wanted that. Should I post the 'user' class and 'noteBook' class also? (Coz you have to import them I mean)


    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.MatteBorder;
    
    import notePackage.NoteBook;
    
    import user.User;

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

    Default Re: Why is my custom cell renderer 'looping'?

    Quote Originally Posted by tomtaila View Post
    Whoops! Sorry I didn't think you wanted that. Should I post the 'user' class and 'noteBook' class also? (Coz you have to import them I mean
    Didn't you bother to go through the SSCCE page? or did that seem too much like hard work?

    Your stated problem is with a custom renderer. Compose a SSCCE with a JList that uses the custom renderer. That shouldn't need any supporting non-JDK classes.

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

  10. #10
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Re: Why is my custom cell renderer 'looping'?

    Oh I see what you mean, I'm very sorry lol ok I'll try to set something up in SSCCE that works.
    DarrylBurke likes this.

  11. #11
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Re: Why is my custom cell renderer 'looping'?

    Right, I think I've done this correctly, in terms of being compliant with SSCCE, sorry if its not perfect its my first time trying this and I'm not the most experienced developer, anyways, here it is I hope its good:


    Java Code:
    package gui;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.util.ArrayList;
    import javax.swing.*;
    import javax.swing.event.ListDataListener;
    
    
    public class SwingListTest{
    	private NoteBook noteBook;
    	private Note[] notes = {new Note("NOTE 1"), new Note("NOTE 2"), new Note("NOTE 3"), new Note("NOTE 4")};
    	//private 
    	private JList jl_list;
    	
    	public void setUp()
    	{
    		JFrame jf_frame = new JFrame();
    		jf_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		jf_frame.setSize(275, 650);
    		jf_frame.setResizable(false);
    		jf_frame.setVisible(true);
    		
    		JPanel jp_panel = new JPanel();
    		jp_panel.add(jl_list);
    		jf_frame.getContentPane().add(jp_panel);
    		jf_frame.pack();
    	}
    	
    	public SwingListTest()
    	{
    		noteBook = new NoteBook("NOTEBOOK 1");
    		noteBook.addNote(notes[0]); 	
    		noteBook.addNote(notes[1]); 	
    		noteBook.addNote(notes[2]); 	
    		noteBook.addNote(notes[3]);
    		
    		jl_list = new JList(this.noteBook);
    		jl_list.setCellRenderer(new NoteListRenderer());
    	}
    	
    	public static void main(String[] args)
    	{
    		SwingListTest SLT = new SwingListTest();
    		SLT.setUp();
    	}
    }
    
    
    
    
    
    
    
    
    	class NoteBook implements ListModel {
    		/** Note array to represent the notebook */
    		private ArrayList<Note> noteBook; 
    		/** Title of the Notebook */
    		private String title;
    	
    		public NoteBook(String title)
    		{
    			this.noteBook = new ArrayList<Note>();
    			this.title = title;
    		}
    		
    		public void addNote(Note note)
    		{
    			this.noteBook.add(note);
    		}
    	
    		@Override
    		public void addListDataListener(ListDataListener arg0) {
    			// TODO Auto-generated method stub
    		}
    		@Override
    		public Object getElementAt(int x) {
    			return this.noteBook.get(x);
    		}
    		@Override
    		public int getSize() {
    			return this.noteBook.size();
    		}
    		@Override
    		public void removeListDataListener(ListDataListener arg0) {
    			// TODO Auto-generated method stub
    	
    		}
    	}
    
    
    	class Note {
    		/** Title of note */
    		private String title;
    
    		/**Creates a new Note 
    		 * @param title Title of the new note
    		 */
    		public Note(String title)
    		{
    			this.title = title;
    		}
    
    		/** Returns title of note */
    		public String getTitle()
    		{
    			return this.title;
    		}	
    	}
    
    
    
    	class NoteListRenderer extends JPanel implements ListCellRenderer{
    		Note note;
    		JLabel jl_titleLabel;
    		JLabel jl_dateLabel;
    
    		//DIMENSION CONSTANTS//////
    		private static final Dimension jl_titleLabelSize = new Dimension(274, 20);
    		private static final Dimension jl_dateLabelSize = new Dimension(274, 10);
    		private static final Dimension cellSize = new Dimension(274, 75);
    
    		//COLOR CONSTANTS///////
    		private static Color background;
    		private static Color foreground;
    
    		//CONSTRUCTOR////////
    		public NoteListRenderer() {
    			setPreferredSize(cellSize);
    			setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    			setOpaque(true);
    		}
    
    		@Override
    		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    
    			note = (Note) value;
    
    			jl_titleLabel = new JLabel(note.getTitle());
    			jl_titleLabel.setPreferredSize(jl_titleLabelSize);
    
    			add(jl_titleLabel);
    
    			if (isSelected) {
    				background = Color.RED;
    				foreground = Color.WHITE;
    
    				// unselected, and not the DnD drop location
    			} else {
    				background = Color.WHITE;
    				foreground = Color.BLACK;
    			};
    
    			setBackground(background);
    			setForeground(foreground);
    
    
    
    			return this;
    		}
    
    	}

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Why is my custom cell renderer 'looping'?

    An observation:
    What do you see if you add these two lines to the list of calls to addNote()?
    Java Code:
    		noteBook.addNote(notes[0]);     //<<<<<<<<<<<  Now 5 groups of 4
    		noteBook.addNote(notes[0]);     //<<<<<<<<<<<  Now 6 groups of 4
    It appears there are 4 lines added to the list for each call to addNote().
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Re: Why is my custom cell renderer 'looping'?

    I think actually it becomes 5 groups of 5, and then 6 groups of 6. I've also noticed that when I change the 'getSize()' method in my custom listModel it changes everything, for example if I change it from this:

    Java Code:
    public int getSize() {
    			return this.noteBook.size();
    		}
    which returns the size of the arraylist (originally a value of 4), to this:

    Java Code:
    public int getSize() {
    			return 2;
    		}
    I get two cells containing two elements within my arraylist.

  14. #14
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Re: Why is my custom cell renderer 'looping'?

    Quote Originally Posted by tomtaila View Post
    I think actually it becomes 5 groups of 5, and then 6 groups of 6.
    You can only see this is you change the following from:

    Java Code:
    private static final Dimension cellSize = new Dimension(274, 75);
    to

    Java Code:
    private static final Dimension cellSize = new Dimension(274, 150);

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

    Default Re: Why is my custom cell renderer 'looping'?

    Every time through the getListCellRendererComponent(...) method, you're adding another JLabel to the JPanel that is returned as the renderer component. Don't do that.
    Java Code:
    class NoteListRenderer extends JPanel implements ListCellRenderer {
    
      ...
      JLabel label = new JLabel();
    
      public NoteListRenderer() {
        ...
        add(label);
      }
    
      @Override
      public Component getListCellRendererComponent(JList list, Object value,
              int index, boolean isSelected, boolean cellHasFocus) {
    
        note = (Note) value;
        label.setText(note.getTitle());
    
        //jl_titleLabel = new JLabel(note.getTitle());
        //jl_titleLabel.setPreferredSize(jl_titleLabelSize);
        //add(jl_titleLabel);
    
        ...
        return this;
      }
    }
    Additionally, always avoid setting a preferredSize. Use an appropriate layout manager and that will compute an appropriate preferredSize.

    Also, when posting code on a forum, please remove inane comments, empty statements, unused variables and multiple blank lines. All of these make code more difficult to read, and will result in less members even trying to help out. Examples of each, form your 'SSCCE':
    Java Code:
    //CONSTRUCTOR////////
            public NoteListRenderer() {
    Java Code:
                    foreground = Color.BLACK;
                }; // <-- empty statement
     
                setBackground(background);
    Java Code:
    JLabel jl_dateLabel;
    Java Code:
    }
     
     
     
     
     
     
     
     
        class NoteBook implements ListModel {
    db
    Last edited by DarrylBurke; 09-18-2012 at 07:24 PM.
    tomtaila and Fubarable like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default Re: Why is my custom cell renderer 'looping'?

    Wow thanks so much! That worked, and I'll take on board what you said about the SSCCE + setting preferred sizes. Thank you all actually, I know my post hasn't been exactly easy to follow so thanks for being patient with me. In the future my posts will be clearer I promise!
    Fubarable likes this.

Similar Threads

  1. Web PDF Renderer
    By java software in forum Java Software
    Replies: 0
    Last Post: 10-20-2011, 06:41 PM
  2. looping around a table renderer, is it possible?
    By shy_ted in forum New To Java
    Replies: 1
    Last Post: 10-14-2010, 02:28 PM
  3. Facing the Problem When I placed my Custom ComboCheckBox in JTable cell
    By miryala.rahul@gmail.com in forum AWT / Swing
    Replies: 0
    Last Post: 06-26-2010, 03:14 PM
  4. Custom renderer (almost works)
    By geforce2000 in forum AWT / Swing
    Replies: 11
    Last Post: 12-13-2009, 09:15 PM
  5. jtable cell renderer
    By ankitmcgill in forum New To Java
    Replies: 2
    Last Post: 05-22-2009, 01:08 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
  •