Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is java background process slow?

Please have in mind i'm new to java and had only used C# for desktop applications.

I just made a brand new project and want to load content as the window open, I have a JFrame with a JPanel(swing), i've tested many things until I found that windowOpened is the equivalent of the onload method from C#, the problem comes when trying to use this event to load content, after many tries I didn't get anything so I went to the basic and called a System.out.print("Test");.

The JFrame loaded succesfully but the message didn't show up in the console ultil I closed the window, then added a button to play around and added the same print and when clicking the button I got nothing in the console until I spammed it like 5 to 6 times, kind of weird so I added a showMessageDialog after the print and to my surprise the messagedialog works as expected but the print is still not working until I spam it many times.

This is my third brand new project trying to load content but all of them seems to work the same way, is it a problem with my computer or this is a normal behavior for java? can I change this behavior? what is the correct way to do this?

Here is the console logs:

cd PATH\NetBeansProjects\mavenproject1; "JAVA_HOME=C:\\Program Files\\Java\\jdk-16.0.2" cmd /c "\"PATH" -Dexec.vmArgs= -Dexec.args=\"${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}\" -Dexec.appArgs= -Dexec.mainClass=.mavenproject1.NewJFrame -Dexec.executable=\"PATH" -Dmaven.ext.class.path=\"PATH"
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...

-----------------------< mavenproject1 >------------------------
Building mavenproject1 1.0
--------------------------------[ jar ]---------------------------------

--- exec-maven-plugin:3.0.0:exec (default-cli) @ mavenproject1 ---
not working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expectednot working as expected
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  13.098 s
Finished at: 2021-08-16T17:14:40-05:00
------------------------------------------------------------------------

And here is the code:

/*
 * 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 .mavenproject1;

import javax.swing.JOptionPane;

/**
 *
 * @author 
 */
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() {

        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(196, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(131, 131, 131))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(196, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(79, 79, 79))
        );

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

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
       System.out.print("not working as expected");
    }                                 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       System.out.print("not working as expected");
       javax.swing.JOptionPane.showMessageDialog(this,"","",JOptionPane.OK_OPTION);
    }                                        

    /**
     * @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                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   
}
like image 747
Ezequiel Avatar asked Jul 12 '26 19:07

Ezequiel


1 Answers

As @apangin said it is because of buffered output, by simply changing print to pringln, you can see the instant result which is what i was looking for.

like image 199
Ezequiel Avatar answered Jul 14 '26 09:07

Ezequiel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!