Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to import javax.swing in android studio

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

like image 860
Learningmind Avatar asked Feb 16 '15 15:02

Learningmind


People also ask

What is import javax.swing * used for?

swing. Provides interfaces that enable the development of input methods that can be used with any Java runtime environment. Provides a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms.

Can I use Swing for android?

There is no way you can use swing in android, because android is not based on JavaSE, while swing is. android uses a special java that is designed to run on DVM . Even if their is no compatibility issue. Swing is used for desktop apps which differ in their UI completely from mobile apps.

What is swing javax?

Swing is a GUI widget toolkit for Java. It is part of Oracle's Java Foundation Classes (JFC) – an API for providing a graphical user interface (GUI) for Java programs.

What is import Java Swing JFrame?

JFrame class is a type of container which inherits the java. awt. Frame class. JFrame works like the main window where components like labels, buttons, textfields are added to create a GUI. Unlike Frame, JFrame has the option to hide or close the window with the help of setDefaultCloseOperation(int) method.


1 Answers

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.

like image 189
Igor Soudakevitch Avatar answered Oct 12 '22 19:10

Igor Soudakevitch