my cellRenderer doesn't work


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: my cellRenderer doesn't work

  1. #1
    Join Date
    Jun 2004
    Posts
    29

    my cellRenderer doesn't work

    Okay, this is going to be a rather lengthy question, but here goes...
    I'm trying to work with the cellRenderer from the JList class, and it doesn't appear to be working.

    First let me explain my application. I'm programming a realestate project customization application which stores the details of buildings that are under development. Currently, I'm working on the interface for customizing the units and levels for a particular building. I've got a JScrollPane and a JList to be viewed in the JScrollPane, and in the JList I'd like to render a vertically aligned list of CustomizeLevelPanel objects which I programmed to inherit from JPanel. These CustomizeLevelPanel objects are essentially interfaces for customizing the details of each level in the building (such as level number, types of units, quantity of each type, etc). They don't seemed to be rendering properly though.

    Here's some (not-quite-verbatim) code snippets of my work:

    ...
    CustomizeLevelPanel CLP[] = new CustomizeLevelPanel[number_of_levels];
    for (int i = 0; i < CLP.length; i++) {
    CLP[i] = new CustomizeLevelPanel(i);
    CLP[i].setBounds(0, 0, CLP_WIDTH, CLP_HEIGHT); // assuming JList will set the X and Y values automatically
    }
    JList L = new JList(CLP);
    L.setRenderer(new CustomizeLevelPanelRenderer());
    CustomizeLevelsScrollPane.getViewport().setView(L);
    ...

    public class CustomizeLevelPanelRenderer implements ListCellRenderer {

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    return (CustomizeLevelPanel)value;
    }

    };

    But when I run my program, nothing shows up in the JScrollPane. Actually, that's not true: a thin horizontal line spanning the width of the JScrollPane with the default background color of the JPanel shows up. This line seems to get thicker the more levels I add. So it makes sense to assume that the CustomizeLevelPanels are being added but rendered only as a horizontal lines.

    Does anyone see the problem?

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    I don't quite understand what you're trying to do with the ListCellRenderer. A JList typically aligns things properly by default, i'm not sure what you want it to do differently.

  3. #3
    Join Date
    Jun 2004
    Posts
    29
    Sorry,

    It's not the vertical alignment that's the problem. It's that the CustomizeLevelPanels are being rendered as horizontal lines instead of 2D panels. I have a full arrangement of JComboBoxes, JLabels, and a JButton all layed out inside the CustomizeLevelPanel, but none of it shows up.

  4. #4
    Join Date
    Mar 2004
    Posts
    635
    sounds like the panels have no vertical size. theres a method to set the minimum size of a panel i believe

  5. #5
    Join Date
    Oct 2004
    Posts
    3

    Exclamation I am not sure

    Hi ,



    I am not sure but I think that SetRenderer is applicable to JComboBox .For JList we can use setCellRenderer.So,you can goahead and look for the change and reply me.

    Maduraguy.

  6. #6
    Join Date
    Jun 2004
    Posts
    29
    Okay, that preferredSize() method worked to render them in a JList. Now I've got a new problem which I should have anticipated from the beginning:

    Using the CellRenderer works to display the CustomizeLevelsPanels but it seems as though they are only rendered as images. I don't seem to be able to click on any of their components like the comboboxes, spinners, and buttons. I'm assuming this is probably because the cellRenderer fulfills one function, and one function only: to RENDER them. Therefore, they only come through as pretty pictures, not real Swing components.

    Anyway, I've abandonned this approach and I'm persuing a different approach which I've been toying on the side all the while: I'm placing the CustomizeLevelPanels onto a parent panel I call CustomizeLevelsSuperPanel. This super panel is then added to the viewport of the JScrollPane. The problem with this approach, however, is that the JScrollPane seems not to realize when the viewport contents (i.e. the CustomizeLevelsSuperPanel) is too big for the viewport. Therefore, it doesn't accomodate by adjusting the attributes of the JScrollBars. I can set the scroll bar policies to AS_NEEDED but this seems to have no effect. I can set them to ALWAYS but those only seems to create scroll bars whose knobs take up the entire range (i.e. they have no room to move) and thus cannot be scrolled. I even tried setting the min, max, value, and extent fields in the scrollbar's BoundedRangeModel to no avail.

    There's got to be a way around this. How can I tell the JScrollPane how big the contents of the viewport is, and therefore how wide the scrolling range should be?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center