Erro com estrutura de JTable

4 respostas
M

Beleza galera, tudo bom?
Tou com um problema com essa JTable, ela retorna uns erros estranhos, por mais que eu tente eu não consigo sanar eles. Esta tabela seria responsável por fazer uma busca em uma tabela do banco d dados, mas quando eu executo essa classe ela ñ faz simplesmente nada... :(

ela tá meio grandinha, mas mandei todo código relacionado a tal. Caso necessário o código da aplicação inteira, é só falar.

Eu comentei as msgs de erro que deram, pra fica fácil caso alguém já tenha tido algum(ns) desses problemas :)

desde já agradeço o apoio, sou mto noob em tabelas ainda :( :? :( :?

//#############################################
	private class ConsClass extends JFrame
	{
		private Connection con;
		private JTable tabela;
		ResultSetMetaData rsmd;
		
		private ConsClass(String strnome, String strnascimento, 
				String strtrabalho, String strrg, String strcpf, 
				String strendereco, String strbairro, 
				String strtelefon, String strcelular, String stremail, 
				String strfuncao, String stradmissão, String strobs) {
			super("Consulta");
			setSize(470,505);
			setLocationRelativeTo(null);
			try
			{
				con = gConexao.getConexao();
			}
			catch (Exception e)
			{
				JOptionPane.showMessageDialog(null,"Conexão não estabelecida","Mensagem do Programa",JOptionPane.ERROR_MESSAGE);
			}
		}
		
		private void buscaTabela(String strnome, String strnascimento, 
				String strtrabalho, String strrg, String strcpf, 
				String strendereco, String strbairro, 
				String strtelefon, String strcelular, String stremail, 
				String strfuncao, String stradmissão, String strobs) {
			Statement st;
			ResultSet res;
			
			try {
				Vector cabecalho = new Vector();
				Vector linhas = new Vector();
				st = con.createStatement();
				if(strnome.equals("") == false){
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE nome = '"+strnome+"'");
				}else{
					if(strnascimento.equals("") == false){
						res = st.executeQuery("SELECT * FROM Funcionarios WHERE nascimento= '"+strnascimento+"'");
					}else{
						if(strtrabalho.equals("") == false){
							res = st.executeQuery("SELECT * FROM Funcionarios WHERE cart_trabalho = '"+strtrabalho+"'");
						}else{
							if(strrg.equals("") == false){
								res = st.executeQuery("SELECT * FROM Funcionarios WHERE rg = '"+strrg+"'");
							}else {
								if(strcpf.equals("") == false){
									res = st.executeQuery("SELECT * FROM Funcionarios WHERE cpf = '"+strcpf+"'");
								}else {
									if(strendereco.equals("") == false){
										res = st.executeQuery("SELECT * FROM Funcionarios WHERE endereco = '"+strendereco+"'");
									}else {
										if(strbairro.equals("") == false){
											res = st.executeQuery("SELECT * FROM Funcionarios WHERE bairro = '"+strbairro+"'");
										}else {
											if(strtelefon.equals("") == false){
												res = st.executeQuery("SELECT * FROM Funcionarios WHERE fone = '"+strtelefon+"'");
											}else {
												if(strcelular.equals("") == false){
													res = st.executeQuery("SELECT * FROM Funcionarios WHERE cell = '"+strcelular+"'");
												}else {
													if(stremail.equals("") == false){
														res = st.executeQuery("SELECT * FROM Funcionarios WHERE email = '"+stremail+"'");
													}else {
														if(strfuncao.equals("") == false){
															res = st.executeQuery("SELECT * FROM Funcionarios WHERE funcao = '"+strfuncao+"'");
														}else {
															if(stradmissão.equals("") == false){
																res = st.executeQuery("SELECT * FROM Funcionarios WHERE admissao = '"+stradmissão+"'");
															}else {
																res = st.executeQuery("SELECT * FROM Funcionarios ");
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
				//-------caralho de if's!!!!!!
				
				res.next();
				ResultSetMetaData rsmd = res.getMetaData();
				for (int i = 1; i <= rsmd.getColumnCount(); ++i)
					cabecalho.addElement(rsmd.getColumnName(i));
				do
					{
					linhas.addElement(proximaLinha/*Erro=>The method proximaLinha(ResultSet, ResultSetMetaData) is undefined for the type CadFun.ConsClass*/(res,rsmd));
					}
				while (res.next());
				tabela = new JTable(linhas,cabecalho);
				JScrollPane scroller = new JScrollPane( tabela );
				getContentPane().add(scroller, BorderLayout.CENTER);
				tabelaMouseClicked/*Erro=>The method tabelaMouseClicked(null) is undefined for the type CadFun.ConsClass*/(null);
				TableColumn column = null;
			    column = tabela.getColumnModel().getColumn(0);
			        column.setPreferredWidth(50);
			    column = tabela.getColumnModel().getColumn(1);
			        column.setPreferredWidth(50);    
			    column = tabela.getColumnModel().getColumn(2);
			        column.setPreferredWidth(180);
				validate();
				st.close();
				}
			catch (SQLException sqlex) {
				}
			private Vector proximaLinha/*Illegal modifier for parameter proximaLinha; only final is permitted*/(ResultSet rs, ResultSetMetaData rsmd)
			{
				Vector LinhaAtual = new Vector();
				try
				{
					for (int i = 1; i <= rsmd.getColumnCount(); ++i)
						switch(rsmd.getColumnType(i))
						{
						case Types.VARCHAR: LinhaAtual.addElement(rs.getString(i));/*Multiple markers at this line
						- Types.VARCHAR cannot be resolved
						- Type safety: The method addElement(Object) belongs to the raw type Vector. References to generic type Vector<E> should be 
						 parameterized*/
							break;
						case Types.TIMESTAMP: LinhaAtual.addElement(rs.getDate(i));
							break;
						case Types.INTEGER: LinhaAtual.addElement(new Long(rs.getLong(i)));
							break;
						}
					}
				catch(SQLException e) {
					}
				return LinhaAtual;/*Void methods cannot return a value*/
				}
			private void tabelaMouseClicked(MouseEvent e)/*void is an invalid type for the variable tabelaMouseClicked*/ {
		    	int dadoCampo = 0;
		    	if (tabela.getSelectedRow() == -1){
		    		dadoCampo = 0;
		    	}
				else{
					dadoCampo = tabela.getSelectedRow();
				}
				String codigo = tabela.getValueAt(dadoCampo, 0).toString();
				nome 		= tabela.getValueAt(dadoCampo, 1).toString();
				obs 	= tabela.getValueAt(dadoCampo, 2).toString();
				dispose();

		        tabela.addMouseListener(new MouseAdapter() {
		        	public void mouseClicked(MouseEvent e) {
		        		tabelaMouseClicked(e);/*The method tabelaMouseClicked(MouseEvent) is undefined for the type new MouseAdapter(){}*/
		        	}
		        });
			}
				
				int linha;
				class CellListener extends MouseMotionAdapter{
				      public void mouseMoved(MouseEvent e){
				  	JTable tb = (JTable)e.getSource();
				        linha = tb.rowAtPoint(e.getPoint());/*Cannot refer to a non-final variable linha inside an inner class defined in a different method*/
				        tb.repaint();
				      }
				    }
				    
				    class ColorirCelula extends JLabel 
				         implements TableCellRenderer{
				      public ColorirCelula(){
				        setOpaque(true);
				      }
					      public Component getTableCellRendererComponent(
				                 JTable table, Object value,
				                 boolean isSelected, boolean hasFocus, 
				                 int row, int column){
				        if(row == linha ){
				          this.setBackground(Color.red);
				        }
				        else{
				          this.setBackground(table.getBackground());
				        }
				        
				        this.setText(value.toString());
				        return this;
				      }
				    }
		
		}//Fim Chave Busca Tabela
	}
//#############################################

4 Respostas

Marky.Vasconcelos

Cara seu código ta muito confuso.

Começe organizando um pouco para voce ver todos os métodos direito.

Voce nao conheçe a expressao “}else if(” é mais legivel que }else{ if(

M

Ok cara, vo fazer isso.

valeu a dica! \o

Marky.Vasconcelos

O que eu falei sobre os ifs é que assim fica mais facil de enchergar do que aquilo.

if(strnome.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE nome = '"+strnome+"'");
				else if(strnascimento.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE nascimento= '"+strnascimento+"'");
				else if(strtrabalho.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE cart_trabalho = '"+strtrabalho+"'");
				else if(strrg.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE rg = '"+strrg+"'");
				else if(strcpf.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE cpf = '"+strcpf+"'");
				else  if(strendereco.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE endereco = '"+strendereco+"'");
				else  if(strbairro.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE bairro = '"+strbairro+"'");
				else  if(strtelefon.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE fone = '"+strtelefon+"'");
				else if(strcelular.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE cell = '"+strcelular+"'");
				else if(stremail.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE email = '"+stremail+"'");
				else  if(strfuncao.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE funcao = '"+strfuncao+"'");
				else  if(stradmissão.equals("") == false)
					res = st.executeQuery("SELECT * FROM Funcionarios WHERE admissao = '"+stradmissão+"'");
				else 
					res = st.executeQuery("SELECT * FROM Funcionarios ");
				//-------caralho de if's!!!!!!

E outra… esta dando erro por que falta fechar a chave do método buscaTabela antes da declaração da função proximaLinha.

Então voce esta tentando criar o métood dentro do outro, por isso o erro.

E outra dica.
Use List invés de Vector.

M

realmente, eram as chaves fora de lugar, mas ainda dá pau no mouse clicked… será outras chaves? essas parecem tar corretas…

Criado 22 de junho de 2010
Ultima resposta 22 de jun. de 2010
Respostas 4
Participantes 2