Foros del Web » Programación para mayores de 30 ;) » Java »

hola ayuda!!! buscar , filtrar y listar

Estas en el tema de hola ayuda!!! buscar , filtrar y listar en el foro de Java en Foros del Web. hola buenas tardes para todo, ando necesitando ayuda con programacion JAVA lo siguiente.. necesito algun eljemplo o como hacer para buscar elementos en una lista ...
  #1 (permalink)  
Antiguo 24/07/2008, 12:08
 
Fecha de Ingreso: julio-2008
Mensajes: 8
Antigüedad: 15 años, 10 meses
Puntos: 0
Exclamación hola ayuda!!! buscar , filtrar y listar

hola buenas tardes para todo, ando necesitando ayuda con programacion JAVA lo siguiente..
necesito algun eljemplo o como hacer para buscar elementos en una lista y me vaya filtrando segun los datos ingresado en una lista de texto.
ej. en el cuadro de texto ingreso "an"
y en la lista (Listview) me aparecen todas las coincidencias ( ana, andres, andrea, anabel, antonio)
todos mis datos ya tengo cargados en un arrayList, pero no se como hacer para filtrarlos segun lo que ingrese..

muuuchas gracias por su ayuda!!

Última edición por loly20; 24/07/2008 a las 12:44
  #2 (permalink)  
Antiguo 24/07/2008, 16:34
 
Fecha de Ingreso: mayo-2007
Mensajes: 210
Antigüedad: 17 años
Puntos: 8
Respuesta: hola ayuda!!! buscar , filtrar y listar

te dejo un codigo que te puede ayudar, es un ejemplo completo lo corres y puedes mirar como funciona

Código PHP:
public class FilterHistoryJList extends JList {

    private 
FilterField filterField;
    private 
int DEFAULT_FIELD_WIDTH 20;

    public 
FilterHistoryJList() {
        
super();
        
setModel (new FilterModel());
        
filterField = new FilterField (DEFAULT_FIELD_WIDTH);
        
filterField.textField.requestFocus();
    }

    public 
void setModel (ListModel m) {
        if (! (
instanceof FilterModel))
            throw new 
IllegalArgumentException();
        
super.setModel (m);
    }

    public 
void addItem (Object o) {
        ((
FilterModel)getModel()).addElement (o);
    }

    public 
FilterField getFilterField() {
        return 
filterField;
    }

    
// test filter list
    
public static void main (String[] args) {
        
String[] listItems = {
            
"Chris""Joshua""Daniel""Michael",
            
"Don""Kimi""Kelly""Keagan"
        
};
        
JFrame frame = new JFrame ("FilterHistoryJList");
        
frame.getContentPane().setLayout (new BorderLayout());
        
// populate list
        
FilterHistoryJList list = new FilterHistoryJList();
        for (
int i=0i<listItems.lengthi++)
            list.
addItem (listItems[i]);
        
// add to gui
        
JScrollPane pane =
            new 
JScrollPane (list,
                             
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                             
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        
frame.getContentPane().add (paneBorderLayout.CENTER);
        
FilterField filterField = list.getFilterField();
        
frame.getContentPane().add (filterFieldBorderLayout.NORTH);
        
frame.pack();
        
frame.setVisible(true);
        
filterField.textField.requestFocus();
    }

    
// inner class to provide filtered model
    
class FilterModel extends AbstractListModel {
        
ArrayList items;
        
ArrayList filterItems;
        public 
FilterModel() {
            
super();
            
items = new ArrayList();
            
filterItems = new ArrayList();
        }
        public 
Object getElementAt (int index) {
            if (
index filterItems.size())
                return 
filterItems.get (index);
            else
                return 
null;
        }
        public 
int getSize() {
            return 
filterItems.size();
        }
        public 
void addElement (Object o) {
            
items.add (o);
            
refilter();
        }
        private 
void refilter() {
            
filterItems.clear();
            
String term getFilterField().textField.getText();
            for (
int i=0i<items.size(); i++)
                if (
items.get(i).toString().indexOf(term0) != -1)
                    
filterItems.add (items.get(i));
            
fireContentsChanged (this0getSize());
        }
    }

    
// inner class provides filter-by-keystroke field
    
class FilterField extends JComponent
        
implements DocumentListenerActionListener {
        
LinkedList prevSearches;
        
JTextField textField;
        
JButton prevSearchButton;
        
JPopupMenu prevSearchMenu;
        public 
FilterField (int width) {
            
super();
            
setLayout(new BorderLayout());
            
textField = new JTextField (width);
            
textField.getDocument().addDocumentListener (this);
            
textField.addActionListener (this);
            
prevSearchButton = new JButton (new ImageIcon ("mag-glass.png"));
            
prevSearchButton.setBorder(null);
            
prevSearchButton.addMouseListener (new MouseAdapter() {
                    public 
void mousePressed (MouseEvent me) {
                        
popMenu (me.getX(), me.getY());
                    }
                });
            
add (prevSearchButtonBorderLayout.WEST);
            
add (textFieldBorderLayout.CENTER);
            
prevSearches = new LinkedList ();
        }
        public 
void popMenu (int xint y) {
            
prevSearchMenu = new JPopupMenu();
            
Iterator it prevSearches.iterator();
            while (
it.hasNext())
                
prevSearchMenu.add (new PrevSearchAction(it.next().toString()));
            
prevSearchMenu.show (prevSearchButtonxy);
        }
        public 
void actionPerformed (ActionEvent e) {
            
// called on return/enter, adds term to prevSearches
            
if (e.getSource() == textField) {
                
prevSearches.addFirst (textField.getText());
                if (
prevSearches.size() > 10)
                    
prevSearches.removeLast();
            } 
        }
        public 
void changedUpdate (DocumentEvent e) {((FilterModel)getModel()).refilter(); }
        public 
void insertUpdate (DocumentEvent e) {((FilterModel)getModel()).refilter(); }
        public 
void removeUpdate (DocumentEvent e) {((FilterModel)getModel()).refilter(); }
    }
    
    class 
PrevSearchAction extends AbstractAction {
        
String term;
        public 
PrevSearchAction (String s) {
            
term s;
            
putValue (Action.NAMEterm);
        }
        public 
String toString() { return term; }
        public 
void actionPerformed (ActionEvent e) {
            
getFilterField().textField.setText (term);
            
// don't need this - setText fires a DocumentEvent
            // that FilterField handles
            // ((FilterModel)getModel()).refilter();
        
}
    }

salu2
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:14.