Populating and Sorting a List<> of any number of different objects
What I'm trying to do is take all my custom swing components and placing them into a List and then sorting them based on a property that will be common between them all.
For example, I have two components (so far):
Code:
WWTextField
WWFormattedTextField
Both of those classes have a property called tabIndex. I want to be able to place all those different objects into a List, sort them by tabIndex and then using the list to a create a FocusTraversalPolicy. My original implementation was to have all my components implement Indexed which has a single method called getIndex(). From there I made a custom comparator called IndexedComparator which implements Comparator<Indexed> and finally declared my List as List<? extends Component & Indexed).
Code:
Indexed.java:
Code:
public interface Indexed {
public int getIndex();
}
IndexedComparator.java:
Code:
public class IndexedComparator implements Comparator<Indexed> {
@Override
public int compare(Indexed o1, Indexed o2) {
return o1.getIndex() - o2.getIndex();
}
}
WWTextField.java/WWFormattedtextField.java:
Code:
public class WWTextField extends JTextField implements Indexed, FocusListener {
private final Border BORDER_ERROR = BorderFactory.createLineBorder(new Color(255, 0, 0));
private final Border BORDER_ALL_GOOD = BorderFactory.createLineBorder(new Color(0, 0, 0));
private boolean required;
private int tabIndex;
public WWTextField(){
super();
setBorder(BORDER_ALL_GOOD);
addFocusListener(this);
}
public boolean isRequired() {
return required;
}
public void setRequired(boolean required) {
this.required = required;
}
public int getTabIndex() {
return tabIndex;
}
public void setTabIndex(int tabIndex) {
this.tabIndex = tabIndex;
}
public void error(boolean b) {
if(b) {
setBorder(BORDER_ERROR);
} else {
setBorder(BORDER_ALL_GOOD);
}
}
@Override
public void focusGained(FocusEvent e) {}
@Override
public void focusLost(FocusEvent e) {
if (!this.getText().isEmpty() && required) {
error(false);
}
}
@Override
public int getIndex() {
return tabIndex;
}
}
NewJFrame.java:
Code:
public class NewJFrame extends JFrame {
ArrayList<WWTextField> list = new ArrayList<>();
IndexedFocusTraversalPolicy policy = new IndexedFocusTraversalPolicy();
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
list.add(wWTextField1);
list.add(wWTextField2);
list.add(wWTextField3);
list.add(wWTextField4);
list.add(wWTextField5);
list.add(wWFormatedTextField1);
list.add(wWFormatedTextField2);
policy.populateComponents(list);
this.setFocusTraversalPolicy(policy);
}
}
IndexedFocusTraversalPolicy.java
Code:
ublic class IndexedFocusTraversalPolicy extends FocusTraversalPolicy {
private ArrayList<Component> components = new ArrayList<>();
public void addIndexedComponent(Component component) {
components.add(component);
}
public void addIndexedComponentAt(int index, Component component) {
components.add(index, component);
}
public void populateComponents(ArrayList components) {
Collections.sort(components);
this.components = components;
}
@Override
public Component getComponentAfter(Container aContainer,
Component aComponent) {
int atIndex = components.indexOf(aComponent);
int nextIndex = (atIndex + 1) % components.size();
return components.get(nextIndex);
}
@Override
public Component getComponentBefore(Container aContainer,
Component aComponent) {
int atIndex = components.indexOf(aComponent);
int nextIndex = (atIndex + components.size() - 1) %
components.size();
return components.get(nextIndex);
}
@Override
public Component getFirstComponent(Container aContainer) {
return components.get(0);
}
@Override
public Component getLastComponent(Container aContainer) {
return components.get(components.size()-1);
}
@Override
public Component getDefaultComponent(Container aContainer) {
return components.get(0);
}
}
My problem is that List<? extends Component & Indexed> is causing a compile time error, saying that it's an invalid argument. I'm not an expert on generics and I'm not sure if this is even the right way to go about this so any help would be appreciated! Thanks!
Re: Populating and Sorting a List<> of any number of different objects
WHat is the full compilation error and what line does it occur on (that's a lot of code to go through to find an error)?
If your things all implement Indexed then make the list a List<Indexed>, surely?