JEditorPane html Document#getText() not working
When I try to get the data of an html form from JEditorPane with getText()
I get only "\nName:\n " without the actual input data "Andre". Why not?
/*
* Html_Form_1.java
*/
import java.awt.*;
import java.awt.event.*;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
publicclass Html_Form_1extends JFrame{
public Html_Form_1(){
super("Html Form");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(700,300);
setLocationRelativeTo(null);
getDataBtn =new JButton("Get data");
getDataBtn.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
getData();
}
});
editorPane =new JEditorPane();
editorPane.setEditable(false);
editorPane.setContentType("text/html");
editorPane.setText(FORM_TEXT);
getContentPane().add(getDataBtn, BorderLayout.NORTH);
getContentPane().add(new JScrollPane(editorPane));
}
privatevoid getData(){
Document doc = editorPane.getDocument();
try{
String data = doc.getText(0, doc.getLength());
JOptionPane.showMessageDialog(this, data);
}catch (BadLocationException ex){
ex.printStackTrace();
}
}
publicstaticvoid main(String args[]){new Html_Form_1().setVisible(true);}
private JEditorPane editorPane;
private JButton getDataBtn;
privatefinalstatic String FORM_TEXT =
"<form>" +
"<table>"+
"<tr>"+
"<td>Name:</td>"+
"<td><input name='Name' type='text' value='Andre'></td>"+
"</table>"+
"</tr>"+
"</form>";
}
Here is some old code I have lying around. Most of it is comment out now so I'm not even sure if it will work. But the basic premise of the code is that the data for HTML components (inputBox, comboBox etc) is stored in a separate Object in the attributes part of the Element. So I guess somehow you will need to parse the Elements to find the inputBox and then parse each attribute to see which one contains the data value. Hope it helps:
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
class GetHeadings
{
public static void main(String[] args)
{
EditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
// The Document class does not yet handle charset's properly.
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
try
{
// Read and Parse the file
Reader rd = getReader(args[0]);
kit.read(rd, doc, 0);
// Iterate through the elements of the HTML document.
ElementIterator it = new ElementIterator(doc);
Element element;
while ( (element = it.next()) != null )
{
System.out.println(element);
if ( element.getName().equals( HTML.Tag.A.toString() ) )
{
Object o = element.getAttributes().getAttribute( HTML.Attribute.HREF );
System.out.println(o);
if ( o.toString().startsWith( "/2003" ) );
{
System.out.println( o );
}
}
/*
Enumeration enum = as.getAttributeNames();
while( enum.hasMoreElements() )
{
Object name = enum.nextElement();
Object value = as.getAttribute( name );
System.out.println( "\t" + name + " : " + value );
if (value instanceof DefaultComboBoxModel)
{
DefaultComboBoxModel model = (DefaultComboBoxModel)value;
for (int j = 0; j < model.getSize(); j++)
{
Object o = model.getElementAt(j);
Object selected = model.getSelectedItem();
if ( o.equals( selected ) )
System.out.println( o + " : selected" );
else
System.out.println( o );
}
}
}
//
if ( elem.getName().equals( HTML.Tag.SELECT.toString() ) )
{
Object o = as.getAttribute( HTML.Attribute.ID );
System.out.println( o );
}
// Wierd, the text for each tag is stored in a 'content' element
if (elem.getElementCount() == 0)
{
int start = elem.getStartOffset();
int end = elem.getEndOffset();
System.out.println( "\t" + doc.getText(start, end - start) );
}
*/
}
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
}
// Returns a reader on the HTML data. If 'uri' begins
// with "http:", it's treated as a URL; otherwise,
// it's assumed to be a local filename.
static Reader getReader(String uri)
throws IOException
{
// Retrieve from Internet.
if (uri.startsWith("http:"))
{
URLConnection conn = new URL(uri).openConnection();
return new InputStreamReader(conn.getInputStream());
}
// Retrieve from file.
else
{
return new FileReader(uri);
}
}
}