Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

What Java Listener to use to populate a textField in a new Form?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Java Listener should be used when populating a textField in a new form with data?

When a new form is created, I have attempted to used windowOpen, windowOpened, and formOpen, and it seems like the control has not been drawn yet, since setting the text on a textField control does not update the control.



When I run the above code in either of the three Listeners, the textField (TFSIBNtmp) does not show the text in 'windowOpen' listener metohd, and the println does show text in the IntelliJ IDEA IDE.

The same setText command in the addPropertyChangeListener can be successfully run, and the textField text is updated. However, the addPropertyChangeListener will fire about three times while the new form is being drawn, which can cause other issues when redrawing/updating many times.

What Java listener should/could I use to populate a textField control that fires only once when a new form is created and the textFields are drawn?

Thanks a bunch

Using:
Windows 10
IntelliJ IDEA
Java
 
Rancher
Posts: 187
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried a WindowListener on the top-level Window yet? If so: Show us how. It may be an issue of order by adding the listener to late. Always call the final Window.setVisible() as your very last statement in your startup method.
 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Matthew,

Here is a link to download a demonstration example that I quickly created: Download JavaTextField.zip example (https://www.mediafire.com/file/oayupxpkx4jkus3/JavaTextField.zip/file)

The file is called JavaTextField.zip, and please unzip it so that the project can be read in IntelliJ IDEA. In the first form, press the Open button, which opens a second form. In the second form there is a couple of labels and a TextField. The textfield 'should' update from either of the windowOpen, windowOpened, or formOpen events. I have the TFTest.setText("propertyChange") line of code commented out. If the comment is removed, the textField will show the text 'propertyChange', and in the console there are four lines of 'propertyChange' shown, which means the propertyChange method is called four times when the form is being created.

Thanks for your help.

I am using the following systems:
- openjdk-16 (java version 16.0.2)
- Windows 10
- IntelliJ 2021.1.3 community edition

P.S. After looking at your suggestion, I can't seem to figure out how a Window Listener works... Trying out a few versions now...  
 
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would I be correct that you're asking about Swing components?

(By the way, asking people to install IntelliJ IDEA and download a zip file will substantially reduce the number of people who might consider answering your question. If you have a small example code -- which would be an excellent idea -- it would be better to post it here where everybody can read it.)
 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Paul,

My question 'might' be around swing, or it might be on the topic of IntelliJ, or it might be consistent with the Java programming language. My guess is the answer to your question might be 'maybe'? I read many of the posts on this forum, and there were consistent requests for a minimum working program as an example.

(By the way, asking people to install IntelliJ IDEA and download a zip file will substantially reduce the number of people who might consider answering your question. If you have a small example code -- which would be an excellent idea -- it would be better to post it here where everybody can read it.)


I agree. This may beg the question 'what is the most common IDE so that I can fulfill your request to maximize the number of people who may consider answering my question?', while also fulfilling the other requests? I am open for suggestions.

Here is the code for FirstForm.java


And here is the code for the SecondForm



Does this help?

edit: Fixed spelling mistakes.
 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eugene Dakin wrote:What Java Listener should be used when populating a textField in a new form with data?



Well, this clearly isn't a question about IntelliJ so I'll move the thread to the Swing forum.

Thanks for posting your code here, by the way.
 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, this clearly isn't a question about IntelliJ so I'll move the thread to the Swing forum.



Thank you.
 
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see where you're actually creating a new JTextField.
 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why you feel the need to use a listener to initialize that text field.

First of all you don't even create a text field, so you're just going to get NullPointerExceptions when you run that code. Your SecondForm class simply needs some code which creates a JTextField, adds it to the JFrame, and initializes its value. You'd put that before line 27. Unless there's some other requirement that I'm missing.

And, welcome to the Ranch!
 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please follow Java naming conventions, variable names must begin with a lower case letter: SomeString, and TFTest.
 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't see where you're actually creating a new JTextField.



I believe that IntelliJ creates an xml format for creating the form components. Here is a listing of the xml for the SecondForm.form code:

 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't understand why you feel the need to use a listener to initialize that text field.

First of all you don't even create a text field, so you're just going to get NullPointerExceptions when you run that code. Your SecondForm class simply needs some code which creates a JTextField, adds it to the JFrame, and initializes its value. You'd put that before line 27. Unless there's some other requirement that I'm missing.

And, welcome to the Ranch!



I guess this would be the reason for creating a minimal running program, as there is a TextField created, and I have it working when running the IntelliJ code using propertyChange:


Above is commented code that can update the TextField, and works as it appears(?) that the TextField has been drawn, and can then accept the setText command. Maybe this has something to do with the order in which IntelliJ creates a form and draws the controls?

Thanks for the kind welcome to the Ranch
 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please follow Java naming conventions, variable names must begin with a lower case letter: SomeString, and TFTest.



Thank you for the suggestion Carey. I know too many programming languages, and naming conventions often get thrown-out-the-window, as there are positive aspects to some, while other conventions have merit. I will attempt to start implementing Java naming practices for this form.

Thanks!
 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it hard to believe that the XML you just posted actually generated the code you showed us earlier.

However I notice that this line of XML:

is in a place where it looks like it should apply to a JLabel, rather than the JTextField which is described later.

It looks to me like you're trying to learn Swing and IntelliJ at the same time, which I think would be harder than doing it one at a time. I would expect you to run into problems where you can't tell whether it's Swing or IntelliJ that you don't quite understand.
 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I find it hard to believe that the XML you just posted actually generated the code you showed us earlier.


I am not sure what IntelliJ does in the background, and the xml line that you mentioned is a JLabel that I dragged-and-dropped onto the SecondForm.form, which has the added text "The textField should be populated when this form first opens". I am guessing that the IntelliJ IDEA IDE has this information stored in a file somewhere and the JLabel is updated with the string. <shrug> = I dunno

Edit: When looking at the xml file, the parent of the line-of-code that you highlighted shows the creation of a Jlabel.


It looks to me like you're trying to learn Swing and IntelliJ at the same time, which I think would be harder than doing it one at a time. I would expect you to run into problems where you can't tell whether it's Swing or IntelliJ that you don't quite understand.



Yes you are correct and I am learning both Swing and IntelliJ at the same time. My wild guess (and thats all it truly is) is that the Java code is being mostly executed before some of the controls are being drawn, which is why the order-of-code-execution is important.
 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're going to continue down that road, I would suggest starting with the simplest possible example. (I would suggest that anyway.) One form, one text field. See if you can get that working.
 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:If you're going to continue down that road, I would suggest starting with the simplest possible example. (I would suggest that anyway.) One form, one text field. See if you can get that working.



Hi Paul,

Thanks for making the suggestion. Can I ask what is the most common IDE for making a Java program which is popular among the programmers on this forum? I will download it so that my question is easier to understand, and I will convert the code from IntelliJ to native Java.

Warm regards
 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would recommend not using an IDE, or at least not using one which tries to write Swing code on your behalf. Instead I would recommend starting by writing the code yourself. (You can do this in an IDE, but just limit yourself to typing the code into a blank screen to start with.) There's a lot of Swing tutorials out there on the web, Oracle wrote a whole series of them back a decade and a half ago when Swing was more of a thing. They even come with prewritten code which you can examine and then play with. Try this one for a start: How to Use Text Fields.
 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would follow Paul's advice and avoid IDEs for the time being, they tend to have their own learning curve that gets in the way of learning Java in the beginning.

To answer your question though, In my 20+ years of Java consulting, every client used Eclipse.
 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I would follow Paul's advice and avoid IDEs for the time being, they tend to have their own learning curve that gets in the way of learning Java in the beginning.

To answer your question though, In my 20+ years of Java consulting, every client used Eclipse.



Thank you, this is VERY helpful.

Warm regards.
 
Paul Clapham
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using Eclipse too, but I don't use the plugin which allows me to design Swing forms graphically.

That's mostly because my Swing forms were basically all designed before I started using Eclipse; these days I rarely design new forms and it's not worth my time to learn the Eclipse plugin which would let me do that and figure out how to make it design forms which look consistent with the ones I already have. If I had a lot more forms to generate, I might spend the time, but that's not the case.

I've heard good things about IntelliJ, but I'm not switching from Eclipse because it's not worth my time to retrain myself on a new IDE. Installed System Bias, or something like that, is the name of that way of thinking.

 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used the "Jigloo" plugin for a number of years. I really liked it but it never caught on and the company went belly up. It didn't impact my project though because the output was plain old Java code not xml or some such.

One wonderful thing I got out of the experience was an appreciation of GridBag which is "expert friendly" but incredibly powerful in terms of layout design. There's copies of a GBC utility (Java source code) floating around that makes the task manageable and the code more maintainable. Still, I wouldn't recommend GridBag as a place to start.
 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I'm using Eclipse too, but I don't use the plugin which allows me to design Swing forms graphically.

That's mostly because my Swing forms were basically all designed before I started using Eclipse; these days I rarely design new forms and it's not worth my time to learn the Eclipse plugin which would let me do that and figure out how to make it design forms which look consistent with the ones I already have. If I had a lot more forms to generate, I might spend the time, but that's not the case.

I've heard good things about IntelliJ, but I'm not switching from Eclipse because it's not worth my time to retrain myself on a new IDE. Installed System Bias, or something like that, is the name of that way of thinking.



Yes I heard good things about IntelliJ, unfortunately the documentation is not the easiest to understand (at least for me). I'll finish my current project with IntelliJ, since it is required by the customer. I'll give Eclipse a try once this project is complete. Thanks for the kind words :)
 
Eugene Dakin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I used the "Jigloo" plugin for a number of years. I really liked it but it never caught on and the company went belly up. It didn't impact my project though because the output was plain old Java code not xml or some such.

One wonderful thing I got out of the experience was an appreciation of GridBag which is "expert friendly" but incredibly powerful in terms of layout design. There's copies of a GBC utility (Java source code) floating around that makes the task manageable and the code more maintainable. Still, I wouldn't recommend GridBag as a place to start.



The deprecated plugin scenario that you mentioned is sooooo familiar. I am quite hesitant when using plugins, since I have been burned with some of my project due to them being discontinued, obsolete, deprecated, and fill in the rest of the possibilities here  I like the idea of a plugin that makes Java code, since the raw code can be modified in the future.

Warm regards.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic