Hey guys I'm a bit confused here as I haven't edited a renderer before.

My situation is that I have a custom object that I have created. It has a few jLabels, textboxes, and a combo box. And what I'm wanting to do it make it display on a jList. For now we can call the object ViewObject.

Can someone explain or show me how I would go about doing this?

I have checked the Java Docs, but they don't quite seem to make a whole lot of sense to me.

Recommended Answers

All 7 Replies

Thanks for the advice BestJewSinceJC, sorry for such a late response. I have just now gotten back to the project that I'm working on with this jList renderer.

I haven't messed with making a custom render before so I'm a bit clueless on how to get it working the way I want it to. The basic structure seems to be like so:

class MyCellRenderer extends DatabaseObject implements ListCellRenderer {
        final static ViewObject vo = new ViewObject();     
        
        public Component getListCellRendererComponent (JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            
            //set ViewObject
            
                if (isSelected) {
                    setBackground(list.getSelectionBackground());
                    setForeground(list.getSelectionForeground());
                } else {
                    setBackground(list.getBackgroud());
                    setForeground(list.getForeground());
                }      
                setEnabled(list.isEnabled());
                setFont(list.getFont());
                setOpaque(true);
                return this;
            }
        }
    }
    //set cell renderer

I'm not quite sure how I should be setting the viewobject exactly though. What's going to happen is when I click a button I'm wanting information to go into the viewobject which would then go onto this custom renderer.

If anyone is able to give me a hint on how to continue setting this up, I'd be grateful.

In the example I was viewing, it had the following code:

String s = value.toString();
         setText(s);
         setIcon((s.length() > 10) ? longIcon : shortIcon);

However, I'm not aware of there being a setObject which is where I remain confused a bit.

I can't figure why you would want to render it in a list. If you want to display a series of panel entries they you would be better off putting them in a vertical box layout in a scroll pane.

I can't figure why you would want to render it in a list. If you want to display a series of panel entries they you would be better off putting them in a vertical box layout in a scroll pane.

What I had originally done was put them in a jPanel inside a scroll pane. However, I realized I also had to be able to select the object in case I wanted to delete from the panel and arraylist or vector that the entry would be stored in.

I'm not familiar with a vertical box layout, I would assume I wouldn't be able to select the object though in case I need to remove the entry

"Selection" is nothing more than a boolean. You can represent that however you choose.

The box layout just stacks the components vertically. You could use grid or grid bag as well. The point is that each entry is a JPanel component.

"Selection" is nothing more than a boolean. You can represent that however you choose.

The box layout just stacks the components vertically. You could use grid or grid bag as well. The point is that each entry is a JPanel component.

So the way I have it right now (through gridBagLayout) it automatically stacks them to the top would technically be sufficient if I add a boolean for isSelected?

I also have to remove whatever information is in the viewObject from a arraylist or vector depending on whether it is selected in this jPanel.

So the way I have it right now (through gridBagLayout) it automatically stacks them to the top would technically be sufficient if I add a boolean for isSelected?

Sure, which ever you want to work with.

I also have to remove whatever information is in the viewObject from a arraylist or vector depending on whether it is selected in this jPanel.

As long as you provide a method to determine if it's selected, you can process that however you like, i.e. a button action that removes all selected entries, etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.