Conversao de Tipos

9 respostas
H

Como posso fazer para pegar a segunda coluna desta array que é o tamanho do JTextField que desejo criar, conforme demonstrado abaixo ?

Object [][] aObjetos = {{ "Nome"     , "25" },
                                      { "Endereco", "40" },
                                      { "Fone"      ,  "8"   } };

J:JavaPrgGeraForm.java:62: illegal start of expression
txts[ i ] = new JTextField(()Integer)aObjetos[i][1] );
^

Obrigado

9 Respostas

_fs

Você declarou a variável ‘txts’?

H

Sim, eu declarei.

Vc, sabe como fazer isto ?

_fs

txts[ i ] = new JTextField(b[/b]Integer)aObjetos[i][1] );

Isto não me parece muito certo hehe

Mas de qualquer maneira, posta o código para eu entender melhor o que está acontecendo, pois, tirando aquela falha ali nos parenteses, tudo parece certo.

leonardom

Tenta assim:

txts[ i ] = new JTextField( Integer.parseInt(aObjetos[i][1].toString()) );
H

Lá vai o código:

import java.awt.<em>;

import javax.swing.</em>;

import java.awt.event.*;

public class GeraForm extends javax.swing.JFrame{

public JLabel lbls[];
	 public JTextField[] txts;


   public GeraForm(String aTituloJanela,
                   int largJanela,
                   int altJanela ) {

    super("Nome da janela Default");
    setTitle(aTituloJanela);
    setSize(largJanela, altJanela);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void GeraObjeto(Object [] [] aObjetos , int qtdObjetos, int tamTextField, 
                       String conteudoLabel) {

	 	 System.out.print(aObjetos[0][0]);
		 System.out.print(aObjetos[0][1]);
		 System.out.print(aObjetos[1][0]);
		 System.out.print(aObjetos[1][1]);
		 System.out.print(aObjetos[2][0]);
		 System.out.print(aObjetos[2][1]);


    GridBagLayout layoutTipoGrid = new GridBagLayout();
    GridBagConstraints restricoesLayout = new GridBagConstraints();
    JPanel painel1 = new JPanel();


	 		 Restricoes minhaRestricao = new Restricoes();

    painel1.setLayout(layoutTipoGrid);

    // Arrray de Textos
	 		 JLabel lbls[] = new JLabel[ qtdObjetos ];
	 		 JTextField txts[] = new JTextField[ qtdObjetos ];


	 		 for ( int i = 0; i &lt; txts.length; i++ ) {
	 
      //lbls[ i ] = new JLabel(String() aObjetos[i][0]);
      //lbls[ i ] = new JLabel("label__");
        lbls[ i ] = new JLabel();
        lbls[ i ].setText((String)aObjetos[i][0]);
    		 minhaRestricao.criaRestricoes(restricoesLayout, 0, i,1,1,100,100,2,17);
    		 layoutTipoGrid.setConstraints(lbls[i],restricoesLayout);
    }

// ===================================
for ( int i = 0; i < txts.length; i++ ) {

txts[ i ] = new JTextField((Integer)aObjetos[i][1] );
     // txts[ i ] = new JTextField(15);

	 		 		 minhaRestricao.criaRestricoes(restricoesLayout, 1,i,1,1,100,100,2,17);
    		 layoutTipoGrid.setConstraints(txts[i],restricoesLayout); }







	 		 for ( int i = 0; i &lt; txts.length; i++ ) {
	 		 		 painel1.add(txts[i]);
	 		 		 painel1.add(lbls[i]); }


    setContentPane(painel1);

    show();
}

public static void main(String[] arguments) {

    Object [][] aObjetos = {{ "Nome"    , "25" },
                            { "Endereco", "40" },
                            { "Fone"    ,  "8" } };
//        aObjetos[0] [0] = “Nome”;

//        aObjetos[0] [1] = “25”;

//        aObjetos[1] [0] = “Endereço”;

//        aObjetos[1] [1] = “40”;

//        aObjetos[2] [0] = “Fone”;

//        aObjetos[2] [1] = “8”;
System.out.print(aObjetos[0][0]);
		 System.out.print(aObjetos[0][1]);
		 System.out.print(aObjetos[1][0]);
		 System.out.print(aObjetos[1][1]);
		 System.out.print(aObjetos[2][0]);
		 System.out.print(aObjetos[2][1]);


    GeraForm tela = new GeraForm("minha tela",500, 175);
    tela.GeraObjeto(aObjetos, 3,15,"meu label");
}

}

_fs

Cara, o construtor JTextField( int ) não existe hehe e é exatamente isso que você tá fazendo.

*dica: quando for postar código, coloque ele dentro das tags [ code]codigo[ /code], assim ele fica endentado :smiley:

caiofilipini

JTextField(int) existe sim. O que não existe é JTextField(Integer). :wink:

H

Então pessoal, concluímos que o contrutor existe, certo?

Como posso então converter a String que está na array de Objetos para uma variável tipo Int que será passada para o construtor ?

Obrigado,

Hugo.

Rafael_Steil

“hugosoaresp1”:
Então pessoal, concluímos que o contrutor existe, certo?

Como posso então converter a String que está na array de Objetos para uma variável tipo Int que será passada para o construtor ?

Obrigado,

Hugo.

O 5o post deste topico ja responde a essa pergunta.

Rafael

Criado 3 de fevereiro de 2004
Ultima resposta 3 de fev. de 2004
Respostas 9
Participantes 5