galla.info
galla.info

Reputation: 33

Why my JTextField not being filled

I would like to know why JTextField are not being filled, and that put the System.out.println test are completed correctly? Could someone could help me clarify this doubt? My method.:

protected void ApresentaGrupos(List<GrupoBean> grupos) {  
String codigo = "";  
        String grupo = "";  
        for (GrupoBean g : grupos) {  
            codigo = g.getCodigo().toString().trim();  
            grupo = g.getGrupo();  

            System.out.println("" + g.getCodigo().toString().trim()); // TEST
            System.out.println("" + g.getGrupo().toUpperCase()); // TEST
        }  
        this.habilitaCampos();  
        txtCodigo.setText("TEST"); // nor that message is being shown  
        txtGrupo.setText(grupo);  
        System.out.println("teste" + codigo);  
        System.out.println("teste" + grupo);
}

Upvotes: 0

Views: 118

Answers (2)

Vishal K
Vishal K

Reputation: 13066

It might be Possible that after adding txtCodigo to the container, you reconstructed the txtCodigo and now putting texts in that newly constructed JTextArea. So the value that you are putting in txtCodigo is not reflecting in the actual JTextArea added in the container. For Example Consider the Code Give below:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JTextField;
public class  DoubleConstruction extends JFrame
{
    JTextField field = new JTextField(20);
    public DoubleConstruction()
    {
        super("JTextArea Demo");
    }
    public void prepareAndShowGUI()
    {
        getContentPane().add(field);
        field.setText("Java");
        field = new JTextField(20);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    public void setText()
    {
        field.setText("J2EE");
    }
    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                DoubleConstruction dc = new DoubleConstruction();
                dc.prepareAndShowGUI();
                dc.setText();
            }
        });
    }
}

In method prepareAndShowGUI() after adding field to the ContentPane , field is reconstructed. And within main method although I have changed the content of field to "J2EE" but the JTextField shown in JFrame is still showing "Java". These are the silly mistakes but chances of occurrence of such mistakes are not null..

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

The problem is not identifiable in the code you've posted, and so we can only guess. Perhaps your txtCodigo and txtGrupo JTextFields are not the same as the ones that are being displayed. This can happen in a variety of situations, often if there is misuse of inheritance. Does this class inherit from another class of yours by chance?

Also this doesn't make sense:

    for (GrupoBean g : grupos) {  
        codigo = g.getCodigo().toString().trim();  
        grupo = g.getGrupo();  
    }  
    txtGrupo.setText(grupo);  

Since it appears that only the last GrupoBean in the grupos collection will have a chance of being displayed in your GUI.

As per mKorbel: For better help though, consider creating and posting an SSCCE.

Upvotes: 1

Related Questions