将输入从一个JTextArea传递到另一个JTextArea时遇到麻烦 [英] trouble passing input from one JTextArea into another JTextArea

查看:80
本文介绍了将输入从一个JTextArea传递到另一个JTextArea时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题出在我的DocumentLister AreaListener中.我似乎无法弄清楚如何将用户输入的文本传递到一个JTextAreas中进行转换,并将其返回给另一个JTextArea.

My issue lies in my DocumentLister AreaListener. I can't seem to figure out how to pass in the text the user enters into one of the JTextAreas for my conversions and return it to the other JTextArea.

该程序的目标是在一个字段中输入用户输入的罗马或阿拉伯数字,将其转换并实时将转换值返回到另一个字段.

The goal of the program is to take in a user entered Roman or Arabic Number in one of the fields, convert it and return the value of the conversion to the other field in real time.

我的GUI和转换方法有效,但似乎无法从用户那里获取字符串并实时打印.

My GUI and conversion methods work, I just can't seem to wrap my head around obtaining that string from the user and printing it in real time.

public class ArabicToRomanGUI_Hard extends JFrame
{
private static final long serialVersionUID = 1L;
 JPanel panel = new JPanel();


//constructor to add text fields to frame
public ArabicToRomanGUI_Hard()
{   
    JTextArea left = new JTextArea(10, 20);
    JTextArea right = new JTextArea(10, 20);
    setLayout(new GridLayout(1, 2));

    add(new JScrollPane(left));
    add(new JScrollPane(right));

    MirrorDocument leftDoc = new MirrorDocument();
    MirrorDocument rightDoc = new MirrorDocument();

    left.setDocument(leftDoc);
    right.setDocument(rightDoc);

    leftDoc.addDocumentListener(new DocumentHandler(rightDoc));
    rightDoc.addDocumentListener(new DocumentHandler(leftDoc));

    leftDoc.getDocument().addDocumentListener(AreaListener);
    rightDoc.getDocument().addDocumentListener(AreaListener);
   }



DocumentListener listener = new DocumentListener()

public class AreaListener implements DocumentListener
{
    //DocumentListener listener = new DocumentListener()

        @Override
        public void changedUpdate(DocumentEvent e) 
        {
            convertInput();
        }

        @Override
        public void insertUpdate(DocumentEvent e) 
        {
            convertInput();

        }

        @Override
        public void removeUpdate(DocumentEvent e) 
        {
            convertInput();

        }
        private void convertInput(DocumentEvent e)
        {
            boolean arabicEntered = false;
            boolean romanEntered = false; 
            for (char ch : userInputtedText.toCharArray())
            {
                if(Character.isLetter(ch))
                {
                    romanEntered = true;
                }
                if(Character.isDigit(ch))
                {
                    arabicEntered = true;
                }           
            }

            if(romanEntered = true)
            {
                if(ConversionLogic_Hard.getCheckFail() == false)
                {
                ConversionLogic_Hard.ConvertFromRomanToArabic(userInputtedText); //converts String of RomanNumerals to an arabic int
                String arabicNumberAsString = ConversionLogic_Hard.getConvertedRomanNumeral(); //converts number from int to string
                }
            }
            if(arabicEntered == true)
            {
                if(ConversionLogic_Hard.getCheckFail() == false)
                {
                ConversionLogic_Hard.ConvertFromArabicToRoman(userInputtedText); //converts String arabicNumberal to String roman numberal
                String romanNumberalAsString = ConversionLogic_Hard.getConvertedRomanNumeral(); //gets romanNumberal as String
                }
            }



        }//end convertInput


}//end AreaListener

//creates a flag to test the state of the TextArea
    public class MirrorDocument extends PlainDocument
    {
        private boolean ignoreUpdatedText;
        public void setIgnoreUpdates(boolean ignoreUpdatesText)
        {
            this.ignoreUpdatedText = ignoreUpdatesText;
        }
        public boolean isIgnoreUpdates()
        {
            return ignoreUpdatedText;
        }
    }

//when an event occurs checks the ignoreUpdatedText flag of the document to check if it is false.
//then sets the flag in the checkdocument to true to prevent the document listener from processing any new events.
//then updates the checkdocument.
public static class DocumentHandler implements DocumentListener
{
    private MirrorDocument checkDocument;
    private boolean ignoreUpdatedText = false;
    private JTextArea leftdoc, rightdoc;
    boolean arabicEntered = false;
    boolean romanEntered = false; 

    public DocumentHandler(MirrorDocument checkDocument)
    {
        this.checkDocument = checkDocument;
    }

    @Override
    public void removeUpdate(DocumentEvent e) 
    {
        Document doc = e.getDocument();
        if (doc instanceof MirrorDocument)
        {
            MirrorDocument mirrordoc = (MirrorDocument) doc;
            if (!mirrordoc.isIgnoreUpdates()) 
            {
                try 
                {
                    checkDocument.setIgnoreUpdates(true);
                    checkDocument.remove(e.getOffset(), e.getLength());
                } 
                catch (BadLocationException exc) 
                {
                    exc.printStackTrace();
                } 
                finally 
                {
                    checkDocument.setIgnoreUpdates(false);
                }
            }
        }   
    }//End removeUpdate

    @Override
    public void changedUpdate(DocumentEvent e) 
    {
        //NOT USED  
    }

    @Override
    public void insertUpdate(DocumentEvent e) 
    {
        Document doc = e.getDocument();
        if (doc instanceof MirrorDocument)
        {
            MirrorDocument mirrordoc = (MirrorDocument) doc;
            if( !mirrordoc.isIgnoreUpdates())
            {
                try
                {
                    String textInput = e.getDocument().getText(e.getOffset(), e.getLength());
                    checkDocument.setIgnoreUpdates(true);
                    checkDocument.insertString(e.getOffset(), textInput, null);
                }
                catch(BadLocationException exc)
                {
                    exc.printStackTrace();
                }
                finally
                {
                    checkDocument.setIgnoreUpdates(false);
                }
            }
        }
    }//End insertUpdate

    }
}//class

推荐答案

您可以使用DocumentFilter来监听更改.

You can use a DocumentFilter to listen for changes.

创建DocumentFilter时,可以指定要使用转换后的文本进行更新的文本字段.

When you create the DocumentFilter you can specify the text field to be updated with the converted text.

使用这种方法,您可以先在文本字段中删除DocumentFilter,然后再设置其文本,这样就避免了递归尝试更新的两个文本字段的递归.

Using this approach you can then remove the DocumentFilter from the text field before you set its text so you avoid the recursion of two text fields trying to update one another.

过滤器可能类似于:

import javax.swing.*;
import javax.swing.text.*;
import java.awt.Toolkit;

public class ConversionFilter extends DocumentFilter
{
    private boolean arabic;
    private JTextField converted;

    public ConversionFilter(boolean arabic, JTextField converted)
    {
        this.arabic = arabic;
        this.converted = converted;
    }

    @Override
    public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
        throws BadLocationException
    {
        super.insertString(fb, offs, str, a);

        convertInput(fb);
    }

    @Override
    public void replace(final FilterBypass fb, final int offs, final int length, final String str, final AttributeSet a)
        throws BadLocationException
    {
        super.replace(fb, offs, length, str, a);
        convertInput(fb);
    }

    @Override
    public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
        throws BadLocationException
    {
        super.remove(fb, offset, length);
        convertInput(fb);
    }

    private void convertInput(DocumentFilter.FilterBypass fb)
    {
        //  Remove the DocumentFilter from the text field to be converted

        AbstractDocument document = (AbstractDocument)converted.getDocument();
        DocumentFilter df = document.getDocumentFilter();
        document.setDocumentFilter( null );

        //  Do the conversion and update the text field

        String text = fb.getDocument().getText();
        String convertedText = arabic ? convertToRoman(text) : convertToArabic(text);
        converted.setText( convertedText );

        //  Restore the DocumentFilter on the converted text field

        document.setDocumentFilter( df );
    }
}

然后使用过滤器,代码可能类似于:

Then to use the filter the code might be something like:

JTextField arabicTextField = new JTextField(...);
JTextField romanTextField = new JTextField(...);

AbstractDocument arabicDocument = (AbstractDocument)arabicTextField.getDocument();
arabicDocument.setDocumentFilter( new ConversonFilter(true, romanTextField) );

AbstractDocument romanDocument = (AbstractDocument)romanTextField.getDocument();
romanDocument.setDocumentFilter( new ConversonFilter(false, arabicTextField) );

我之所以使用DocumentFilter而不是DocumentListener来接收有关对文档所做的更改的通知,这仅仅是因为DocumentFilter的getter/setter方法使删除和恢复过滤器变得容易.

I used a DocumentFilter instead of a DocumentListener to be notified of changes to the Document simply because of the getter/setter method for the DocumentFilter make is easy to remove and restore the filter.

这篇关于将输入从一个JTextArea传递到另一个JTextArea时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆