HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Component-oriented implementation of JList

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
orientedcomponentimplementationjlist

Problem

One of the main problems I have with JList is that no component is actually added to it. This means that things like ActionListeners don't work, and you can't really have a list of interactive components. To solve this, I've tried to quickly implement an alternative.

I have a number of concerns.

-
Speed: Will this be fast enough to display hundreds (thousands? millions?) of items?

-
Reliability: I've tried to make it reasonably solid, but no doubt there are a lot of things I should be checking that I am not.

-
Design: This is probably the most important. I tried to make a general, all-purpose design similar to that of JList, but it feels almost too complex to me. However, I cannot think of a better way. Any suggestions on how to improve it are most welcome.

It has three general parts:

The AdvancedList:

```
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.util.List;
import java.util.Vector;

import javax.swing.BoxLayout;

public class AdvancedList extends Container {
protected List model;
protected List> listCells;
protected AdvancedCellRenderer cellRenderer;

public AdvancedList(List model, AdvancedCellRenderer cellRenderer){
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

this.cellRenderer = cellRenderer;
this.model = new Vector();
this.listCells = new Vector>();

for(E element: model){
this.addElement(element);
}
}

public AdvancedList(AdvancedCellRenderer cellRenderer){
this(new Vector(), cellRenderer);
}

public void addElement(E element){
AdvancedListCell alc = cellRenderer.getAdvancedListCell(this, element, model.size());
this.add(alc.getComponent());
this.listCells.add(alc);
this.model.add(element);
}

public E removeElement(E element){
int idx = this.model.indexOf(element);
if(idx != -1) {
this.remove(idx);
this.l

Solution

This is a huge step to take to solve a problem which I am not sure actually exists.

Why do you need to reimplement the way the entire component is rendered just to add the ActionListeners in a different way? I think you have missed something here.

You can override the ListModel (AbstractListModel) and add all the hard effort in to that. As far as I can tell, none of your requirements need to extend beyond that. Your custom ListModel can register actionListeners with each added list member, and can broker the correct action respoinses if needed. Additionally, it can handle all the other events and eventhandling that needs to happen.....

I think all three of your concerns are valid, and you are right to be concerned...

  • Performance - I am not sure about this one, but I doubt your solution will be as well-refined as the native one.



  • Reliability - This solution is likely to be buggy, and is not going to be as upgradable in terms of look/feel as other components. This is a big deal.... the native components will be more responsive to that sort of context change.



  • Over complicated design: I think you are reinventing the wheel here. There is nothing in your design that I can't see in the native implementation.... I think the model/view differentiation is better in the native JList.



All in all, I think you are getting ahead of yourself, and have not fully understood how the native implementation can be adapted to solve the problems you think you have.

Context

StackExchange Code Review Q#43899, answer score: 3

Revisions (0)

No revisions yet.