Link to home
Start Free TrialLog in
Avatar of rossryan
rossryan

asked on

JList -> Won't display my listmodel

Right, I am sure I am doing something wrong, but I can't find it.

All I am trying to do, at this point, is set the model to something I create. I can't even clear the standard model...

Any ideas?
-Ryan
public class SearchList extends JList{

     public SearchList() {
        super();
        this.setModel(new DefaultListModel());
        //this.repaint();
        //items.add("Test");
        //items.add("Another Test");
        //items.add("Yet another test.");
        
        //BuildPrototypeCells();
        //setCellRenderer(new PolyRenderer());
        //RefreshModel();
    }

     private void RefreshModel() {
        DefaultListModel newModel = new DefaultListModel();
        
        for(int i = 0; i < items.size(); i++) {
                newModel.addElement(new SearchItem((String)(items.get(i))));
        }

        this.setModel(newModel);
        this.validate();
        //this.repaint();
    }

     // I love ArrayLists. Type declarations are for the weak.
     private ArrayList items = new ArrayList();


     public void SetItems(ArrayList items) {
         this.items = items;
     }

     public ArrayList GetItems() {
         return items;
     }

    private ImageIcon defaultIcon = new ImageIcon ("defaultIcon.png");
    private JComponent resultCellPrototype;
    private JLabel fileNameLabel, textNameLabel, directoryNameLabel, imageNameLabel, fileSizeLabel, textSizeLabel, textWordCountLabel,directoryCountLabel,imageSizeLabel, imageIconLabel;

     

    private void BuildPrototypeCells() {
        resultCellPrototype = new JPanel();
        resultCellPrototype.setLayout (new GridBagLayout());
        AddWithGridBag(new JLabel(defaultIcon), resultCellPrototype, 0, 0, 1, 2, GridBagConstraints.WEST, GridBagConstraints.BOTH, 0, 0);
        fileNameLabel = new JLabel();
        Font defaultLabelFont = fileNameLabel.getFont();
        Font nameFont = defaultLabelFont.deriveFont(Font.BOLD,defaultLabelFont.getSize()+2);
        fileNameLabel.setFont(nameFont);
        AddWithGridBag(fileNameLabel, resultCellPrototype, 1, 0, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, 1, 0);
        fileSizeLabel = new JLabel();
        AddWithGridBag(fileSizeLabel, resultCellPrototype, 1, 1, 1, 1, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, 1, 0);
        Opacify(resultCellPrototype);

    }


     private void AddWithGridBag(Component comp, Container cont, int x, int y, int width, int height, int anchor, int fill, int weightx, int weighty) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.anchor = anchor;
        gbc.fill = fill;
        gbc.weightx = weightx;
        gbc.weighty = weighty;
        cont.add(comp, gbc);
    }

    private void Opacify(Container prototype) {
        Component[] comps = prototype.getComponents();
        for (int i=0; i<comps.length; i++) {
            if (comps[i] instanceof JComponent)
                ((JComponent)comps[i]).setOpaque(true);
        }
    }

    class SearchItem extends Object {
        String file;
        public SearchItem (String f) {
            file = f;
        }
    }



        class PolyRenderer extends Object implements ListCellRenderer {
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

                if(value instanceof SearchItem){
                    SearchItem item = (SearchItem)(value);
                    fileNameLabel.setText(item.file);
                    //fileSizeLabel.setText(item.file.length() + " bytes");
                    setColorsForSelectionState(resultCellPrototype, isSelected);
                    return resultCellPrototype;
                }
                else {
                    return resultCellPrototype;
                }
            }
            
            private void setColorsForSelectionState (Container prototype, boolean isSelected) {
                Component[] comps = prototype.getComponents();
                for (int i = 0;  i <comps.length; i++) {
                    if (isSelected) {
                        comps[i].setForeground(UIManager.getLookAndFeel().getDefaults().getColor("List.selectionForeground"));
                        comps[i].setBackground(UIManager.getLookAndFeel().getDefaults().getColor("List.selectionBackground"));
                    } else {
                        comps[i].setForeground(UIManager.getLookAndFeel().getDefaults().getColor("List.foreground"));
                        comps[i].setBackground(UIManager.getLookAndFeel().getDefaults().getColor("List.background"));
                    }
                }
            }
    }
}

Open in new window

Avatar of rossryan
rossryan

ASKER

I hate Java with a passion. Why must everything be like a trip across a old, rickety wooden bridge over a pit filled with lava, with Kwejfdwijefioj the Unthinkable blocking your way?

-Ryan
ASKER CERTIFIED SOLUTION
Avatar of cusl-info
cusl-info

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial