Freezing the first column of a JTable

Courtesy of Nobuo Tamemasa (http://www2.gol.com/users/tame/swing/examples/JTableExamples1.html)



RowHeaderRenderer.java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
 
/**
 * @version 1.0 11/09/98
 */
 
class RowHeaderRenderer extends JLabel implements ListCellRenderer {
  
  RowHeaderRenderer(JTable table) {
    JTableHeader header = table.getTableHeader();
    setOpaque(true);
    setBorder(UIManager.getBorder("TableHeader.cellBorder"));
    setHorizontalAlignment(CENTER);
    setForeground(header.getForeground());
    setBackground(header.getBackground());
    setFont(header.getFont());
  }
  
  public Component getListCellRendererComponent( JList list, 
         Object value, int index, boolean isSelected, boolean cellHasFocus) {
    setText((value == null) ? "" : value.toString());
    return this;
  }
}
 
public class RowHeaderExample extends JFrame {
 
  public RowHeaderExample() {
    super( "Row Header Example" );
    setSize( 300, 150 );
         
    ListModel lm = new AbstractListModel() {
      String headers[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i"};
      public int getSize() { return headers.length; }
      public Object getElementAt(int index) {
        return headers[index];
      }
    };
 
    DefaultTableModel dm = new DefaultTableModel(lm.getSize(),10);
    JTable table = new JTable( dm );
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    
    JList rowHeader = new JList(lm);    
    rowHeader.setFixedCellWidth(50);
    
    rowHeader.setFixedCellHeight(table.getRowHeight()
                               + table.getRowMargin());
//                             + table.getIntercellSpacing().height);
    rowHeader.setCellRenderer(new RowHeaderRenderer(table));
 
    JScrollPane scroll = new JScrollPane( table );
    scroll.setRowHeaderView(rowHeader);
    getContentPane().add(scroll, BorderLayout.CENTER);
  }
 
  public static void main(String[] args) {
    RowHeaderExample frame = new RowHeaderExample();
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
    frame.setVisible(true);
  }
}

Making a JSlider cell editor for my JTable


Note: in JDK1.4, you don’t need to do the setSize() and updateUI in the SliderRenderer.

Main.java:

import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
  
public class JSliderTableExample extends JFrame {
 
  public JSliderTableExample() {
    super( "JSliderTableExample Example" );
 
    DefaultTableModel dtm = new DefaultTableModel();
    dtm.setDataVector(new Object[][]{{ "JSlider1", new Integer(10) },
                                     { "JSlider2", new Integer(12) }},
                      new Object[]{ "String","JSlider"});
                     
    JTable table = new JTable(dtm);
    table.getColumn("JSlider").setCellRenderer(new SliderRenderer(JSlider.HORIZONTAL, 0, 20, 10));
    table.getColumn("JSlider").setCellEditor(new SliderEditor(JSlider.HORIZONTAL, 0, 20, 10));
 
    table.setRowHeight(20);
    JScrollPane scroll = new JScrollPane(table);
    getContentPane().add(scroll);
 
    setSize( 400, 100 );
    setVisible(true);
  }
 
  public static void main(String[] args) {
    JSliderTableExample frame = new JSliderTableExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }
}
  
class SliderRenderer extends JSlider implements TableCellRenderer
{
   public SliderRenderer(int orientation, int min, int max, int value) {
      super(orientation, min, max, value);
   }
 
   public Component getTableCellRendererComponent(JTable table, Object value,
                                  boolean isSelected, boolean hasFocus,
                                  int row, int column)
   {
      if (isSelected) {
         setForeground(table.getSelectionForeground());
         setBackground(table.getSelectionBackground());
      } else {
         setForeground(table.getForeground());
         setBackground(table.getBackground());
      }
 
      TableColumnModel columnModel = table.getColumnModel();      
      TableColumn selectedColumn = columnModel.getColumn(column);      
      int columnWidth = selectedColumn.getWidth();
      int columnHeight = table.getRowHeight();
      setSize(new Dimension(columnWidth, columnHeight));
 
      setValue(((Integer)value).intValue());
      updateUI();
      return this;
   }
}
 
class SliderEditor extends DefaultCellEditor {
   protected JSlider slider;
 
   public SliderEditor(int orientation, int min, int max, int value) {
      super(new JCheckBox());
      slider = new JSlider(orientation, min, max, value);      
      slider.setOpaque(true);      
   }
 
   public Component getTableCellEditorComponent(JTable table, Object value,
                                   boolean isSelected, int row, int column) {
      if (isSelected) {
         slider.setForeground(table.getSelectionForeground());
         slider.setBackground(table.getSelectionBackground());
      } else {
         slider.setForeground(table.getForeground());
         slider.setBackground(table.getBackground());
      }
      slider.setValue(((Integer) value).intValue());
 
      return slider;
   }
 
   public Object getCellEditorValue() {
      return new Integer(slider.getValue());
   }
 
   public boolean stopCellEditing() {
      return super.stopCellEditing();
   }
 
   protected void fireEditingStopped() {
      super.fireEditingStopped();
   }
}

Changing the icons of a JTree

Just use the methods setOpenIcon, setClosedIcon and setLeafIcon defined in the DefaultCellRenderer class.

Here’s an example.

Main.java:

import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
 
public class Main extends JFrame
{
   public Main() {
      DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
      DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
      root.add(child1);
      DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
      root.add(child2);
      JTree tree = new JTree(root);
 
      DefaultTreeCellRenderer cr = new DefaultTreeCellRenderer();
      cr.setOpenIcon(new ImageIcon("openicon.jpg"));
      cr.setClosedIcon(new ImageIcon("closedicon.jpg"));
      cr.setLeafIcon(new ImageIcon("leaficon.jpg"));
  
      tree.setCellRenderer(cr);
 
      getContentPane().add(new JScrollPane(tree));
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(400, 400);
      main.setVisible(true);
   }
}

The icons used in this program:


Listing all the default values of the look and feel

UIDefaults is a class derived from Hashtable that contains a map of the default values
for the installed UI look and feel (LAF). The following program produces a sorted list:

Main.java:

import javax.swing.*;
import java.util.*;
import java.awt.*;
 
public class Main {
   public static void main(String[] args) {
      UIDefaults defaults = UIManager.getDefaults();
 
      String[] ar = new String[defaults.size()];
      int count=0;
      Enumeration enum = defaults.keys();
      while (enum.hasMoreElements()) { 
         ar[count++] = (String) enum.nextElement();
      }
 
      // sort values
      Arrays.sort(ar);
 
      // print out
      for (int i=0; i<ar.length; i++) {
         System.out.println(ar[i]);
      } 
 
      // exit program (AWT event thread running)
      System.exit(1);
   }
}

outputs:

Button.background
Button.border
Button.disabledText
Button.focus
Button.font
Button.foreground
Button.margin
Button.select
Button.textIconGap
Button.textShiftOffset
ButtonUI
CheckBox.background
CheckBox.border
CheckBox.disabledText
CheckBox.focus
CheckBox.font
CheckBox.foreground
CheckBox.icon
CheckBox.margin
CheckBox.textIconGap
CheckBox.textShiftOffset
CheckBoxMenuItem.acceleratorFont
CheckBoxMenuItem.acceleratorForeground
CheckBoxMenuItem.acceleratorSelectionForeground
CheckBoxMenuItem.arrowIcon
CheckBoxMenuItem.background
CheckBoxMenuItem.border
CheckBoxMenuItem.borderPainted
CheckBoxMenuItem.checkIcon
CheckBoxMenuItem.disabledForeground
CheckBoxMenuItem.font
CheckBoxMenuItem.foreground
CheckBoxMenuItem.margin
CheckBoxMenuItem.selectionBackground
CheckBoxMenuItem.selectionForeground
CheckBoxMenuItemUI
CheckBoxUI
Checkbox.select
ColorChooser.background
ColorChooser.cancelText
ColorChooser.font
ColorChooser.foreground
ColorChooser.hsbBlueText
ColorChooser.hsbBrightnessText
ColorChooser.hsbGreenText
ColorChooser.hsbHueText
ColorChooser.hsbNameText
ColorChooser.hsbRedText
ColorChooser.hsbSaturationText
ColorChooser.okText
ColorChooser.previewText
ColorChooser.resetText
ColorChooser.rgbBlueMnemonic
ColorChooser.rgbBlueText
ColorChooser.rgbGreenMnemonic
ColorChooser.rgbGreenText
ColorChooser.rgbNameText
ColorChooser.rgbRedMnemonic
ColorChooser.rgbRedText
ColorChooser.swatchesDefaultRecentColor
ColorChooser.swatchesNameText
ColorChooser.swatchesRecentSwatchSize
ColorChooser.swatchesRecentText
ColorChooser.swatchesSwatchSize
ColorChooserUI
ComboBox.background
ComboBox.disabledBackground
ComboBox.disabledForeground
ComboBox.font
ComboBox.foreground
ComboBox.listBackground
ComboBox.listForeground
ComboBox.selectionBackground
ComboBox.selectionForeground
ComboBoxUI
Desktop.background
DesktopIcon.background
DesktopIcon.border
DesktopIcon.font
DesktopIcon.foreground
DesktopIconUI
DesktopPaneUI
EditorPane.background
EditorPane.border
EditorPane.caretBlinkRate
EditorPane.caretForeground
EditorPane.font
EditorPane.foreground
EditorPane.inactiveForeground
EditorPane.keyBindings
EditorPane.margin
EditorPane.selectionBackground
EditorPane.selectionForeground
EditorPaneUI
FileChooser.acceptAllFileFilterText
FileChooser.cancelButtonMnemonic
FileChooser.cancelButtonText
FileChooser.cancelButtonToolTipText
FileChooser.detailsViewButtonAccessibleName
FileChooser.detailsViewButtonToolTipText
FileChooser.detailsViewIcon
FileChooser.directoryDescriptionText
FileChooser.fileDescriptionText
FileChooser.fileNameLabelMnemonic
FileChooser.fileNameLabelText
FileChooser.filesOfTypeLabelMnemonic
FileChooser.filesOfTypeLabelText
FileChooser.helpButtonMnemonic
FileChooser.helpButtonText
FileChooser.helpButtonToolTipText
FileChooser.homeFolderAccessibleName
FileChooser.homeFolderIcon
FileChooser.homeFolderToolTipText
FileChooser.listViewButtonAccessibleName
FileChooser.listViewButtonToolTipText
FileChooser.listViewIcon
FileChooser.lookInLabelMnemonic
FileChooser.lookInLabelText
FileChooser.newFolderAccessibleNam
FileChooser.newFolderErrorSeparator
FileChooser.newFolderErrorText
FileChooser.newFolderIcon
FileChooser.newFolderToolTipText
FileChooser.openButtonMnemonic
FileChooser.openButtonText
FileChooser.openButtonToolTipText
FileChooser.saveButtonMnemonic
FileChooser.saveButtonText
FileChooser.saveButtonToolTipText
FileChooser.upFolderAccessibleName
FileChooser.upFolderIcon
FileChooser.upFolderToolTipText
FileChooser.updateButtonMnemonic
FileChooser.updateButtonText
FileChooser.updateButtonToolTipText
FileChooserUI
FileView.computerIcon
FileView.directoryIcon
FileView.fileIcon
FileView.floppyDriveIcon
FileView.hardDriveIcon
FocusManagerClassName
InternalFrame.activeTitleBackground
InternalFrame.activeTitleForeground
InternalFrame.border
InternalFrame.closeIcon
InternalFrame.font
InternalFrame.icon
InternalFrame.iconifyIcon
InternalFrame.iconizeIcon
InternalFrame.inactiveTitleBackground
InternalFrame.inactiveTitleForeground
InternalFrame.maximizeIcon
InternalFrame.minimizeIcon
InternalFrame.paletteBorder
InternalFrame.paletteCloseIcon
InternalFrame.paletteTitleHeight
InternalFrame.titleFont
InternalFrameUI
Label.background
Label.disabledForeground
Label.disabledShadow
Label.font
Label.foreground
LabelUI
List.background
List.cellRenderer
List.focusCellHighlightBorder
List.font
List.foreground
List.selectionBackground
List.selectionForeground
ListUI
Menu.acceleratorFont
Menu.acceleratorForeground
Menu.acceleratorSelectionForeground
Menu.arrowIcon
Menu.background
Menu.border
Menu.borderPainted
Menu.checkIcon
Menu.consumesTabs
Menu.disabledForeground
Menu.font
Menu.foreground
Menu.margin
Menu.selectionBackground
Menu.selectionForeground
MenuBar.background
MenuBar.border
MenuBar.font
MenuBar.foreground
MenuBarUI
MenuItem.acceleratorDelimiter
MenuItem.acceleratorFont
MenuItem.acceleratorForeground
MenuItem.acceleratorSelectionForeground
MenuItem.arrowIcon
MenuItem.background
MenuItem.border
MenuItem.borderPainted
MenuItem.checkIcon
MenuItem.disabledForeground
MenuItem.font
MenuItem.foreground
MenuItem.margin
MenuItem.selectionBackground
MenuItem.selectionForeground
MenuItemUI
MenuUI
OptionPane.background
OptionPane.border
OptionPane.buttonAreaBorder
OptionPane.cancelButtonText
OptionPane.errorIcon
OptionPane.font
OptionPane.foreground
OptionPane.informationIcon
OptionPane.messageAreaBorder
OptionPane.messageForeground
OptionPane.minimumSize
OptionPane.noButtonText
OptionPane.okButtonText
OptionPane.questionIcon
OptionPane.warningIcon
OptionPane.yesButtonText
OptionPaneUI
Panel.background
Panel.font
Panel.foreground
PanelUI
PasswordField.background
PasswordField.border
PasswordField.caretBlinkRate
PasswordField.caretForeground
PasswordField.font
PasswordField.foreground
PasswordField.inactiveForeground
PasswordField.keyBindings
PasswordField.margin
PasswordField.selectionBackground
PasswordField.selectionForeground
PasswordFieldUI
PopupMenu.background
PopupMenu.border
PopupMenu.font
PopupMenu.foreground
PopupMenuSeparatorUI
PopupMenuUI
ProgressBar.background
ProgressBar.backgroundHighlight
ProgressBar.border
ProgressBar.cellLength
ProgressBar.cellSpacing
ProgressBar.font
ProgressBar.foreground
ProgressBar.foregroundHighlight
ProgressBar.selectionBackground
ProgressBar.selectionForeground
ProgressBarUI
RadioButton.background
RadioButton.border
RadioButton.disabledText
RadioButton.focus
RadioButton.font
RadioButton.foreground
RadioButton.icon
RadioButton.margin
RadioButton.select
RadioButton.textIconGap
RadioButton.textShiftOffset
RadioButtonMenuItem.acceleratorFont
RadioButtonMenuItem.acceleratorForeground
RadioButtonMenuItem.acceleratorSelectionForeground
RadioButtonMenuItem.arrowIcon
RadioButtonMenuItem.background
RadioButtonMenuItem.border
RadioButtonMenuItem.borderPainted
RadioButtonMenuItem.checkIcon
RadioButtonMenuItem.disabledForeground
RadioButtonMenuItem.font
RadioButtonMenuItem.foreground
RadioButtonMenuItem.margin
RadioButtonMenuItem.selectionBackground
RadioButtonMenuItem.selectionForeground
RadioButtonMenuItemUI
RadioButtonUI
ScrollBar.background
ScrollBar.darkShadow
ScrollBar.foreground
ScrollBar.highlight
ScrollBar.maximumThumbSize
ScrollBar.minimumThumbSize
ScrollBar.shadow
ScrollBar.thumb
ScrollBar.thumbDarkShadow
ScrollBar.thumbHighlight
ScrollBar.thumbLightShadow
ScrollBar.thumbShadow
ScrollBar.track
ScrollBar.trackHighlight
ScrollBar.width
ScrollBarUI
ScrollPane.background
ScrollPane.border
ScrollPane.font
ScrollPane.foreground
ScrollPaneUI
Separator.background
Separator.foreground
Separator.highlight
Separator.shadow
SeparatorUI
Slider.background
Slider.focus
Slider.focusInsets
Slider.foreground
Slider.highlight
Slider.horizontalThumbIcon
Slider.majorTickLength
Slider.shadow
Slider.trackWidth
Slider.verticalThumbIcon
SliderUI
SplitPane.background
SplitPane.border
SplitPane.dividerSize
SplitPane.highlight
SplitPane.shadow
SplitPaneUI
StandardDialogUI
TabbedPane.background
TabbedPane.contentBorderInsets
TabbedPane.darkShadow
TabbedPane.focus
TabbedPane.font
TabbedPane.foreground
TabbedPane.highlight
TabbedPane.lightHighlight
TabbedPane.selectHighlight
TabbedPane.selected
TabbedPane.selectedTabPadInsets
TabbedPane.shadow
TabbedPane.tabAreaBackground
TabbedPane.tabAreaInsets
TabbedPane.tabInsets
TabbedPane.tabRunOverlay
TabbedPane.textIconGap
TabbedPaneUI
Table.background
Table.focusCellBackground
Table.focusCellForeground
Table.focusCellHighlightBorder
Table.font
Table.foreground
Table.gridColor
Table.scrollPaneBorder
Table.selectionBackground
Table.selectionForeground
TableHeader.background
TableHeader.cellBorder
TableHeader.font
TableHeader.foreground
TableHeaderUI
TableUI
TextArea.background
TextArea.border
TextArea.caretBlinkRate
TextArea.caretForeground
TextArea.font
TextArea.foreground
TextArea.inactiveForeground
TextArea.keyBindings
TextArea.margin
TextArea.selectionBackground
TextArea.selectionForeground
TextAreaUI
TextField.background
TextField.border
TextField.caretBlinkRate
TextField.caretForeground
TextField.font
TextField.foreground
TextField.inactiveForeground
TextField.keyBindings
TextField.margin
TextField.selectionBackground
TextField.selectionForeground
TextFieldUI
TextPane.background
TextPane.border
TextPane.caretBlinkRate
TextPane.caretForeground
TextPane.font
TextPane.foreground
TextPane.inactiveForeground
TextPane.keyBindings
TextPane.margin
TextPane.selectionBackground
TextPane.selectionForeground
TextPaneUI
TitledBorder.border
TitledBorder.font
TitledBorder.titleColor
ToggleButton.background
ToggleButton.border
ToggleButton.disabledBackground
ToggleButton.disabledSelectedBackground
ToggleButton.disabledSelectedText
ToggleButton.disabledText
ToggleButton.focus
ToggleButton.font
ToggleButton.foreground
ToggleButton.margin
ToggleButton.select
ToggleButton.text
ToggleButton.textIconGap
ToggleButton.textShiftOffset
ToggleButtonUI
ToolBar.background
ToolBar.border
ToolBar.dockingBackground
ToolBar.dockingForeground
ToolBar.floatingBackground
ToolBar.floatingForeground
ToolBar.font
ToolBar.foreground
ToolBar.separatorSize
ToolBarSeparatorUI
ToolBarUI
ToolTip.background
ToolTip.border
ToolTip.font
ToolTip.foreground
ToolTipUI
Tree.background
Tree.changeSelectionWithFocus
Tree.closedIcon
Tree.collapsedIcon
Tree.drawsFocusBorderAroundIcon
Tree.editorBorder
Tree.expandedIcon
Tree.font
Tree.foreground
Tree.hash
Tree.leafIcon
Tree.leftChildIndent
Tree.line
Tree.openIcon
Tree.rightChildIndent
Tree.rowHeight
Tree.scrollsOnExpand
Tree.selectionBackground
Tree.selectionBorderColor
Tree.selectionForeground
Tree.textBackground
Tree.textForeground
TreeUI
Viewport.background
Viewport.font
Viewport.foreground
ViewportUI
activeCaption
activeCaptionBorder
activeCaptionText
control
controlDkShadow
controlHighlight
controlLtHighlight
controlShadow
controlText
desktop
inactiveCaption
inactiveCaptionBorder
inactiveCaptionText
info
infoText
menu
menuText
scrollbar
text
textHighlight
textHighlightText
textInactiveText
textText
window
windowBorder
windowText

Disabling selection in my JTable

Look at the three lines of code in this working program to find out how to disallow any selection.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame 
{
   public Main() {
      super("Table example, Wines from Bordeaux");
 
      Object[][] tabledata = {
            { "Chateau Meyney, St. Estephe", 	   new Integer(1994), "$18.75"},
            { "Chateau Montrose, St. Estephe", 	   new Integer(1975), "$54.25" },
            { "Chateau Gloria, St. Julien", 	   new Integer(1993), "$22.99" },
            { "Chateau Beychevelle, St. Julien",   new Integer(1970), "$61.63" },
            { "Chateau La Tour de Mons, Margeaux", new Integer(1975), "$57.03" },
            { "Chateau Brane-Cantenac, Margeaux",  new Integer(1978), "$49.92" },
      };
 
      String columnheaders[] = { "Wine", "Vintage", "Price" };
 
      JTable table = new JTable(tabledata, columnheaders);
      table.setPreferredScrollableViewportSize(new Dimension(500, 70));
      JScrollPane scrollPane = new JScrollPane(table);
 
      getContentPane().add(scrollPane);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
 
      // disable selection
      table.setRowSelectionAllowed(false);
      table.setColumnSelectionAllowed(false);
      table.setCellSelectionEnabled(false);
 
      pack();
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.show();
   }
}

Catching an event when a form SUBMIT button has been clicked on a JTextPane

Set a custom HTMLEditorKit on your JTextPane. In the example below, this is MyHTMLEditorKit. It contains a factory that is responsible for returning a custom FormView. In MyFormView, you can override the method submitData that is called when the submit button is pressed. The form data is passed to this method in a escaped form.

Main.java:

import javax.swing.text.html.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.io.*;
 
public class Main extends JFrame
{
   final JTextField urlTextField = new JTextField();
   final JEditorPane htmlPane = new JEditorPane();
   JButton urlButton = new JButton("Go!");
 
   public Main() {
      htmlPane.setEditable(false);
      urlTextField.setText("file:c:\test.html");
 
      urlButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            loadPage(urlTextField.getText());
         }
      });
 
      htmlPane.setEditorKit(new MyHTMLEditorKit());
 
      getContentPane().setLayout(new BorderLayout());
      JPanel topPanel = new JPanel(new BorderLayout());
      topPanel.add(BorderLayout.CENTER, urlTextField);
      topPanel.add(BorderLayout.EAST, urlButton);
 
      getContentPane().add(BorderLayout.NORTH, topPanel);
      getContentPane().add(BorderLayout.CENTER, new JScrollPane(htmlPane));
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
 
      setSize(400, 400);
   }
 
   public void loadPage(String url) {
      try {
         htmlPane.setPage(new URL(urlTextField.getText()));
      }
      catch(Exception e) {
         System.out.println(e);
      }
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setVisible(true);
   }
}

MyHTMLEditorKit.java:

import javax.swing.text.html.*;
import javax.swing.text.*;
 
public class MyHTMLEditorKit extends HTMLEditorKit {
   private static final ViewFactory customFactory = new MyHTMLFactory();
 
   public ViewFactory getViewFactory() {
      return customFactory;
   }
 
   public static class MyHTMLFactory extends HTMLEditorKit.HTMLFactory
   {
      public View create(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 || kind == HTML.Tag.SELECT || kind == HTML.Tag.TEXTAREA) {
               return new MyFormView(elem);
            }
            else return super.create(elem);
         }
 
         return super.create(elem);
      }
   }
}

MyFormView.java:

import javax.swing.text.html.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.util.*;
 
public class MyFormView extends FormView
{
   public MyFormView(Element elem) {
      super(elem);
   }
 
   public void actionPerformed(ActionEvent event) {
      super.actionPerformed(event);
   }
 
   public void submitData(String data) {
      System.out.println("Escaped data: " + data);
   }
}

A simple HTML file to test it with: test.html:

<html>
<body>
 
<form METHOD=POST ACTION="performyourownaction">
 
   <i>Subcategory:</i><br>
   <SELECT NAME="subcat">
      <OPTION> J2SE </OPTION>
      <OPTION> J2EE </OPTION>
      <OPTION> J2ME </OPTION>
      <OPTION> Other </OPTION>
   </SELECT>
   <p>
   <i>Subject:</i><br>
      <input type=text size=80 maxsize=200 name="subject"><p>
   <i>Question:</i><br>
      <textarea name="question" cols=60 rows=4></textarea><p>
 
   <br><br>
 
   <input TYPE=SUBMIT VALUE=" Submit Question! ">
</form>
 
</body>
</html>