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