Example usage for javax.swing.text Document addDocumentListener

List of usage examples for javax.swing.text Document addDocumentListener

Introduction

In this page you can find the example usage for javax.swing.text Document addDocumentListener.

Prototype

public void addDocumentListener(DocumentListener listener);

Source Link

Document

Registers the given observer to begin receiving notifications when changes are made to the document.

Usage

From source file:ListenerSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Offset Example");
    Container content = frame.getContentPane();
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    final Document document = textArea.getDocument();
    document.addDocumentListener(new MyListener());

    content.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(250, 150);//from w  w w . jav  a  2  s  . co  m
    frame.setVisible(true);
}

From source file:Main.java

public static void addTextUpdateListener(JTextComponent textComponent, Consumer<DocumentEvent> listener) {
    Document doc = textComponent.getDocument();
    doc.addDocumentListener(new DocumentListener() {
        @Override//from w ww.java 2s . c  om
        public void removeUpdate(DocumentEvent e) {
            listener.accept(e);
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            listener.accept(e);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            listener.accept(e);
        }
    });
}

From source file:Main.java

public Main() {
    JButton button = new JButton("foo");
    JTextField textField = new JTextField(10);
    Document document = textField.getDocument();
    document.addDocumentListener(new JButtonStateController(button));

    JOptionPane.showMessageDialog(null, textField);

}

From source file:Main.java

public Main() {
    JButton button = new JButton("foo");
    JTextField textField = new JTextField(10);
    Document document = textField.getDocument();
    document.addDocumentListener(new JButtonStateController(button));

    JFrame frame = new JFrame();
    frame.add(button, BorderLayout.WEST);
    frame.add(textField, BorderLayout.CENTER);
    frame.setSize(300, 300);//ww  w .j a  v a 2  s.co m
    frame.setVisible(true);
}

From source file:de.unentscheidbar.validation.swing.trigger.DocumentChangeTrigger.java

private void observeDocument(JComponent component) {

    Document doc = DocumentGetter.INSTANCE.apply(component);
    Wrapper<Document> docIdentity = Beans.identity(doc);
    Wrapper<JComponent> componentIdentity = Beans.identity(component);

    /* Don't listen more than once to each document */
    documentMap.remove(docIdentity, componentIdentity);
    if (!documentMap.containsKey(docIdentity)) {
        doc.removeDocumentListener(this);
        doc.addDocumentListener(this);
    }//w  w w  .j  av  a 2s  .  c o m
    documentMap.put(docIdentity, componentIdentity);
}

From source file:jeplus.gui.EPlusEditorPanel.java

/**
 * Create text panel with the specified title, text, and mode
 *
 * @param container Reference to the container of this panel
 * @param title Title for this panel, to appear in the title field of a
 * frame, or title of a tab/*from w  w  w .  j  ava  2s. c o m*/
 * @param filename The name of the file to be opened
 * @param type The predefined file type
 * @param project
 */
public EPlusEditorPanel(Container container, String title, String filename, FileType type,
        JEPlusProject project) {
    initComponents();
    initRSTA(type.getRSTA_Style());
    FC.setFileFilter(type.getFileFilter());
    FC.setMultiSelectionEnabled(false);
    this.ContainerComponent = container;
    this.Title = title;
    this.CurrentFileName = filename;
    if (CurrentFileName != null) {
        this.rsTextArea.setText(getFileContent(CurrentFileName));
    }
    this.ContentType = type;
    this.ContentChanged = false;
    this.Project = project;
    switch (ContentType) {
    case IDF:
        updateSearchStrings((Project == null) ? null : Project.getSearchStrings());
        this.cmdLoad.setEnabled(true);
        this.cmdCheck.setEnabled(false);
        this.cmdSave.setEnabled(true);
        break;
    case EPW:
    case RVI:
        updateSearchStrings(null);
        this.cmdLoad.setEnabled(false);
        this.cmdCheck.setEnabled(false);
        this.cmdSave.setEnabled(true);
        break;
    case RVX:
        updateSearchStrings(RVX.quickIndex());
        this.cmdLoad.setEnabled(false);
        this.cmdCheck.setEnabled(true);
        this.cmdSave.setEnabled(true);
        break;
    case JSON:
        updateSearchStrings(null);
        this.cmdLoad.setEnabled(false);
        this.cmdCheck.setEnabled(true);
        this.cmdSave.setEnabled(false);
        break;
    case PYTHON:
    case CSV:
    case XML:
    case PLAIN:
    default:
        updateSearchStrings(null);
        this.cmdLoad.setEnabled(true);
        this.cmdCheck.setEnabled(false);
        this.cmdSave.setEnabled(true);
    }
    // this.cboSearchStrings.setEditable(true);

    this.Document = rsTextArea.getDocument();
    Document.addDocumentListener(this);
}

From source file:org.docx4all.ui.main.WordMLEditor.java

private JEditorPane createSourceView(WordMLTextPane editorView) {
    //Create the Source View
    JEditorPane sourceView = new JEditorPane();

    MutableAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setFontFamily(attrs, FontManager.getInstance().getSourceViewFontFamilyName());
    StyleConstants.setFontSize(attrs, FontManager.getInstance().getSourceViewFontSize());

    // TODO - only do this if the font is available.
    Font font = new Font("Arial Unicode MS", Font.PLAIN, 12);

    System.out.println(font.getFamily());
    System.out.println(font.getFontName());
    System.out.println(font.getPSName());

    sourceView.setFont(font);//ww w.ja va 2  s  .  c o  m
    //sourceView.setFont(FontManager.getInstance().getFontInAction(attrs));

    sourceView.setContentType("text/xml; charset=UTF-16");

    // Instantiate a XMLEditorKit with wrapping enabled.
    XMLEditorKit kit = new XMLEditorKit(true);
    // Set the wrapping style.
    kit.setWrapStyleWord(true);

    sourceView.setEditorKit(kit);

    WordMLDocument editorViewDoc = (WordMLDocument) editorView.getDocument();

    try {
        editorViewDoc.readLock();

        editorView.getWordMLEditorKit().saveCaretText();

        DocumentElement elem = (DocumentElement) editorViewDoc.getDefaultRootElement();
        WordprocessingMLPackage wmlPackage = ((DocumentML) elem.getElementML()).getWordprocessingMLPackage();
        String filePath = (String) editorView.getDocument().getProperty(WordMLDocument.FILE_PATH_PROPERTY);

        //Do not include the last paragraph which is an extra paragraph.
        elem = (DocumentElement) elem.getElement(elem.getElementCount() - 1);
        ElementML paraML = elem.getElementML();
        ElementML bodyML = paraML.getParent();
        paraML.delete();

        Document doc = DocUtil.read(sourceView, wmlPackage);
        doc.putProperty(WordMLDocument.FILE_PATH_PROPERTY, filePath);
        doc.putProperty(WordMLDocument.WML_PACKAGE_PROPERTY, wmlPackage);
        doc.addDocumentListener(getToolbarStates());

        //Below are the properties used by bounce.jar library
        //See http://www.edankert.com/bounce/xmleditorkit.html
        doc.putProperty(PlainDocument.tabSizeAttribute, new Integer(4));
        doc.putProperty(XMLDocument.AUTO_INDENTATION_ATTRIBUTE, Boolean.TRUE);
        doc.putProperty(XMLDocument.TAG_COMPLETION_ATTRIBUTE, Boolean.TRUE);

        //Remember to put 'paraML' as last paragraph
        bodyML.addChild(paraML);

    } finally {
        editorViewDoc.readUnlock();
    }

    kit.setStyle(XMLStyleConstants.ATTRIBUTE_NAME, new Color(255, 0, 0), Font.PLAIN);

    sourceView.addFocusListener(getToolbarStates());
    //sourceView.setDocument(doc);
    sourceView.putClientProperty(Constants.LOCAL_VIEWS_SYNCHRONIZED_FLAG, Boolean.TRUE);

    return sourceView;
}

From source file:org.nuclos.client.ui.collect.component.CollectableComboBox.java

public void addDocumentListenerForEditor() {
    final Document document = getEditorDocument();
    if (document != null) {
        document.addDocumentListener(getDocumentListener());
    }//from w ww  . ja v a2  s  . c  o m
}

From source file:org.onebusaway.phone.client.SimplePhoneClient.java

private static void setupGui(AgiClientScriptImpl script) {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    KeyPressHandler handler = new KeyPressHandler(script);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.addKeyListener(handler);//from www  .java2 s.c  o  m

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(4, 3));
    panel.add(buttonPanel, BorderLayout.CENTER);

    String buttons = "123456789*0#";

    for (int i = 0; i < buttons.length(); i++)
        addButton(buttonPanel, script, handler, buttons.charAt(i));

    Document document = script.getDocument();

    final JTextArea textArea = new JTextArea(document);
    textArea.setEditable(false);
    textArea.addKeyListener(handler);
    document.addDocumentListener(new ScrollDocumentToEnd(textArea));

    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new Dimension(300, 100));
    scrollPane.addKeyListener(handler);
    panel.add(scrollPane, BorderLayout.SOUTH);

    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:org.paxle.desktop.impl.event.MultipleChangesListener.java

public void addComp2Monitor(final Object comp) {
    if (comp instanceof JTextComponent) {
        final JTextComponent tc = (JTextComponent) comp;
        final Document doc = tc.getDocument();
        initialValues.put(doc, new CompEntry(tc.getText()));
        doc.addDocumentListener(this);
    } else {// w  w  w .j a va 2s.  c  o  m
        final Class<?> clazz = comp.getClass();
        final int eidx = getEntryIndex(clazz);
        if (eidx < 0)
            throw new RuntimeException("component '" + clazz.getName() + "' not supported for monitoring");
        try {
            // save the initial value of the component for comparison purposes on state change
            initialValues.put(comp, new CompEntry(clazz.getMethod(GET_VALUES[eidx]).invoke(comp)));

            // register this as event listener for the component
            final Method method = clazz.getMethod("add" + ADD_LISTENERS[eidx].getSimpleName(),
                    ADD_LISTENERS[eidx]);
            method.invoke(comp, this);
            logger.debug("added " + ADD_LISTENERS[eidx].getSimpleName() + " for " + comp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}