The Artima Developer Community
Sponsored Link

Java Answers Forum
my cellRenderer won't work

3 replies on 1 page. Most recent reply: Oct 26, 2004 3:05 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Gibran Shah

Posts: 27
Nickname: gibby
Registered: Jun, 2004

my cellRenderer won't work Posted: Oct 15, 2004 10:28 AM
Reply to this message Reply
Advertisement
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 = new CustomizeLevelPanel(i);
CLP.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?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: my cellRenderer won't work Posted: Oct 16, 2004 1:47 AM
Reply to this message Reply
My guess is that this thin lines are not the cell content, but the spacer between lines.
I'd say the line hight is 0.


Do you have ANYTHING in the panel? If it contains no objects, normally it does not request space for being rendered.

I think setBounds() makes only sense, if you're using a NullLayOut Form.

Try the setPreferredSize(Dimension preferredSize) and/or setMinimumSize(Dimension preferredSize).

Gibran Shah

Posts: 27
Nickname: gibby
Registered: Jun, 2004

Re: my cellRenderer won't work Posted: Oct 21, 2004 9:51 AM
Reply to this message Reply
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?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: my cellRenderer won't work Posted: Oct 26, 2004 3:05 AM
Reply to this message Reply
Set the size of CustomizeSuperPanel.

The JScrollpanel only asks it's first child for it's size.


You can't edit a JList, at least I don't know any method to do it.

Try using a JPanel.
You have to set the Cell Renderer and the Cell Editor.

Flat View: This topic has 3 replies on 1 page
Topic: Version Control Question: Branching, Labeling/Tagging Previous Topic   Next Topic Topic: loops?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use