AndroidMoron
AndroidMoron

Reputation: 35

android - how to convert java swing to android?

I am a total noob to android and really have very hard time understanding some concepts. I also have a little understanding of java swing. I am a bit knowledgeable about java web app using spring mvc. I am using eclipse indigo

This is what happened:

After I successfully created android app named AndroidExer I created a package com.swing.demo and placed the already running swing source code JTextAreaDemo.java (The source is from a tutorial. This is working in pure java). It seems that that android doesn't recognize swing packages (I don't know if I understand it correctly, please help me) because I got alot of errors and most of them says <Class name> cannot be resolved to a type. When I check for suggestions, eclipse does not include any suggestion about importing which makes me think that android doesn't recognize swing.

I tried a bit of research and I found out that android cannot run swing. can we run java swing and applet on android

My problem is how will I convert the simple swing demo to android. I don't have any clue where and how to start. I really need help. BTW, the code is for JTextAreaDemo.java is down below:

JTextAreaDemo.java

package com.swing.demo;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class JTextAreaDemo extends JFrame implements ActionListener {

   /**
 * 
 */
private static final long serialVersionUID = 5416184196156296457L;
JTextField jtfInput;
JTextArea jtAreaOutput;
String newline = "\n";
public JTextAreaDemo() {
    createGui();
}
public void createGui() {
    jtfInput = new JTextField(20);
    jtfInput.addActionListener(this);
    jtAreaOutput = new JTextArea(5, 20);
    jtAreaOutput.setCaretPosition(jtAreaOutput.getDocument()
            .getLength());
    jtAreaOutput.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(jtAreaOutput,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    GridBagLayout gridBag = new GridBagLayout();
    Container contentPane = getContentPane();
    contentPane.setLayout(gridBag);
    GridBagConstraints gridCons1 = new GridBagConstraints();
    gridCons1.gridwidth = GridBagConstraints.REMAINDER;
    gridCons1.fill = GridBagConstraints.HORIZONTAL;
    contentPane.add(jtfInput, gridCons1);
    GridBagConstraints gridCons2 = new GridBagConstraints();
    gridCons2.weightx = 1.0;
    gridCons2.weighty = 1.0;
    contentPane.add(scrollPane, gridCons2);
}
public void actionPerformed(ActionEvent evt) {
    String text = jtfInput.getText();
    jtAreaOutput.append(text + newline);
    jtfInput.selectAll();
}
public static void main(String[] args) {
    JTextAreaDemo jtfTfDemo = new JTextAreaDemo();
    jtfTfDemo.pack();
    jtfTfDemo.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    jtfTfDemo.setVisible(true);
}
}

I really need your help. Thanks.

Upvotes: 0

Views: 8591

Answers (3)

vector
vector

Reputation: 7576

Start simple. You build the interface first. Then start adding some 'plumbing' to it.

Break the process down into small steps and you'll figure it out. You'll have an easier time getting help when you're stuck on something.

I'm afraid the best you can hope for is reuse business logic that your swig app would use. Beyond that you have to create the thing from scratch.

Upvotes: 2

didityedi
didityedi

Reputation: 171

As long as I know, there is no program that can directly convert them to android layout, but as fact android and java is very similar. If you are using eclipse, then building the UI won't be so hard. you can also use these layout tutorial, events tutorial, and control tutorial from android developers to start learning.

Upvotes: 1

kosa
kosa

Reputation: 66637

I don't think you can mix those two. If you want android native app, you need to use Android API than Swing. Here is android documentation.

Upvotes: 3

Related Questions