ArrayList - Comparação

3 respostas
M

Bom dia pessoal estou tentanto comparar se um cliente já está ou não online e gostaria de saber oque está errado no meu código pois o mesmo não passa pelo (IF) para realizar a comparação..

segue o Código.

public class TestJTable_CLient extends JFrame {

	private static final long serialVersionUID = 1L;
	private JTable table;
	private DefaultTableModel model;
	private ArrayList<String> cliente = new ArrayList<String>();

	public TestJTable_CLient() {

		setTitle("Clientes");
		setSize(400, 400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		table = new JTable();
		model = new DefaultTableModel();
		table.setModel(model);
		table.getTableHeader().setReorderingAllowed(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		add(new JScrollPane(table));

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// chama metodo que carrega a tabela aqui!

		carregaJTable();

		setVisible(true);

		// não deixa os campos da tabela serem editados.
		for (int i = 0; i < table.getColumnCount(); i++) {

			TableColumn column = table.getColumnModel().getColumn(i);

			column.setCellEditor(new TableCellEditor() {

				//....Outros Métodos

				@Override
				public boolean isCellEditable(EventObject arg0) { // editable false
					// TODO Auto-generated method stub
					return false; // editable false
                                }

				
			});
		}

		// Adiciona login no Array
		cliente.add("13591450"); // Tenho que ter alguma coisa dentro do arrayList para ele comparar senão não funciona !

		// evento de mouse para duplo clique cria tela Cliente.
		table.addMouseListener(new MouseListener() {

//.....Outros Métodos			

			@Override
			public void mouseClicked(MouseEvent e) {


				if (e.getClickCount() == 2) {

					try {

						String nome = (String) table.getValueAt(table.getSelectedRow(), 0); // Pega valores da linha

						String login = (String) table.getValueAt(table.getSelectedRow(), 1);  // pega valores da linha

						
						System.out.println(cliente.size());  // tamanho do arrayList

						Iterator<String> it = cliente.iterator(); // cria "lista" de cliente

						while (it.hasNext()) { // ENTRA AQUI !!  e percorre a lista 

							String clientes = it.next();

							System.out.println("Passa Pelo WHILE !");

				/* PROBLEMA >>*/  if (clientes.equals(login)) { // NÃO ENTRA AQUI ! ELE DEIXA ABRIR MAIS DE 1 JANELA PARA O MESMO CLIENTE !

								System.out.println("Passa Pelo IF!");
								JOptionPane.showMessageDialog(null,"Só é possivel se conectar com esse cliente uma vez!",
												"Atenção",JOptionPane.WARNING_MESSAGE);

							} else {
								// cria tela Client
								new ClientScreen().setVisible(true);	
							
								cliente.add(login); // adiciona login no arrayList cliente e se caso o usuario tente abrir mais uma tela lança exeption

								System.out.println(cliente.size()); // mostra tamanho depois de adicionado o login.
								System.out.println(login); // imprimi o login que foi adicionado 

							}
							break;

						}
					} catch (IOException erro) {
						erro.printStackTrace();
					}

				}

			}
		});

	}

	

}

3 Respostas

jaboot

matheus.nani,

Não tenho muito a lhe orientar senão debugar, cara. Printa no console o que está vindo de clientes, e o que está vindo de login.
O que eu sugiro é você dar um trim, limpar essas Strings e verificar maiúsculas e minúsculas.

Boa sorte!

R

clientes.equals(login)

Veja o conteudo de clientes e login… estão escrito iguais!?
Pois…
“rafael”.equals(“Rafael”) = false
“rafael”.equals("rafael ") = false
“rafael”.equals(“RAFAEL”) = false

M

Já resolvi aqui galera JAJA eu posto o Codígo ! VALEU ! :stuck_out_tongue:

Criado 12 de abril de 2012
Ultima resposta 12 de abr. de 2012
Respostas 3
Participantes 3