Results 1 to 2 of 2
  1. #1
    Phenomena is offline Member
    Join Date
    Apr 2010
    Posts
    6
    Rep Power
    0

    Default 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):
    Java 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:
    Java Code:
    public interface Indexed {
        
        public int getIndex();
    }
    IndexedComparator.java:
    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:
    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:
    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
    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!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. Sorting objects in a custom list
    By Parashurama in forum New To Java
    Replies: 17
    Last Post: 09-14-2012, 05:40 PM
  2. Collections sorting a list made of generic class objects
    By andreiutz10 in forum New To Java
    Replies: 5
    Last Post: 02-07-2012, 05:56 PM
  3. populating a list box based on another without refreshing the page
    By zahir in forum JavaServer Pages (JSP) and JSTL
    Replies: 8
    Last Post: 07-25-2011, 01:09 PM
  4. Populating a gui list box with the files in a directory
    By josejvelezcolon in forum New To Java
    Replies: 1
    Last Post: 08-10-2009, 04:50 PM
  5. Populating a drop down list from a database
    By matpj in forum New To Java
    Replies: 0
    Last Post: 01-19-2009, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •