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.
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.