'jTextField.setText() does not work in the Jframe
I created a simple jFrame with a jDialog, in this jDialog I have textfield just to get an ID, them, with this ID, use a webservice to search for the information and them show the information in the jFrame, however, all textfields that I use on my jFrame, do not setText(), why this happens and how can I fix it?
Here's the code where i use the webservice and show the information:
public void Buscar () throws Exception {
InterfaceConsu2 IFC2 = new InterfaceConsu2();
ConsumirWS2 http = new ConsumirWS2();
Gson g = new GsonBuilder().setDateFormat("dd-MM-yyyy").create();
Cliente2 u = new Cliente2();
Type ClienteType = new TypeToken<Cliente2>() {
}.getType();
u.setCad_pes_cpf(DialogCPF.jFormattedTextField1.getText());
String url = "http://localhost:8080/clienteWebService/webresources/CadastroCliente/Cliente/get/"+u.getCad_pes_cpf();
String json = http.sendGet(url, "GET");
u = g.fromJson(json, ClienteType);
System.out.println(json + ("\n"));
System.out.println(u.getcad_pes_nome() +("\n") + u.getCad_pes_apelido() +("\n") + u.getCad_pes_cpf() +("\n") + u.getCad_pes_data()+("\n"));
String Date1 = new SimpleDateFormat("dd-MM-yyyy").format(u.getCad_pes_data());
System.out.println(Date1);
IFC2.jTextFieldNOME.setText(u.getcad_pes_nome());
IFC2.jTextFieldAPELIDO.setText(u.getCad_pes_apelido());
IFC2.jFormattedTextFieldCPF.setText(u.getCad_pes_cpf());
IFC2.jFormattedTextFieldDATA.setText(Date1);
IFC2.jTextFieldID.setText(u.getcad_pes_id());
}
using the System.out.println(u.getcad_pes_nome() +("\n") + u.getCad_pes_apelido() +("\n") + u.getCad_pes_cpf() +("\n") + u.getCad_pes_data()+("\n")); I receive all the information and show on the console, but, in my jFrame don't
Solution 1:[1]
Your problem is likely due to your setting the state of the wrong instance, here a new InterfaceConsu2 object, one likely totally unrelated to the one that is displayed (as a JFrame, presumably), since you appear to create a new instance within the method.
Suggestions to a solution:
- Don't create a new
InterfaceConsu2instance - Do not try to solve this using static fields
- Instead, give your
InterfaceConsu2class setter methods to allow outside classes to update its state without having to directly touch fields, fields that should be private - Pass a reference of the visualized
InterfaceConsu2to where it is needed. - Better still, learn about and use a M-V-C (model-view-controller) design pattern and update the model(s) where needed. The views should update themselves when listeners to the model notify the views of model state changes.
- As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others.
- You seem to have some long-running code in the posted method. If so, have this code run in a background thread such as within a SwingWorker, so as not to freeze the GUI.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hovercraft Full Of Eels |
