We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

java Class FAQ

Kennywen
Kennywen asked
on
Medium Priority
329 Views
Last Modified: 2013-11-23
i have a java program like below:

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class popUp extends JPanel implements ActionListener
{
      public popUp()
      {
            System.out.println("popUp");
      }

      public static void main(String[] args)
      {
            new popUp().createComponents();
      }
}

i have i question that is y i didn't call the popUp function but the popUp function will call by itself?

thanks
Comment
Watch Question

Author

Commented:
if i use:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createComponents();
            }
        });

then the popUp will not run by itself ... why?

thanks

Commented:
Hi Kennywen,

popUp() is the constructor of the class. and if your create new instance of class it runs.

also you can not call a non-static method from a static method. so you need to create an instance of popUp class in order to call another method from main.

Regards.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
I don't understand your question properly.

Author

Commented:
i mean the new popUp().createComponents(); will call the public popUp() function rite?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
It should.

Author

Commented:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createComponents();
            }
        });

what is the above function???
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
It causes the run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI.

Where are you writing that invokeLater () code?

Author

Commented:
in main method.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
You mean:

public static void main ( String args[] )
{
  SwingUtitlies.invokeLater ( .... ) ;

}

??

That will obviously not call popUp ().

Author

Commented:
sorry, why the  SwingUtitlies.invokeLater ( .... ) ; will not call popUp ()?

thanks
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Principal Technologist
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thank you and forgive my poor english.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Glad to help ;-)

Author

Commented:
those method that call by the main method must be static rite?

is there anyway to create a non-static method from the main method without create an instance of it?

thanks
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
No. You need to make an object to call the non-static method. Otherwise, you will not be able to do it.

The reason why you were able to call createComponents from main () was that your main () is in the popUp class. Try putting the main () separately in a different class and use the same SwingUtilities.invokeLater () code - I don't think that it'll compile.

Author

Commented:
thanks again
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.