7

I have just setup Android Studio and started a simple application. Well started is an over statement, I got stuck on first few lines and am unable to import JFrame into android studio.

I have the latest SDK installed aling with LibGDX. I am still not able to setup JFrame. I have searched the net/youtube and no solutions have been found.

I can see the javax and swing in my external libraries but just cannot import.

Any ideas what I am doing wrong or not doing?

I am not looking for a "how to tutorial", I just a pointer where I should go hunting for the answer.

wow, not huge amount of response.

Please advise if I have asked a stupid question or difficult question.

public hungryDog() {

        JFrame jframe = new JFrame(); 
        Timer timer = new Timer(20, this); 

                renderer = new Renderer(); 
        rand = new Random(); 


        jframe.add(renderer); 
        jframe.setTitle("Hungry Dog"); 
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        jframe.setSize(WIDTH, HEIGHT); 
        jframe.addMouseListener(this); 
        jframe.addKeyListener(this); 
        jframe.setResizable(false); 
        jframe.setVisible(true); 


        dog = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20); 
        columns = new ArrayList<Rectangle>(); 


        addColumn(true); 
        addColumn(true); 
        addColumn(true); 
        addColumn(true); 


        timer.start(); 
    } 

enter image description here

Learningmind
  • 127
  • 1
  • 1
  • 6
  • What errors do you get, is it at build or run time? I doubt I can help fix the problem but in terms of getting responses you should probably put some more info. I get that your question is about how to import Swing but generally speakings no ones gonna write a custom how to for you, on here. From a quick search it seems like its possible, so i think your question is too generic. Few grammer and markup errors too – owen gerig Feb 16 '15 at 16:21
  • I am just unable to import javax.swing or anything to do with java. – Learningmind Feb 16 '15 at 16:28
  • are the jar files on the Java Build Path (Right click project and go to properties>Java Build Path>Libraries) ? – owen gerig Feb 16 '15 at 17:21
  • As you can see from the image javax is in my external libraries but it has a lock symbol next to it. I am guessing this might have something to do with it. – Learningmind Feb 16 '15 at 17:32
  • not really sure. check these links out though | http://stackoverflow.com/questions/11905415/java-packages-not-importing | http://stackoverflow.com/questions/4230075/how-do-i-add-the-javax-swing-packages-to-my-libs-for-my-android-project | http://stackoverflow.com/questions/14181142/eclipse-doesnt-find-javax-swing – owen gerig Feb 16 '15 at 18:18
  • you have android.jar expanded. i would think you would see an entry for swing at the same level as android.jar – owen gerig Feb 16 '15 at 18:48
  • I am really surprised that no one has asked this question before. I cannot find anything on the internet or stackoverflow.. Its amazing. I am sure it is something really simple. Thanks for your time and your effort to help. – Learningmind Feb 16 '15 at 19:06

3 Answers3

9

You can't use Swing on Android. Android has its own UI framework which is different, and Swing isn't supported. To see what Java APIs are supported on Android look at the API docs at http://developer.android.com/reference/packages.html

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
9

I used to have the same problem; here's how to solve it. You see, we were trying to use swing lib in a totally wrong context, that is, within an Android app. As Scott Barta pointed out, Android has its own mechanisms for doing what we wanted to achieve and that's why IntelliJ didn't let us import anything that would interfere with Android API. Hence, do NOT use Android app when, say, simply learning how to code in Java or, on a more advanced level, when testing/debugging your algorithms. Instead, build a standalone Java program (yes, in Android Studio). This very topic is covered here: Can Android Studio be used to run standard Java projects? , "Tested on Android Studio 0.8.6 - 1.0.2" by idunnololz). A few clarifying remarks to this solution: 1) wnen preparing your configuration (Run | Edit Configurations...), use your new Java module name and its main class in the appropriate fields. 2) before clicking Run make sure you selected same configuration.

Incidentally, there indeed IS a way to import swing library into any Android app: based on http://www.youtube.com/watch?v=fHEvI_G6UtI. Specifically, add a new line to build.gradle file for your Module:app: compile files ('<path_to_your_jdk's_rt.jar>') Like this: compile files ('C:/Program Files/Java/jdk1.8.0_31/jre/lib/rt.jar') Note single quotes and forward slashes. Then click Sync Gradle icon and enjoy the view.

In our particular case, however, the program won't run. The code itself will be clean, there'll be no errors; with swing lib well in place and connected, IntelliJ won't suspect a thing; the project WILL compile... but then we'll get a runtime error. Now your know why. On the other hand, this method works like magic when we do not interfere with Android API --- which we shouldn't in the first place.

That's all there is to it.

Community
  • 1
  • 1
Igor Soudakevitch
  • 671
  • 10
  • 19
0

Yes you can. As addition to Igor's answer here are the steps:

  1. Create new Android Studio project: File -> New -> New Project -> Phone and Tablet -> Add No Activity, select Java (not Kotlin) as language obviously.
  2. Add new Java module: File -> New -> New Module -> Java Library
  3. Remove Android module: File -> Project Structure -> Modules -> app, click - button.
  4. NOTE: step 5 is probably not needed. Try without it first.
  5. Edit build.gradle of your java module, and add in dependencies implementation fileTree pointing to the JRE you need.

Example of build.gradle:

apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation fileTree ('/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/rt.jar')
}

sourceCompatibility = "6"
targetCompatibility = "6"

Now code completion works and you can even run the app:

  • Click on Add Configuration: near run button
  • Click on + -> Application
  • Select your Main Class
  • At Use classpath of module: select your java module there (not project's)
Borzh
  • 5,069
  • 2
  • 48
  • 64