HTML-Formular in Swing-Benutzerinterface: Unterschied zwischen den Versionen

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen
K (hat HTMLFormular nach HTML-Formular verschoben)
(kein Unterschied)

Version vom 4. April 2010, 09:07 Uhr

Das HTML Formular ist eine interessante Alternative zu einem reinen Swing Formular.

Die Klasse HTMLFormular musst du nicht bis in alle Einzelheiten verstehen. Du kannst sie unverändert übernehmen. Ihr Zweck ist lediglich, die Klasse JEditorPane so zu erweitern, dass sie ein Html Script verstehen und verarbeiten kann.

In der Klasse FormularDemo wird das HTMLFormular über einen PropertyChangeListener überwacht. Die Methode propertyChange wird aufgerufen, wenn der "submit" Button im Formular betätigt wird.

Zu der eigentlichen Html Sprache findest du sicher mehrere Tutorials im Netz. Zum Beispiel hier: http://www.htmlcodetutorial.com/ <code=java> /*

* FormularDemo.java
*
* Das HTML Formular ist eine interessante Alternative zu einem reinen Swing Formular.
*
*/

import java.beans.*; import java.util.*; import javax.swing.*;

public class FormularDemo extends JFrame {

   private final static String VORNAME = "Vorname";
   private final static String NACHNAME = "Nachname";
   private final static String GESCHLECHT = "Geschlecht";
   private final static String BEMERKUNG = "Bemerkung";
   private final static String M = "Männlich";
   private final static String W = "Weiblich";
   private HTMLFormular formular;
   private String vorname = "André";
   private String nachname = "Uhres";
   private String geschlecht = M;
   private String bemerkung = "Ich spreche deutsch, französisch und englisch";
   public FormularDemo() {
       super("FormularDemo");
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       setSize(700, 300);
       setLocationRelativeTo(null);
       formular = new HTMLFormular();
       formular.setText(getForm());
       formular.addPropertyChangeListener("data", new PropertyChangeListener() {
           public void propertyChange(PropertyChangeEvent evt) {
               showData();
           }
       });
       getContentPane().add(new JScrollPane(formular));
   }
   private void showData() {
       Map<String, String> result = formular.getResult();
       vorname = result.get(VORNAME);
       nachname = result.get(NACHNAME);
       geschlecht = result.get(GESCHLECHT);
       bemerkung = result.get(BEMERKUNG);
       JOptionPane.showMessageDialog(null,
               VORNAME + ": " + vorname + "\n"
               + NACHNAME + ": " + nachname + "\n"
               + GESCHLECHT + ": " + geschlecht + "\n"
               + BEMERKUNG + ": " + bemerkung);
   }
   private String getForm() {
       return "<form>"

+ "

" + "" + " " + " " + "" + "" + " " + " " + "" + "" + " " + " " + "" + "" + " " + " " + "" + "" + " " + " " + "" + "" + " " + " " + "" + "
" + VORNAME + ":<input name='" + VORNAME + "' type='text' value='" + vorname + "'>
" + NACHNAME + ":<input name='" + NACHNAME + "' type='text' value='" + nachname + "'>
" + GESCHLECHT + ":<select name='" + GESCHLECHT + "'> <option "
               + (geschlecht.equals(M) ? "selected" : "") + ">" + M + "<option "
+ (geschlecht.equals(W) ? "selected" : "") + ">" + W + "</select>
" + BEMERKUNG + ":<textarea name='" + BEMERKUNG + "' rows=5 cols=50 >" + bemerkung + "</textarea>

<input type='submit'>

"

               + "</form>";
   }
   public static void main(final String args[]) {
       Runnable gui = new Runnable() {
           public void run() {
               new FormularDemo().setVisible(true);
           }
       };
       //GUI must start on EventDispatchThread:
       SwingUtilities.invokeLater(gui);
   }

} </code=java> <code=java> /*

* HTMLFormular.java
*/

import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; import javax.swing.text.*; import javax.swing.text.html.*;

public class HTMLFormular extends JEditorPane {

   private Map<String, String> resultMap;
   public HTMLFormular() {
       setEditable(false);
       setEditorKit(new HTMLEditorKit() {
           @Override
           public ViewFactory getViewFactory() {
               return new HTMLEditorKit.HTMLFactory() {
                   @Override
                   public View create(final Element elem) {
                       Object o = elem.getAttributes().getAttribute(StyleConstants.NameAttribute);
                       if (o instanceof HTML.Tag) {
                           HTML.Tag kind = (HTML.Tag) o;
                           if (kind == HTML.Tag.INPUT) {
                               return new FormView(elem) {
                                   @Override
                                   protected void submitData(final String data) {
                                       setData(data);
                                   }
                               };
                           }
                       }
                       return super.create(elem);
                   }
               };
           }
       });
   }
   private void setData(final String data) {
       resultMap = new HashMap<String, String>();
       StringTokenizer st = new StringTokenizer(data, "&");
       while (st.hasMoreTokens()) {
           String token = st.nextToken();
           String key = null;
           String value = null;
           try {
               key = URLDecoder.decode(token.substring(0, token.indexOf("=")), "UTF-8");
               value = URLDecoder.decode(token.substring(token.indexOf("=") + 1, token.length()), "UTF-8");
           } catch (UnsupportedEncodingException ex) {
               ex.printStackTrace();
           }
           resultMap.put(key, value);
       }
       firePropertyChange("data", null, data);
   }
   public Map<String, String> getResult() {
       return resultMap;
   }

} </code=java>