• Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing package not working  RSS feed

 
Jake Normandy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've specifically set Eclipse to use the JDK 7 through Windows-Preferences several times but the swing package still refuses to work. It says the import javax.swing.* cannot be resolved. It also says JFrame cannot be resolved to a type/variable. Here is the code (package name edited to "generic package name"). I've been trying to fix this for the last 3 hours and its driving me crazy...
package name;
import javax.swing.*;

public class Frame {

public static void main(String[] args){
JFrame frame = new JFrame("2d game");
frame.add(new Board());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 365);
frame.setVisible(true);
}
}
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you are having difficulties, stop using the IDE, and try running that code from the command line.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have added code tags to your code but it would not have madeit any better because you haven’t indented it properly.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran your code, minus the bit about the Board, without errors. What is your system PATH? Have you added a system CLASSPATH?
 
Jake Normandy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the indents that's just how the code copy and pasted. My Path variable is whatever the default is for Windows 8 and I think the problem may have something to do with that. I have not added a system CLASSPATH.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as a default PATH. Print it out with echo %PATH% and let’s have a look at it.
 
Jake Normandy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the current PATH variable.
C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\EgisTec MyWinLocker\x64;C:\Program Files (x86)\EgisTec MyWinLocker\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT

So is my next step changing it to the JDK? I tried to do this earlier through control panel, environment variables but I was unsure of what I should enter under variable value. Also the above variable is named "Path" not PATH, I don't know if that is significant or not. Thanks.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That PATH won’t work. And it doesn’t matter on Windows® whether it is path or PATH; it is case‑insensitive. I presume you got error messages when you tried to run your app from the command line.

Try the following instructions at the command line:
java -version
javac -version

We have instructions for setting the PATH. Close and reopen the command window, and repeat the above two instructions. If you installed Java the usual way, accepting the default location, your JAVA_HOME will be c:\Program Files\Java\jdk1.7.0_17 or similar. You might need "quote marks" round it because of the space.
I presume the path setting technique is the same on Windows®8 as on 7, but I am not certain.
 
Jake Normandy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I set JAVA_HOME and PATH like the instructions said and I'm still not having any luck. I restarted my computer too. When I attempt to import java.swing.* eclipse suggests other things like java.security etc.
I'm thinking about just uninstalling everything and reinstalling it to see if I did something wrong during installation. I checked where java jdk was saved and it was in the default location.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you run apps, even simple ones like hello world, from the command line? What do you get when you try echo %PATH% at the command line?

The fact that Eclipse suggests the security package doesn’t mean much. Eclipse always tends to give a dropdown list, and will show you everything beginning with an s.

There is a better way to import things in Eclipse: don’t write the imports yourself. When you first use the class name write JF then ctrl-space. You get a dropdown list and you choose the class you want and Eclipse automatically adds the import. Beware: there are often packages beginning com. which appear first because c is before j. The classes in those packages are usually not what you want.
 
Jake Normandy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting the same thing with echo %path% as I was before except the java location I added is before all the other stuff. Should it just be the jdk location? Would reinstalling everything do me any good?
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your PATH is wrong, then re-installation won’t do anything.
  • 1: Please show us the results of echo %PATH%
  • 2: Please run a Java app, even a simple one, from the command line and confirm that it runs correctly.
  •  
    Jake Normandy
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I successfully ran a helloworld app from the command prompt. I took a screen shot of what echo %path% printed out and attached it to this post. Thanks for your help.
    Screenshot (1).png
    [Thumbnail for Screenshot (1).png]
    echo %path%
     
    Campbell Ritchie
    Sheriff
    Posts: 53779
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So it would appear at present that the Java installation and PATH are working. Please use copy‑and‑paste because screenshots can be very difficult to read.

    Now I suggest you try a little Swing app from the command line. Here is the basic one which displays a blank frame.I ran it like this
    [campbell@localhost java]$ javac BlankScreen.java
    [campbell@localhost java]$ java BlankScreen "Campbell was here"
    Try that or something similar. You can miss out the command line argument if you wish. If that works, back to Eclipse.

    I shan’t be able to answer until tomorrow, so if anybody else is around, please look at this thread.
     
    Jake Normandy
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The app you gave me to run worked, but Eclipse still is not recognizing the swing package. I guess this means the problem is with Eclipse? I downloaded it with the ADT bundle on the android developer site. I'm not sure if this matters but at the top of the Eclipse window it says ADT and not Eclipse. The icon I click to get there says Eclipse and it looks exactly the same as all the Eclipses' I've seen on Youtube and other websites. Thanks.
     
    Darryl Burke
    Bartender
    Posts: 5167
    11
    Java Netbeans IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Swing isn't a part of Android. If you're planning to develop for Android, forget about using Swing.

    I don't use Eclipse, but it has a ton of settings so there may be a way to set the version you installed to develop Java, JDK-based applications. If there isn't then all you need to do is download and install a Java IDE. Or use the command line.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!