JTextPane 或 JTextField 中的文本更改(未更改)

Text changeD (not changing) in a JTextPane or JTextField

好的,我有一个非常具体的问题。我有一个带有 JTextField 和 JTextPane 的面板(实际上是 2 个 JTextPane,但我只有一个处于活动状态)并且需要一个事件来在用户编辑完文本后更新调用者表单中的相关变量。

JTextPane 是用 StyledDocument 设置的,因此,我不能每次按下一个键都更新它的变量,因此使用 DocumentListener 是不可行的。

并且使用 FocusListener 也不是解决方案,因为变量 (couple) 是使用 JList 组件选择的,并且它在更改 FocusLost 之前更改了引用。简而言之,其他组件干扰了变量实例。

我就是这样解决问题的。

首先我创建了一个 InputVerifier:

InputVerifier myInputVerifier = new InputVerifier() {
    @Override
    public boolean verify(JComponent input) {
        if ((input instanceof JTextField source) && source == tfKeywords) {
            PropertyChangeEvent evt = new PropertyChangeEvent(source, "Keywords", source.getText(), null);
            for (PropertyChangeListener l: pnlEditorFull.this.getPropertyChangeListeners())
                l.propertyChange(evt);
        } else if (input instanceof JTextPane source && source == ActualEditor) {
            PropertyChangeEvent evt = new PropertyChangeEvent(source, "Description", source.getText(), null);
            for (PropertyChangeListener l: pnlEditorFull.this.getPropertyChangeListeners())
                l.propertyChange(evt);
        }
        return true;
    }
};

当询问验证器时,它会触发事件 propertyChange 作为 JTextField 的“关键字”或 JTextArea 的“描述”。

在另一边我有一个 PropertyListener

pnlEditor.addPropertyChangeListener((PropertyChangeEvent evt) - > {
    if ("Keywords".equals(evt.getPropertyName()))
        myKeywords = EditorPane.getKeywords(); // actually a little more complicated
    else if ("Description".equals(evt.getPropertyName()))
        myDescription = EditorPane.getText(); // actually a little more complicated
});

为了检索文本,我在面板上使用了一个名为 getText():

的函数
public String getText() {
    StringWriter sw = new StringWriter();
    try {
        ActualEditor.write(sw);
    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
    }
    return sw.toString();
}

需要使用 ActualEditor.write(sw);,因为我需要检索文本和格式。

成功了。它在 FocusLost 之前和 JList 更改其选定项之前触发。当然,我可以只使用 JList ListSelectionListener 在选择更改时设置更改(我最初这样做),但这会留下不确定性 window,如果其他组件干扰,我很可能会失去所有更改或更改错误的变量。它需要在更改完成时调用(而不是在更改发生时),这是我唯一能想到的解决方案。

这里有一个简单的例子说明它应该如何工作(它还包括 JList):

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import javax.swing.DefaultListModel;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author luca.scarcia
 */
public class frmMain extends javax.swing.JFrame {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    static final Logger LOGGER  = System.getLogger(frmMain.class.getName());
    
    private JTextArea anEditor;
    private JList<String> aList;
    private DefaultListModel<String> Model;

    public frmMain() {
        
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout());

        aList = new JList<>();
        this.add(aList);
        Model = new DefaultListModel<>();
        Model.addElement("Element 1");
        Model.addElement("Element 2");
        Model.addElement("Element 3");
        Model.addElement("Element 4");
        
        aList.setModel(Model);
        
        anEditor = new JTextArea();
        this.add(anEditor);

        anEditor.setInputVerifier(new InputVerifier() {
            @Override
            public boolean verify(JComponent input) {
                if(input == anEditor) {
                    PropertyChangeEvent evt = new PropertyChangeEvent(anEditor, "Description", anEditor.getText(), null);
                    for(PropertyChangeListener l:anEditor.getPropertyChangeListeners()) {
                        l.propertyChange(evt);
                    }
                }
                return true;
            }
        });
        
        anEditor.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
            if(aList.getSelectedIndex()>=0)
                if("Description".equals(evt.getPropertyName()) && evt.getSource()==anEditor) {
                    StringWriter sw = new StringWriter();
                    try {
                        anEditor.write(sw);
                    } catch (IOException ex) {
                        LOGGER.log(Level.ERROR, ex);
                    }
                    Model.setElementAt(sw.toString(), aList.getSelectedIndex());
                    aList.validate();
                }
            }
        });
        
        
        aList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(e.getSource() == aList) {
                    StringReader sr = new StringReader((String)aList.getSelectedValue());
                    try {
                        anEditor.read(sr, null);
                    } catch (IOException ex) {
                        LOGGER.log(Level.ERROR, ex);
                    }
                }
            }
        });
        pack();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            LOGGER.log(Level.ERROR, ex);
        }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new frmMain().setVisible(true);
        });
    }
}

问题:有人有更好的解决方案吗?

And using FocusListener was not a solution either, because the variable (couple) was selected using a JList component and it changed the reference before FocusLost was changed

当文本区域获得焦点时,您可以保存选定的索引。

然后您还需要确保在选择侦听器完成调整之前不要重置文本:

import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author luca.scarcia
 */
public class frmMain2 extends javax.swing.JFrame {

    private JTextArea anEditor;
    private JList<String> aList;
    private DefaultListModel<String> Model;

    public frmMain2() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout());

        aList = new JList<>();
        this.add(aList);
        Model = new DefaultListModel<>();
        Model.addElement("Element 1");
        Model.addElement("Element 2");
        Model.addElement("Element 3");
        Model.addElement("Element 4");

        aList.setModel(Model);

        anEditor = new JTextArea(5, 30);
        this.add(anEditor);

        anEditor.addFocusListener( new FocusAdapter()
        {
            int selected;

            @Override
            public void focusGained(FocusEvent e)
            {
                selected = aList.getSelectedIndex();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                Model.setElementAt(anEditor.getText(), selected);
            }
        });

        aList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(!e.getValueIsAdjusting()) {
                    StringReader sr = new StringReader((String)aList.getSelectedValue());
                    try {
                        anEditor.read(sr, null);
                    } catch (IOException ex) {
                        System.out.println(ex);
                    }
                }
            }
        });

        pack();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
        }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new frmMain2().setVisible(true);
        });
    }
}