Results 1 to 2 of 2
- 09-21-2012, 04:42 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 6
- Rep Power
- 0
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):
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).Java Code:WWTextField WWFormattedTextField
Code:
Indexed.java:
IndexedComparator.java:Java Code:public interface Indexed { public int getIndex(); }
WWTextField.java/WWFormattedtextField.java:Java Code:public class IndexedComparator implements Comparator<Indexed> { @Override public int compare(Indexed o1, Indexed o2) { return o1.getIndex() - o2.getIndex(); } }
NewJFrame.java: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; } }
IndexedFocusTraversalPolicy.javaJava 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); } }
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!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); } }
- 09-24-2012, 10:52 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
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?Please do not ask for code as refusal often offends.
Similar Threads
-
Sorting objects in a custom list
By Parashurama in forum New To JavaReplies: 17Last Post: 09-14-2012, 05:40 PM -
Collections sorting a list made of generic class objects
By andreiutz10 in forum New To JavaReplies: 5Last Post: 02-07-2012, 05:56 PM -
populating a list box based on another without refreshing the page
By zahir in forum JavaServer Pages (JSP) and JSTLReplies: 8Last Post: 07-25-2011, 01:09 PM -
Populating a gui list box with the files in a directory
By josejvelezcolon in forum New To JavaReplies: 1Last Post: 08-10-2009, 04:50 PM -
Populating a drop down list from a database
By matpj in forum New To JavaReplies: 0Last Post: 01-19-2009, 12:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks