Results 1 to 16 of 16
- 09-17-2012, 05:22 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 21
- Rep Power
- 0
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:
Last edited by tomtaila; 09-17-2012 at 05:25 PM.
- 09-17-2012, 05:42 PM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
Re: Why is my custom cell renderer 'looping'?
- 09-18-2012, 02:00 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 21
- Rep Power
- 0
Re: Why is my custom cell renderer 'looping'?
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.
- 09-18-2012, 02:47 PM #5
Re: Why is my custom cell renderer 'looping'?
How can the code be compiled and executed for testing? Can you post some code that will compile, execute and show the problem?the only relevant code is the "NoteListRenderer" class.If you don't understand my response, don't ignore it, ask a question.
- 09-18-2012, 03:10 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 21
- Rep Power
- 0
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 } }
- 09-18-2012, 03:16 PM #7
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.
- 09-18-2012, 03:21 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 21
- Rep Power
- 0
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;
- 09-18-2012, 03:41 PM #9
Re: Why is my custom cell renderer 'looping'?
Why do they call it rush hour when nothing moves? - Robin Williams
- 09-18-2012, 04:04 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 21
- Rep Power
- 0
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.
- 09-18-2012, 06:35 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 21
- Rep Power
- 0
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; } }
- 09-18-2012, 06:56 PM #12
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()?It appears there are 4 lines added to the list for each call to addNote().Java Code:noteBook.addNote(notes[0]); //<<<<<<<<<<< Now 5 groups of 4 noteBook.addNote(notes[0]); //<<<<<<<<<<< Now 6 groups of 4
If you don't understand my response, don't ignore it, ask a question.
- 09-18-2012, 07:11 PM #13
Member
- Join Date
- Jul 2011
- Posts
- 21
- Rep Power
- 0
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:
which returns the size of the arraylist (originally a value of 4), to this:Java Code:public int getSize() { return this.noteBook.size(); }
I get two cells containing two elements within my arraylist.Java Code:public int getSize() { return 2; }
- 09-18-2012, 07:21 PM #14
Member
- Join Date
- Jul 2011
- Posts
- 21
- Rep Power
- 0
- 09-18-2012, 07:22 PM #15
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.
Additionally, always avoid setting a preferredSize. Use an appropriate layout manager and that will compute an appropriate preferredSize.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; } }
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;
dbJava Code:} class NoteBook implements ListModel {Last edited by DarrylBurke; 09-18-2012 at 07:24 PM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 09-18-2012, 07:33 PM #16
Member
- Join Date
- Jul 2011
- Posts
- 21
- Rep Power
- 0
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!
Similar Threads
-
Web PDF Renderer
By java software in forum Java SoftwareReplies: 0Last Post: 10-20-2011, 06:41 PM -
looping around a table renderer, is it possible?
By shy_ted in forum New To JavaReplies: 1Last Post: 10-14-2010, 02:28 PM -
Facing the Problem When I placed my Custom ComboCheckBox in JTable cell
By miryala.rahul@gmail.com in forum AWT / SwingReplies: 0Last Post: 06-26-2010, 03:14 PM -
Custom renderer (almost works)
By geforce2000 in forum AWT / SwingReplies: 11Last Post: 12-13-2009, 09:15 PM -
jtable cell renderer
By ankitmcgill in forum New To JavaReplies: 2Last Post: 05-22-2009, 01:08 AM


5Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks