Passar o item de um combo como parÂmetro

5 respostas
M

Bom dia galera, sou novo aqui e no mundo java. Estou querendo saber como faço para passar um item de um combobox como parâmetro…
exemplo…
Eu tenho uma classe Cliente com um atributo nome… e criei uma classe conta que tem um atributo do tipo Cliente para eu poder selecionar o nome.
Na construção do form agora eu conseguir carregar os nomes na combobox, porem tenho que passar como parametro o tipo Cliente que sera selecionado no click… se alguem intendeu e quiser me dá uma moral, valeu…

5 Respostas

M

ComboBox… JComboBox, ou combobox da web? =p

RDSILVA

Fala ai MIQUEIASSOUSA
Brow não sei se é exatamente isso que você esta querendo…fiz um exemplo rapidão…
da uma olhada ai e qq coisa me fala, é so salvar a classe e rodar…
Ai com o valor selecionado em mãos você consegue passar como parametro.
abraço

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import java.awt.Rectangle;
import javax.swing.JButton;

public class TesteCombo extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JComboBox jComboBox = null;

	private JButton jButton = null;

	/**
	 * This is the default constructor
	 */
	public TesteCombo() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJComboBox(), null);
			jContentPane.add(getJButton(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setBounds(new Rectangle(33, 57, 230, 30));
			toFillJComboBox();
		}
		return jComboBox;
	}
	
	private void toFillJComboBox(){
		
		getJComboBox().addItem("Teste 1");
		getJComboBox().addItem("Teste 2");
		getJComboBox().addItem("Teste 3");
		getJComboBox().addItem("Teste 4");
		getJComboBox().addItem("Teste 5");
		getJComboBox().addItem("Teste 6");
		
	}
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(94, 116, 96, 35));
			jButton.setText("Item");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					JOptionPane.showMessageDialog(null, getJComboBox().getSelectedItem());
					
				}
			});
		}
		return jButton;
	}

	public static void main(String args[]){
		
		TesteCombo testeCombo = new TesteCombo();
		testeCombo.setVisible(true);
		testeCombo.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

}
X

cria um evento no combobox chamado itemStateChanged para que cada modificacao ele execute-o.
Nesse evento tu pega o item selecionado usando o metodo getSelecteditem() que retorna um Object:
Ex.: jComboBox1.getSelectedItem().toString()

flws

M

valeu galera…

D

xxta:
cria um evento no combobox chamado itemStateChanged para que cada modificacao ele execute-o.
Nesse evento tu pega o item selecionado usando o metodo getSelecteditem() que retorna um Object:
Ex.: jComboBox1.getSelectedItem().toString()

flws

Em meus exemplos aqui, não precisa do toString não.
Ao contrário, não estava passando os itens por causa dele.

Criado 19 de junho de 2008
Ultima resposta 28 de ago. de 2008
Respostas 5
Participantes 5