Pegar o primeiro caracter de um objeto  XML
Índice dos Fóruns » Java Básico
Autor Mensagem
mary_pops
Smalltalk

Membro desde: 22/11/2006 19:43:11
Mensagens: 1
Offline

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!
Deh
JavaGuru
[Avatar]

Membro desde: 18/04/2006 23:02:24
Mensagens: 244
Localização: Florianópolis - SC
Offline

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


Lnunes
Smalltalk
[Avatar]

Membro desde: 21/11/2006 13:33:41
Mensagens: 4
Localização: Rio de Janeiro
Offline

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

[]'s
Leonardo Nunes
[Email]
loganwlogan
JavaTeenager
[Avatar]

Membro desde: 31/08/2004 00:17:26
Mensagens: 182
Offline

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

"Quando você olhar dentro do abismo
O Abismo olhará dentro de você"
Guerras Infinitas
[WWW] [Yahoo!] [MSN]
 
Índice dos Fóruns » Java Básico
Ir para:   
Powered by JForum 2.1.8 © JForum Team