What Java Listener to use to populate a TextField?

Good morning,

What Java Listener in IntelliJ IDEA can I use to populate a Textfield in a new form?

I have used windowOpen, windowOpened, and formOpen, and it seems like the TextField control has not been drawn yet, since a TextField command does not update the control.

When I run the above code, the Textfield (TFISBNtmp) does not show the text ‘windowOpen’, and the println does show the text in the intelliJ IDEA IDE.

I can run the same setText code in the addPropertyChangeListener, and it will update the TextField. However, the addPropertyChangeListener will fire about 3 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 that fires only once when a new form is created and the TextFields are drawn?

Thanks a bunch! :slight_smile:

this is a question that I seriously want also to get an answer to.

currently I have set a timer up in the window that looks at a static string in a ‘global’ class and that does exactly what I want, but is very clumsy and I want to do it properly!

I put the DoGetRecord with the ID passed over from the search screen in the main after setting up the GUI. I don’t use any java listener for this

are you looking for something akin to Xojo’s “open” event for a control ?

I dont think there is such a thing in Java as thats a thing Xojo’s framework makes happen

I just whipped up a small sample in NetBeans and in the windowOpened() listener set the text of the pushbutton and it shows up exactly as I expect

I suppose something like the Open event for a control could be done if a person wanted to simulate the Xojo framework with their own “framework” on top of Swing etc

Yep, looking for something like the Open event for a window to populate a control.

I just created a simple Java IntelliJ example where a new window is created, and the textfield is not populated.

Here is a link to download a demonstration example that I quickly created: Download JavaTextField.zip example (JavaTextField)

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

Like Norman said: there is none. The Button is exact opening when instantiated and published. Before it is not existing. So when I generate a jButton: jButton button = new jButton(); and later add the functionality to my pane pane_something.add(button);
the button exists when the window is shown. If not the button is not really existing but generated in the code and can be overloaded.

The good news is that I figured out how to implement the windowOpened event in IntelliJ, and the example project can be downloaded here: JavaOpenEvent.zip.

Running the Project will show the FirstForm, and then pressing the button will show the SeondForm - which has the windowOpened event added.

Unfortunately, it seems like using multiple JFrames is usually not recommended: The Use of Multiple JFrames: Good or Bad Practice?

It seems like many consulting Java developers almost-always use the Eclipse IDE (at least in the United States). IntelliJ creates a form that is created from an xml file, where experienced Java programmers seem to write raw code and/or use Eclipse with a few plugins.

One of the plugins was “Jigloo” which created the form output in plain old Java. Unfortunately the company went belly-up.

This makes me wonder about Eclipse as an IDE rather than IntelliJ?

Unfortunately, it seems like using multiple JFrames is usually not recommended: The Use of Multiple JFrames: Good or Bad Practice?

Thats really more a UI Design question that good or bad practice
Akin to asking if you should use multiple windows in Xojo vs a tab panel/page panel
And the answer is it depends on the intended UI design

It seems like many consulting Java developers almost-always use the Eclipse IDE (at least in the United States). IntelliJ creates a form that is created from an xml file, where experienced Java programmers seem to write raw code and/or use Eclipse with a few plugins.

That a person CAN write code for a UI is, in some circles, seen as a badge of honor or some indication of a superior skill set.
IMHO Bullshit
Its handy to know how to do - certainly not required
Some IDE’s like Net Beans you can draw the UI and it just write that java code for you
Nothing magic
It just makes creating the UI a little easier since you can deal with a visual designer then tweak as you want

This makes me wonder about Eclipse as an IDE rather than IntelliJ?

Ick
I have never liked Eclipse and I would struggle to tell you why

1 Like

What I did not get is: for what the heck the window open event is needed at all???

In Case of Single or multi jframe: that is only meant in case of simultaneous use. For different windows you can use different frames and you of coarse can dispose one and open the next.

A few million programmers using eclipse, a few million using IntelliJ a few million NetBeans. A few million using jformdesigner. What I would always prefere.

The xml Definition of windows isn’t a question of good or bad programming. Jformdesigner is xtimes faster than Hand coding of gui and less buggy.

What I do not need at all and since twenty years never needed was and is and will be a window open event. Needs only time and gives no sense at all. One of the most stupid decisions of Xojo is stealing all control with the only possible use of elements with its predefined events. Don’t do that is my first reflex for this, please really don’t. It makes your live in the first view less complex.

Instead of riding on the idiotischste between hand coded gui and jformdesigner or eclipses set designer I would suggest to spend the time for coding and use jformdesihner for a few dollars and that’s it. Or use javaFx with scenebuilder which is entirely for free

1 Like

I think netbeans create java file from GUI Form

Yes, Netbeans creates the Java File from it’s Swing form builder. But this has nothing to do with the need of an open event??? I didn’t get why should it be used? It produces bad practice Java Cade as I could see. There is nothing it Is making less complex, nothing what is giving it to me as coder. So maybe it is only for Xojo programmers a want to have but for me as Java programmer an example of making code unreadable and complex and blow it up with not needed events.

The only reason for attempting to use the window open event is to answer the original question. Unfortunately, this event is likely called before the Text field in the second form is drawn and able to accept changes with the serText command.

I will keep experimenting until I figure out ‘which java listener to use to populate a text field’.

Thanks for the good discussion. :slight_smile:

I notice the code generate from java form in netbeans… seem like it is more complicate that it need to be.

The other way around: it is best practice GUI coding. You can trust the GUI coded by Netbeans and that counts.

IF you conceive of the JFrame as the “controller” between the model and view then why wouldnt you use the open event to present data from the model in those controls ? This is sort of how Xojo’s MVC style of coding works

An example of what you WOULD do would likely be instructive

The concept is Model->View->Controller. This is the Java best practices way of programming GUI Software with Java Swing and also Java FX.

Hereby the Frame is no Controller and should never be a controller, Frame is a part of the View. In the inheritance structures of the languages you can see this concept deeply. The same you have with JavaFX, there you have a Scene instead of the Frame. The Scene represents like the Frame the View as is and not a controller or a controller instantiating.

Therefore I for example write always the Controller with the Window (with JFormdesigner the View is represented by XML, with Netbeans it is inside the Code). There you have always the chance to implement the initialization with a main functionality. This has not even to be a statics main method, it can be a non static method. This method catches the Data I want to fill in when opening the form. That is the moment I am defining the Frame, adding the Form, packing that completely and inserting the Data and at the end setting the Frame visible and let the window before dispose. Within this methodical way I have to decide to do that with statical methods of with dynamical methods.

What ever I am doing, I can reach the dynamic method insights only when I can reach it’s instance. When generating it static I can set the variables of the form also public static so that I can change them when ever I want. Or I will set the variables nor static but generate getters and setters.

But what ever I want to do: when packing the form, there is the Moment where I have to place that what I would place in the open event. Nowhere else.

No connector, no nothing else is needed when programming this way. It is not an open event but the opening method where to define this. Why should I setup a listener for opening the form which costs performance and makes the code for every Java programmer looking like a Zombie?

Sure from a purist point of view having the frame act as controller is probably NOT ideal

BUT, folks need a way to move from what they know to this ideal

An example would go along way to illustrating what you’re saying

At the point where you say define the jframe, behind the pack(); the code is supposed to be. No other event stuff needed. At the end setVisible(true); and that’s it. Did I missed something???

pack() ?

Net beans is apparently writing things differently - why I was saying an example owuld be useful

This is what I get from a new NetBeans project where I added one JFrame

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication5;

/**
 *
 * @author npalardy
 */
public class JavaApplication5 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
    
}

And the jFrame (which includes its own static main)


/**
 *
 * @author npalardy
 */
public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

Hence why I say “an example of what you do, are talking about, would be very useful”
Code would help I’m sure