Boa tarde, alguém poderia me ajudar.
Tenho que concluir um projeto de java, mas estou com problema para realizar a ação do jcombobox.
Queria saber se quando eu selecionar um item no combobox, tem como armazenar esse dado e após utiliza-lo na quando eu clicar em um botão, e o mesmo redirecionar para o item selecionado.
Este é o código um fiz:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Choice;
import javax.swing.JComboBox;
import java.awt.Dimension;
public class Cons extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel lblNome = null;
private JLabel lblCpf = null;
private JTextField txNome = null;
private JTextField txCpf = null;
private JButton btPesq = null;
private JButton btCad = null;
private JButton btSair = null;
private JComboBox cboTipo = null;
/**
* This is the default constructor
*/
public Cons() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(346, 250);
this.setContentPane(getJContentPane());
this.setTitle("Consulta");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
lblCpf = new JLabel();
lblCpf.setBounds(new Rectangle(13, 14, 58, 16));
lblCpf.setText("CPF/CNPJ");
lblNome = new JLabel();
lblNome.setBounds(new Rectangle(15, 74, 111, 17));
lblNome.setText("Nome/Razão Social");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(lblNome, null);
jContentPane.add(lblCpf, null);
jContentPane.add(getTxNome(), null);
jContentPane.add(getTxCpf(), null);
jContentPane.add(getBtPesq(), null);
jContentPane.add(getBtCad(), null);
jContentPane.add(getBtSair(), null);
jContentPane.add(getCboTipo(), null);
}
return jContentPane;
}
/**
* This method initializes txNome
*
* @return javax.swing.JTextField
*/
private JTextField getTxNome() {
if (txNome == null) {
txNome = new JTextField();
txNome.setBounds(new Rectangle(15, 103, 261, 19));
}
return txNome;
}
/**
* This method initializes txCpf
*
* @return javax.swing.JTextField
*/
private JTextField getTxCpf() {
if (txCpf == null) {
txCpf = new JTextField();
txCpf.setBounds(new Rectangle(15, 44, 139, 17));
}
return txCpf;
}
/**
* This method initializes btPesq
*
* @return javax.swing.JButton
*/
private JButton getBtPesq() {
if (btPesq == null) {
btPesq = new JButton();
btPesq.setBounds(new Rectangle(16, 150, 90, 17));
btPesq.setText("Consulta");
btPesq.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return btPesq;
}
/**
* This method initializes btCad
*
* @return javax.swing.JButton
*/
private JButton getBtCad() {
if (btCad == null) {
btCad = new JButton();
btCad.setBounds(new Rectangle(119, 149, 95, 18));
btCad.setText("Cadastrar");
btCad.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
new Clin().setVisible(true);
}
});
}
return btCad;
}
/**
* This method initializes btSair
*
* @return javax.swing.JButton
*/
private JButton getBtSair() {
if (btSair == null) {
btSair = new JButton();
btSair.setBounds(new Rectangle(225, 149, 90, 18));
btSair.setText("Sair");
btSair.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent e){
System.exit(0);
}
});
}
return btSair;
}
/**
* This method initializes cboTipo
*
* @return javax.swing.JComboBox
*/
private JComboBox getCboTipo() {
if (cboTipo == null) {
String opcoes[] = {" ","Pessoa Física", "Pessoa Jurídica"};
cboTipo = new JComboBox(opcoes);
cboTipo.setBounds(new Rectangle(200, 44, 100, 17));
cboTipo.setBackground(Color.white);
cboTipo.setVisible(true);
}
return cboTipo;
}
} // @jve:decl-index=0:visual-constraint=“10,10”