Pegar o primeiro caracter de um objeto

3 respostas
M

Olas,

Tenho um jcomboBox alimento ele com:
1- Primeiro Grau
2- Segundo Grau
.

na hora de gravar no banco queria pegar somente o numero (1 por exemplo) estou usando o getSelectedItem que retorna um objeto com a opção selecionda…Como faço p pegar somente o primeiro caracter???

Obg!

3 Respostas

Deh

e se você usar o getSelectedIndex() ?
vai retornar o valor em inteiro do item selecionando começando a contar de zero, e retorna -1 se não houver item selecionado… =]

getSelectedIndex

public int getSelectedIndex()

Returns the first item in the list that matches the given item. The result is not always defined if the JComboBox allows selected items that are not in the list. Returns -1 if there is no selected item or if the user specified an item which is not in the list.

Returns:
    an integer specifying the currently selected list item, where 0 specifies the first item in the list; or -1 if no item is selected or if the currently selected item is not in the list</blockquote>
Lnunes

Simples…

String strTeste = (String) suaCombo.getSelectedItem( );

String strPronto = strTeste.substring( 0, 1);

Dá um olhada na API das classes… vc mata fácil essas dúvidas.

http://java.sun.com/j2se/1.5.0/docs/api/index.html

loganwlogan

Amigo existiriam “n” maneiras de fazer isso mas como vc quer pegar um valor inteiro nada mais lógico de vc utilizar o getSelectedIndex que já retorna um inteiro vc só teria que fazer um pequeno ajuste pq começa contar de zero

Ex:
int valor = jcbMeuCombo.getSeletedIndex() + 1;

Sds

Logan

Criado 22 de novembro de 2006
Ultima resposta 23 de nov. de 2006
Respostas 3
Participantes 4