[JComboBox]Como setar o valor default? Duvida

7 respostas
A

bom galera to com uma dúvida e não estou conseguindo resolver
tenho um comboBox com as seguintes opções:
SELECT (Default)
Vestibular
PROUNI

bom ao fazer o cadastro clicando em ok eu seto os valroes do textField como uma Srting vaziatextFieldNome.setText("") para que se o usuário quiser realizar outro cadastro, mas não consigo fazer isso para um comboBox

Alguem pode me ajudar ae!!??
desde já Obrigado

7 Respostas

V

Use o método setSelectedItem.

A

mas o getSelectItem eu tenho q converter pra String certo?

esse setSelectItem é só colocar a opção do combobox como parametro?

A
da linha 13 a 25 é a lógica do cadastro.. da erro qdo aperto o botão ok e nenhum componente fica em branco e parece q não cadastra.. a linha 25 o método foi um teste não sei se está correto olha so o codigo:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JFormAluno extends JFrame {
	
	private Aluno al;
	public JFormAluno() {
		initComponents();
		al = new Aluno();
	}

	private void buttonOkActionPerformed(ActionEvent e) {
		al.setNome(textFieldNome.getText());
		al.setRg(textFieldRg.getText());
		al.setCategoria(textFieldCategoria.getText());
		al.setCurso(textFieldCurso.getText());
		al.setFormaIngresso(comboBoxFormaIngresso.getSelectedItem().toString());
		//if(!(al.getFormaIngresso().equals("SELECT"))){
			al.setCadastro(al);
			textFieldNome.setText("");
			textFieldRg.setText("");
			textFieldCategoria.setText("");
			textFieldCurso.setText("");
			comboBoxFormaIngresso.setSelectedIndex(1);
		//}
		//else{
		//	JOptionPane.showMessageDialog(null, "Forma de Ingresso não definida", "ERRO" ,JOptionPane.ERROR_MESSAGE);
		//}
		
		
	}

	private void buttonCancelarActionPerformed(ActionEvent e) {
		this.setVisible(false);
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Arthur Menezes
		labelNome = new JLabel();
		labelRG = new JLabel();
		labelCategoria = new JLabel();
		labelCurso = new JLabel();
		labelFormaIngresso = new JLabel();
		comboBoxFormaIngresso = new JComboBox();
		textFieldCurso = new JTextField();
		textFieldCategoria = new JTextField();
		textFieldRg = new JTextField();
		textFieldNome = new JTextField();
		buttonOk = new JButton();
		buttonCancelar = new JButton();

		//======== this ========
		setTitle("Cadastrar Aluno");
		setIconImage(((ImageIcon)UIManager.getIcon("FileView.directoryIcon")).getImage());
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- labelNome ----
		labelNome.setText("Nome:");
		labelNome.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
		contentPane.add(labelNome);
		labelNome.setBounds(new Rectangle(new Point(20, 20), labelNome.getPreferredSize()));

		//---- labelRG ----
		labelRG.setText("RG:");
		labelRG.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
		contentPane.add(labelRG);
		labelRG.setBounds(new Rectangle(new Point(20, 65), labelRG.getPreferredSize()));

		//---- labelCategoria ----
		labelCategoria.setText("Categoria:");
		labelCategoria.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
		contentPane.add(labelCategoria);
		labelCategoria.setBounds(new Rectangle(new Point(20, 110), labelCategoria.getPreferredSize()));

		//---- labelCurso ----
		labelCurso.setText("Curso:");
		labelCurso.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
		contentPane.add(labelCurso);
		labelCurso.setBounds(new Rectangle(new Point(20, 155), labelCurso.getPreferredSize()));

		//---- labelFormaIngresso ----
		labelFormaIngresso.setText("Forma de Ingresso: ");
		labelFormaIngresso.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
		contentPane.add(labelFormaIngresso);
		labelFormaIngresso.setBounds(20, 200, 115, labelFormaIngresso.getPreferredSize().height);

		//---- comboBoxFormaIngresso ----
		comboBoxFormaIngresso.setMaximumRowCount(3);
		comboBoxFormaIngresso.setModel(new DefaultComboBoxModel(new String[] {
			"Select",
			"Vestibular",
			"PROUNI"
		}));
		comboBoxFormaIngresso.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		contentPane.add(comboBoxFormaIngresso);
		comboBoxFormaIngresso.setBounds(20, 215, 140, 20);
		contentPane.add(textFieldCurso);
		textFieldCurso.setBounds(20, 170, 275, 20);
		contentPane.add(textFieldCategoria);
		textFieldCategoria.setBounds(20, 125, 275, 20);
		contentPane.add(textFieldRg);
		textFieldRg.setBounds(20, 80, 275, 20);
		contentPane.add(textFieldNome);
		textFieldNome.setBounds(20, 35, 275, 20);

		//---- buttonOk ----
		buttonOk.setText("OK");
		buttonOk.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
		buttonOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				buttonOkActionPerformed(e);
			}
		});
		contentPane.add(buttonOk);
		buttonOk.setBounds(new Rectangle(new Point(195, 255), buttonOk.getPreferredSize()));

		//---- buttonCancelar ----
		buttonCancelar.setText("Cancelar");
		buttonCancelar.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
		buttonCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				buttonCancelarActionPerformed(e);
				buttonCancelarActionPerformed(e);
			}
		});
		contentPane.add(buttonCancelar);
		buttonCancelar.setBounds(new Rectangle(new Point(280, 255), buttonCancelar.getPreferredSize()));

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < contentPane.getComponentCount(); i++) {
				Rectangle bounds = contentPane.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = contentPane.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			contentPane.setMinimumSize(preferredSize);
			contentPane.setPreferredSize(preferredSize);
		}
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Arthur Menezes
	private JLabel labelNome;
	private JLabel labelRG;
	private JLabel labelCategoria;
	private JLabel labelCurso;
	private JLabel labelFormaIngresso;
	private JComboBox comboBoxFormaIngresso;
	private JTextField textFieldCurso;
	private JTextField textFieldCategoria;
	private JTextField textFieldRg;
	private JTextField textFieldNome;
	private JButton buttonOk;
	private JButton buttonCancelar;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
L

Assim ele vai pro primeiro elemento do JComboBox.

A

Obrigado deu certo aki

A

E para eu recuperar o valor de um ComboBox já cadastrado???

G

Este tópico é bem antigo, mas me ajudou muito.

Criado 23 de novembro de 2008
Ultima resposta 19 de ago. de 2014
Respostas 7
Participantes 4