Avatar of Kennywen
Kennywen

asked on 

java Class FAQ

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
Java

Avatar of undefined
Last Comment
Kennywen
Avatar of Kennywen
Kennywen

ASKER

if i use:

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

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

thanks
Avatar of concon
concon

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.
Avatar of Mayank S
Mayank S
Flag of India image

I don't understand your question properly.
Avatar of Kennywen
Kennywen

ASKER

i mean the new popUp().createComponents(); will call the public popUp() function rite?
Avatar of Mayank S
Mayank S
Flag of India image

It should.
Avatar of Kennywen
Kennywen

ASKER

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

what is the above function???
Avatar of Mayank S
Mayank S
Flag of India image

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?
Avatar of Kennywen
Kennywen

ASKER

in main method.
Avatar of Mayank S
Mayank S
Flag of India image

You mean:

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

}

??

That will obviously not call popUp ().
Avatar of Kennywen
Kennywen

ASKER

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

thanks
SOLUTION
Avatar of concon
concon

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Kennywen
Kennywen

ASKER

thank you and forgive my poor english.
Avatar of Mayank S
Mayank S
Flag of India image

Glad to help ;-)
Avatar of Kennywen
Kennywen

ASKER

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
Avatar of Mayank S
Mayank S
Flag of India image

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.
Avatar of Kennywen
Kennywen

ASKER

thanks again
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo