HTML-Formular in Swing-Benutzerinterface

Aus Byte-Welt Wiki
Version vom 2. Januar 2010, 21:43 Uhr von AndreUhres (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „<code=java> * FormularDemo.java * * Das HTML Formular ist eine interessante Alternative zu einem reinen Swing Formular. *: import java.beans.*; import …“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springenZur Suche springen

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