Java Tutorial/Swing Event/Document
Содержание
Custom Document Filter
<source lang="java">
import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.DocumentFilter; class IntegerRangeDocumentFilter extends DocumentFilter {
public IntegerRangeDocumentFilter() { } public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { System.out.println("insert string"+ string); System.out.println(offset); super.insertString(fb, offset, string, attr); } public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException { System.out.println("remove"); super.remove(fb, offset, length); } public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { System.out.println("replace"); super.replace(fb, offset, length, text, attrs); }
} public class RangeSample {
public static void main(String args[]) { JFrame frame = new JFrame("Range Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textFieldOne = new JTextField(); Document textDocOne = textFieldOne.getDocument(); DocumentFilter filterOne = new IntegerRangeDocumentFilter(); ((AbstractDocument) textDocOne).setDocumentFilter(filterOne); frame.add(textFieldOne); frame.setSize(250, 150); frame.setVisible(true); }
}</source>
Element Walker
<source lang="java">
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.text.Document; import javax.swing.text.Element; import javax.swing.text.ElementIterator; public class ElementSample {
public static void main(String args[]) { JFrame frame = new JFrame("Element Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); JButton button = new JButton("Show Elements"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Document document = textArea.getDocument(); ElementIterator iterator = new ElementIterator(document); Element element = iterator.first(); while (element != null) { System.out.println(element.getStartOffset()); element = iterator.next(); } } }; button.addActionListener(actionListener); frame.add(scrollPane, BorderLayout.CENTER); frame.add(button, BorderLayout.SOUTH); frame.setSize(250, 250); frame.setVisible(true); }
}</source>
extends PlainDocument to create a float value type text field
<source lang="java">
import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; class JTextFieldFilter extends PlainDocument {
public static final String FLOAT = "0123456789."; protected String acceptedChars = null; protected boolean negativeAccepted = false; public JTextFieldFilter() { this(FLOAT); } public JTextFieldFilter(String acceptedchars) { acceptedChars = acceptedchars; } public void setNegativeAccepted(boolean negativeaccepted) { if (acceptedChars.equals(FLOAT)) { negativeAccepted = negativeaccepted; acceptedChars += "-"; } } public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; for (int i = 0; i < str.length(); i++) { if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1) return; } if (acceptedChars.equals(FLOAT) || (acceptedChars.equals(FLOAT + "-") && negativeAccepted)) { if (str.indexOf(".") != -1) { if (getText(0, getLength()).indexOf(".") != -1) { return; } } } if (negativeAccepted && str.indexOf("-") != -1) { if (str.indexOf("-") != 0 || offset != 0) { return; } } super.insertString(offset, str, attr); }
} public class Main extends JFrame {
public static void main(String[] argv) throws Exception { new Main(); } public Main() { JTextField tf1b; JLabel l1b; setLayout(new FlowLayout()); l1b = new JLabel("only float"); tf1b = new JTextField(10); getContentPane().add(l1b); getContentPane().add(tf1b); tf1b.setDocument(new JTextFieldFilter(JTextFieldFilter.FLOAT)); setSize(300, 300); setVisible(true); }
}</source>
extends PlainDocument to create alpha and numeric value based field field
<source lang="java">
import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; class JTextFieldFilter extends PlainDocument {
public static final String ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static final String NUMERIC = "0123456789"; public static final String ALPHA_NUMERIC = ALPHA + NUMERIC; protected String acceptedChars = null; protected boolean negativeAccepted = false; public JTextFieldFilter() { this(ALPHA_NUMERIC); } public JTextFieldFilter(String acceptedchars) { acceptedChars = acceptedchars; } public void setNegativeAccepted(boolean negativeaccepted) { if (acceptedChars.equals(ALPHA_NUMERIC)) { negativeAccepted = negativeaccepted; acceptedChars += "-"; } } public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; for (int i = 0; i < str.length(); i++) { if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1) return; } if (negativeAccepted) { if (str.indexOf(".") != -1) { if (getText(0, getLength()).indexOf(".") != -1) { return; } } } if (negativeAccepted && str.indexOf("-") != -1) { if (str.indexOf("-") != 0 || offset != 0) { return; } } super.insertString(offset, str, attr); }
} public class Main extends JFrame{
public static void main(String[] argv) throws Exception { new Main(); } public Main() { JTextField tf1c; JLabel l1c; setLayout(new FlowLayout()); l1c = new JLabel("only float(can be negative)"); tf1c = new JTextField(10); getContentPane().add(l1c); getContentPane().add(tf1c); JTextFieldFilter jtff = new JTextFieldFilter(JTextFieldFilter.ALPHA_NUMERIC); jtff.setNegativeAccepted(true); tf1c.setDocument(jtff); setSize(300,300); setVisible(true); }
}</source>
Implement a document cleaner that maps lowercase letters to uppercase
<source lang="java">
import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DocumentFilter;
public class Main{
public static void main(String[] args) { DocumentFilter dfilter = new UpperCaseFilter(); JTextArea jta = new JTextArea(); ((AbstractDocument) jta.getDocument()).setDocumentFilter(dfilter); JFrame frame = new JFrame("UpcaseFilter"); frame.getContentPane().add(jta, java.awt.BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 120); frame.setVisible(true); }
} class UpperCaseFilter extends DocumentFilter {
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException { fb.insertString(offset, text.toUpperCase(), attr); } public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException { fb.replace(offset, length, text.toUpperCase(), attr); }
}</source>
Limit JTextField input to a maximum length
<source lang="java">
import javax.swing.JTextField; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; class JTextFieldLimit extends PlainDocument {
private int limit; JTextFieldLimit(int limit) { super(); this.limit = limit; } public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; if ((getLength() + str.length()) <= limit) { super.insertString(offset, str, attr); } }
} public class Main {
public static void main(String[] argv) { JTextField textfield1 = new JTextField(15); textfield1.setDocument(new JTextFieldLimit(10)); }
}</source>