Pesquisando se existe na Combo

0 respostas
Lindberg

Ola pessoal

Peguei um exemplo aqui no GUJ e modifiquei para pesquisar
na Combo.
Retorna um numero 1 = quando encontra
Retorna um numero 0 = quando NÃO encontra

Criei duas classes. Sendo uma static.

Basta compilar ComboTeste.java

ta funcionando beleza …
pesquisa uma string "teste 2".

se alguem tiver algum comentario, sera bem aceito pessoal …

//////////////////////////////////////////////////////////////////////////////////////
A primeira: ComboTeste.java

import java.awt.<em>;

import java.awt.event.</em>;

import javax.swing.*;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

public class ComboTeste extends JFrame {

int value = 3;

 DefaultComboBoxModel model = new DefaultComboBoxModel(getArray());
 JComboBox combo = new JComboBox(this.model);
 JButton button = new JButton("Adicionar");
 JButton button2 = new JButton("Mostrar");
 
 
 public ComboTeste() {
     super("Teste Lindberg");
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     this.getContentPane().setLayout(new FlowLayout());
     this.getContentPane().add(this.combo);
     this.getContentPane().add(this.button);
     this.getContentPane().add(this.button2);

     this.button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent arg0) {
             model.removeAllElements();
             Object[] array = getArray();
             for (int i=0; i &lt; array.length; i++) {
                 model.addElement(array[i]);
             }
         }
  
     });


     this.button2.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent arg0) {


            Object[] array = getArray();
// procura no array se tem "teste 2"

int resposta = bergutil.TestaCombo("teste 2", array);

JOptionPane.showMessageDialog(null,

"1=Encontrou, 0=Nao Eoncontrou\nRetornou "+ resposta ,

"Procurando !", JOptionPane.INFORMATION_MESSAGE);
// calcular -  multiplica n1 * n2

resposta = bergutil.multiplica(20, 10);

JOptionPane.showMessageDialog(null,

"Resultado "+ resposta ,

"Procurando !", JOptionPane.INFORMATION_MESSAGE);
}
  
     });


     
     pack();
     setVisible(true);
 }




 
 public static void main(String[] args) {
     new ComboTeste();
 }
 
 public Object[] getArray() {
     Object[] array = new Object[this.value++];
     for (int i=0; i &lt; array.length; i++) {
         array[i] = "teste " + (i + 1);
     }
     
     return array;
 }

}


A Segunda: bergutil.java

import java.util.;
import java.text.
;

import java.io.;
import javax.swing.
;

import java.util.Date;
import java.text.SimpleDateFormat;

public class bergutil {

public static int multiplica(int n1, int n2)  {
	return (n1 * n2);
}


 	public static int TestaCombo(String aa, Object[] arr) {

   int tt = 0;

       for (int i=0; i &lt; arr.length; i++) {

	JOptionPane.showMessageDialog(null, 
	"Olha "+arr[i], "Procurando !", JOptionPane.INFORMATION_MESSAGE);

       Object pesquisa = arr[i];
	
       if (pesquisa.equals( aa ))  tt = 1;


   }	

   return tt;	
 	}

}

////////////////////////////////////////////////////////////////////////////////////

Espero que ajude !!

Criado 22 de novembro de 2007
Respostas 0
Participantes 1