Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

swing is not working for me ...

Status
Not open for further replies.

PaulMoranFX

Programmer
Jun 13, 2004
7
AU
Hi All,

Im very new to Java and I am doing the simple 'Hello World' swing tutorial...

When i run the HelloWorldSwing.java file from my console it pops up the 'HelloWorld' code in Notepad instead of the expected JFrame ...

What have i overlooked ??

Cheers for any help

Paul
 
Hi,
what do you do for start the program?

you have to compile the class before and then execute it with the java command...


 
hey mate,

ooops...should have cleared that up...I have compiled it..in my dir I have HelloWorldSwing.class and HelloWorldSwing.java ...

I used javac c:\sun\appserver\jdk\bin HelloWorldSwing.java to compile and get the .class file...then i run java c:\sun\appserver\jdk\bin\<working dir>\HelloWorldSwing

When i do this i am now getting the following errors:

Exception occured during the event dispatching:java.lang.NoSuchMethosError at HelloWorldSwing.createAndShowGUI(helloworldswing.java:11) at HelloWorldSwing.access$000(helloworldswing.java:3) at HelloWorldSwing$1.run(helloworldswing.java:31) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unkown Source) at java.awt.EventDispatchThread.pumpOneEventForHeirarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unkown Source) at java.awt.EventDispatchThread.pumpEvents(Unkown Source) at java.awt.EventDispatchThread.run(Unkown Source)

Im quite confused...

Cheers mate...hope to hear from you soon

Paul
 
Can you post your code?
So we can see what do you do in this class...
 
import javax.swing.*;

public class HelloWorldSwing {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
 
Well the interpreter is moaning that there is no method called "createAndShowGUI" -but there clearly is. So I would say that you may have an old version of that code somewhere which the interpreter is picking up on maybe. Try running it like this :

"java -cp <your_directory> HelloWorldSwing".

 
do i try that from the bin directory or the 'source' directory ?
 
It shouldn't matter ... You can add the java.exe to your PATH so you don;t have to keep typing the whole path to the java bin directory like this :

set PATH=c:\sun\appserver\jdk\bin;%PATH%
 
change the line createAndShowGUI(); to
new HelloWorldSwing().createAndShowGUI();

//or

HelloWorldSwing.createAndShowGUI();
After you change the code, you can run the code
 
Like sedj, I tried your code and it worked. So the problem is not your code.

Are you aware that your source will produce two class files?

HelloWorldSwing.class
HelloWorldSwing$1.class

Both class files may be in the same directory, but if the classpath doesn't point to that directory then the second class file may not be found.

Try using the -cp option when running the program to explicitly set the classpath to the directory that contains the class files.
 
toolkit:
I am using J2EE 1.4 SDK that came with the donload of 'Sun Java™ System Application Server Platform Edition 8'

I hope thats ok ??

stefanwagner:
I set that environment variable and i still get the same error.



I have updates that web page wth more console read outs..

Hope to hear from you all asap :)

and thanks a bunch for all your help!!! :D

Cheers

Paul
 
works for me too.
No classpath, just JAVA_HOME (1.4.2).
I don't know appserver-installations.
Have the source in
Code:
/home/stefan/proj/mini/forum/HelloWorldSwing.java
pwd
/home/stefan/proj/mini/forum
javac HelloWorldSwing.java
java HelloWorldSwing

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top