1

I am doing custom rendering in a JList. My custom ListCellRenderer uses a static String so that it can test the value of a particular variable against the previous occurance of that variable.

    private static String lastUsername = "";

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean  isSelected, boolean cellHasFocus) {
        User user = (User)value;
        if (lastUsername == user.getUsername()) {
            // Do something
        } else {
            // Do something else
            lastUsername = user.getUsername();
        }

Now, that works fine when the program first loads but if I then scroll the list it causes problems as the lastUsername variable is still set to the last username it encountered when in fact it should be an empty string. Basically lastUsername needs to be reset to an empty string before or after all the cells have been rendered. Does anyone know if there is an JList post/pre AllCellsRendered event/hook that I can override to achieve this?

csss
  • 1,937
  • 2
  • 14
  • 24
  • 1
    Attributes declared `static` are often a bug waiting to surface, rarely a solution. – Andrew Thompson Mar 23 '12 at 08:46
  • 2
    the logic is wrong: there is no ordering whatever in cell repaint calls (and calls to grabbing the renderer might even be unrelated to repaint), so the general rule is to _never_ change any state outside of the renderer. Fully agree with @AndrewThompson, just in different words: what exactly do you want to achieve? – kleopatra Mar 23 '12 at 08:54
  • I have some JLabels in the cells of my list and one of these labels is for username. If the label has the username John for example, then if any of the following cells have username labels with John as their value I just want to display a blank string for the username, Only when I get to a cell where the username is no longer John will I show the username again. Doesn't look like this is possible though if there is no order to the cell rendering. Hmmm, maybe I can do the check on the list model data instead... – csss Mar 23 '12 at 09:13
  • Altering the listmodel worked – csss Mar 23 '12 at 09:28
  • still don't quite understand what you are after, only show the first John in the list but not the following? (best to show an SSCCE), but never-ever should you have components in the model. Seeing your last comment: glad you found a solution :-) – kleopatra Mar 23 '12 at 09:33
  • @kleopatra: nah i dont have components in the model, Im just setting the the relevant usernames = "" as I add them to the model now on program initialization. Instead of having to use a static variable to keep track of lastusername and checking on every cell render, which doesnt work anyway for the reason you gave above. – csss Mar 23 '12 at 09:50
  • my misunderstanding, then :-) – kleopatra Mar 23 '12 at 09:56
  • @csss So if you ever implement ordering on the view side (or filtering) you are screwed since you modified your model and not your view ... – Robin Mar 23 '12 at 09:57
  • 2
    here's a question which might give you an idea of how-to reach your goal in the renderer (after all :) not by setting any variable, but by actually looping through the values http://stackoverflow.com/questions/7132400/jtable-row-hightlighter-based-on-value-from-tablecell – kleopatra Mar 23 '12 at 10:02
  • I vote to close. If you got a solution, you can publish it as answer, and accept it. If you're not interested, you should delete your post, if you don't want to improve it. – user unknown Mar 23 '12 at 10:08
  • @userunknown hmm ... what's your problem? The question is fine as is, no reason to close, with or without edits – kleopatra Mar 23 '12 at 11:18
  • @kleopatra: 2h ago, csss wrote: `Altering the listmodel worked`. So I didn't expect him to post the solution himself, but of course he is invited to do so. If not, and if he isn't giving more informations, I think the question isn't answerable - is it? – user unknown Mar 23 '12 at 11:36
  • 1
    Note that `lastUsername == user.getUsername()` is _not_ the same as `lastUsername.equals(user.getUsername())`. – trashgod Mar 23 '12 at 14:25

1 Answers1

1

Common mistake to compare String objects with == instead of equals. Try if (lastUsername.equals( user.getUsername() )) instead.

lzdt
  • 489
  • 1
  • 6
  • 17