Preencher jcombobox com um select do postgres

3 respostas
M

olá pessoal

como faço para preencher um combobox através de um select no postgres

o select tá pronto, falta so jogar o resultado no combo

tem que ser em tempo de execução

3 Respostas

robinsonbsilva

veja:

http://www.guj.com.br/posts/list/36743.java

yorgan

Amigo, seu select já está retornando o conteúdo que deve ser inserido?
Posta o código que ajuda.

[]´s

M

segue meu codigo

package teste;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Combo extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane1 = null;
	
	private JComboBox jComboBox1 = null;
	private JComboBox jComboBox2 = null;
	
	private static String estados[] = { "AC" , "AL" , "AP" , "AM" , "BA" , "CE" , "DF" , "ES" , "GO" , "MA" , "MT" , "MS" , "MG" , "PA" , "PB" , "PE" , "PI" , "PR" , "RJ" , "RN" , "RS" , "RO" , "RR" , "SC" , "SP" , "SE" , "TO" };
	
	public static void main(String[] args) {
		new Combo();
	}
	
	public Combo() {
		super();
		initialize();
	}

	private void initialize() {
		this.setSize(300, 370);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension windowSize = this.getSize();
		int x = (int) ((screenSize.getWidth() - windowSize.getWidth()) / 2);
		int y = (int) ((screenSize.getHeight() - windowSize.getHeight()) / 2);
		this.setLocation(x, y);
		this.setContentPane(getjContentPane1());
		this.setTitle("Combo");
		this.setLayout(null);
		this.setResizable(false);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().setBackground(Color.lightGray);
	}

	private JPanel getjContentPane1() {
		if (jContentPane1 == null) {

			jContentPane1 = new JPanel();
			jContentPane1.setVisible(true);
			jContentPane1.setLayout(null);

			jContentPane1.add(getJComboBox1());
			jContentPane1.add(getJComboBox2());
			
		}
		return jContentPane1;
	}

	private void atualiza(String pegar) {

		System.out.println(pegar);
		
		String codigo = null ;
		String fantas = null ;
		String cidade = null ;
		String juntar = null ;
		
		int conta = 0 ;
		
		jComboBox2.removeAllItems();
		
		try {
			ResultSet results = Conectar.execSQL( " select codigo, fantasia, cidade from escol132 where uf='"+pegar+"' and status = '01' order by cidade, fantasia ; " );
			while ( results.next() ) {
				codigo = results.getString(1);
				fantas = results.getString(2);
				cidade = results.getString(3);

				codigo = codigo.trim();
				fantas = fantas.trim();
				cidade = cidade.trim();
				
				juntar = "" ;
				juntar = fantas.concat(cidade);
				juntar = juntar.trim();
				
				conta++ ;
				
				jComboBox2.addItem( juntar );
				
				//System.out.println(codigo);
				
			}

		}catch( SQLException e1 ) {
			JOptionPane.showMessageDialog(null , "erro" + e1.getMessage() , "Operador" , JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
		
		System.out.println(conta);
		
		JOptionPane.showMessageDialog(null , "ok" , "Operador" , JOptionPane.ERROR_MESSAGE);

	}
	
	private JComboBox getJComboBox1() {
		if (jComboBox1 == null) {
			jComboBox1 = new JComboBox(estados);
			jComboBox1.setBounds(50,50,50,25);
			jComboBox1.setMaximumRowCount(10);
			jComboBox1.setVisible(true);
			jComboBox1.setEditable(false);
			jComboBox1.setToolTipText("Selecione o Estado");
			jComboBox1.addFocusListener(new FocusListener(){
				public void focusGained(FocusEvent arg0) {
					//ação desejada quando ganha o foco
				}
				
				public void focusLost(FocusEvent arg0){
					//ação desejada quando perde o foco
					//JOptionPane.showMessageDialog(null , "saiu" , "Operador" , JOptionPane.ERROR_MESSAGE);

					String pega = null ;
					pega = ( estados [jComboBox1.getSelectedIndex()]) ;
					//System.out.println(pega);
					
					//uf = ( coren.Validacao.estados[jComboBox1.getSelectedIndex()] );
					//uf = uf.substring(0,2);

					atualiza(pega);
				}
				
			});
			
		}
		return jComboBox1;
	}

	private JComboBox getJComboBox2() {
		if (jComboBox2 == null) {
			jComboBox2 = new JComboBox();
			jComboBox2.setBounds(50,100,250,25);
			jComboBox2.setMaximumRowCount(10);
			jComboBox2.setVisible(true);
			jComboBox2.setEditable(false);
			jComboBox2.setToolTipText("Selecione a UED");
		}
		return jComboBox2;
	}
}
Criado 17 de julho de 2008
Ultima resposta 17 de jul. de 2008
Respostas 3
Participantes 3