-1

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

  • 1
    Without a decent [mre] code post, it is hard to say *exactly* what may be wrong, but we do see that you are creating a *new* `InterfaceConsu2` object in this method and are setting the state of it in the method. Is this the same `InterfaceConsu2` object that is displayed to the user? Are you *absolutely* sure? That it is a new object suggests otherwise, meaning that you may be setting the state of the *wrong instance*. – Hovercraft Full Of Eels Feb 09 '22 at 17:40
  • 1
    As an aside, you will want to learn and use [Java naming conventions](http://en.wikipedia.org/wiki/Naming_convention_(programming)#Java). 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. – Hovercraft Full Of Eels Feb 09 '22 at 17:41
  • 1
    Pro tip: Simplify your code to troubleshoot. If you suspect the component is failing to set the text, remove all this webservice stuff and hard-code a value. If the hard-coded value is set, then you know the problem is with the webserivce code. If it doesn't, you successfully narrowed the possibilities and that will help you get to the root cause quicker. Right now, you have too many moving parts. – hfontanez Feb 09 '22 at 17:51
  • @HovercraftFullOfEels it is kinda hard to make a minimal reproducible example because is a really long code, About Java naming conventions, I agree I'm a beginner, so all the time I make this small mistakes. About the code, yes i'm creating a new `InterfaceConsu2` in each function, this one i showed i'm searching, but, there are others functions But, if I use `InterfaceConsu2 IFC2 = new InterfaceConsu2();` I get java.lang.StackOverflowError and if I use `InterfaceConsu2.jTextFieldNOME.setText(u.getcad_pes_nome());` I get non static method cannot be referenced from a static context – Luís Ambrozin Feb 09 '22 at 18:39
  • And I don't know where this class is static, so all the time I need to use a `InterfaceConsu2 IFC2 = new InterfaceConsu2();` – Luís Ambrozin Feb 09 '22 at 18:40
  • @LuísAmbrozin: classes are not static per se (unless we're talking inner classes), *variables* are, and that this is happening suggests that you're not yet clear on how Java implements the object-oriented paradigm, causing other unrelated problems. Yes, creating a decent [mre] does require quite a bit of effort on your part, but please understand that no one can fully answer your current question without guessing because you don't have one in your question. It is really needed, or as close to one as you can get. – Hovercraft Full Of Eels Feb 09 '22 at 18:43
  • @hfontanez I tried to use `IFC2.jTextFieldNOME.setText("AAA");` to tested but also, not working, so it's not the webservice, also because using a sout I could manage to print all the information – Luís Ambrozin Feb 09 '22 at 18:44
  • One recommendation for future code has been well-suggested by @hfontanez: strive to make your classes smaller, tighter testable units, code that can be run and tested in isolation. This can help you debug, and if still stuck, can help you with code that is easy to use in a [mre] to post here. – Hovercraft Full Of Eels Feb 09 '22 at 18:50
  • 1
    @LuísAmbrozin "it is kinda hard to make a minimal reproducible example because is a really long code_" - if you do what Hovercraft and I have suggested, this problem would be less impactful. I have helped countless people with problems here where I have stubbed out some non-essential classes and methods by simply analyzing their _minimal_ reproducible code. And this is not a thing for SO, this is a thing for the real world as well. It is very easy to lose focus (and make mistakes) when your code is really long. – hfontanez Feb 09 '22 at 19:25

1 Answers1

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 InterfaceConsu2 instance
  • Do not try to solve this using static fields
  • Instead, give your InterfaceConsu2 class 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 InterfaceConsu2 to 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.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    @LuisAmbrozin pay attention to this point: "_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._" - One of the things I felt victim to when I was learning Swing over 16 years ago. It will give you the impression that nothing is happening (i.e. `setText()` is not setting any text). – hfontanez Feb 09 '22 at 17:56