Scraping data from a applet.

Hi, Folks

I need to scrap data from JTextField component that is placed on the ContentPane component of my Applet. I have only references on the applet instance. I do not know if this particular JTextField component has been set with a name.

How can I do it?

Thanks all for reply.

[305 byte] By [Vitaliy.Vintonyaka] at [2007-11-27 2:42:31]
# 1
?setText("");?
hiwaa at 2007-7-12 3:07:14 > top of Java-index,Java Essentials,Java Programming...
# 2

> setText("");

Scrape, rather than scrap, I think.

The talk of content pane suggests a JApplet rather than an Applet. But, either way, you might be able to do something with the Container method getComponentAt(x,y). Of course this depends on having a reference to the applet.

It all sounds rather odd. If what you really have is someone else's applet on a web page and you want information from it, you are basically stuck with whatever public interface it provides.

pbrockway2a at 2007-7-12 3:07:14 > top of Java-index,Java Essentials,Java Programming...
# 3

Thank you for reply, Peter.

I'm really had made a mistake. I had in view "Scrape", of course.

I found one of suggestions (I guess):

import java.awt.*;

import java.util.*;

import javax.swing.*;

class SomeApplet extends JApplet

{

public void init()

{

final JTextField textField = new JTextField();

textField.setText("Unique char sequence");

final JTextField textField2 = new JTextField();

textField2.setText("Unique char sequence2");

getContentPane().setLayout(new FlowLayout());

JButton button = new JButton("One");

button.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

textField.setText("Unique char sequence2");

}

});

getContentPane().add(button);

getContentPane().add(textField);

getContentPane().add(textField2);

}

public void start()

{

setSize(200, 200);

setVisible(true);

}

}

public class AppletWrapper extends JFrame

{

private JApplet applet;

public AppletWrapper(JApplet applet)

{

this.applet = applet;

setSize(200, 200);

getContentPane().add(applet);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public StringBuffer getComponentText(Object obj) {

StringBuffer strBuffer = new StringBuffer();

f(obj instanceof Container) {

Container cont = (Container)obj;

if (cont.getComponentCount() > 0)

strBuffer.append(getContainerText(cont,new StringBuffer()));

}

if(JTextField.class.isInstance(obj)) strBuffer

.append(((JTextField)obj).getText());

if(JLabel.class.isInstance(obj)) strBuffer

.append(((JLabel)obj).getText());

if(JTextArea.class.isInstance(obj)) strBuffer

.append(((JTextArea)obj).getText());

if(JButton.class.isInstance(obj)) {}

System.out.println(obj);

return strBuffer;

}

public StringBuffer getContainerText(Component comp, StringBuffer strBuffer)

{

if (comp instanceof Container)

{

Container cont = (Container)comp;

LinkedHashSet components = new LinkedHashSet();

for (int i = 0; i < cont.getComponentCount(); i++)

components.add(cont.getComponent(i));

Iterator it = components.iterator();

while (it.hasNext())

{

strBuffer.append(getComponentText(it.next()));

strBuffer.append("|");

}

}

return strBuffer;

}

public void run()

{

applet.init();

applet.start();

setVisible(true);

}

/**

* @param args

*/

public static void main(String[] args)

{

SomeApplet someApplet = new SomeApplet();

AppletWrapper aw = new AppletWrapper(someApplet);

aw.run();

System.out.println(aw.getContainerText(someApplet, new StringBuffer()));

}

}

And I have one question: how can I emulate "doClick()" method with Button (AWT) component?

Vitaliy.Vintonyaka at 2007-7-12 3:07:14 > top of Java-index,Java Essentials,Java Programming...
# 4

> how can I emulate "doClick()" method with Button (AWT) component?

Button has a getActionListeners() method. I suppose you could emulate a click by getting each ActionListener in the returned array and calling its actionPerfomed() method. I have no idea how you would construct the ActionEvent argument - the "source" will be the button itself and the "command" whatever the button's getActionCommand() returns, I suppose.

pbrockway2a at 2007-7-12 3:07:14 > top of Java-index,Java Essentials,Java Programming...