获取每个JTextArea的文本

时间:2015-12-12 12:11:29

标签: java swing jtextarea

我的代码是这样的:

public main() {
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(700, 500);

    //tabbed pane
    add(tb);
}

public JTextArea txtArea() {
    JTextArea area = new JTextArea();
    String st = String.valueOf(tab);
    area.setName(st);

    return area;
}

public static void main (String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new main();
        }
    });
}

@Override
public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    if(source==mnew) {
        tab++;

        tb.add("Untitled-"+tab,new JPanel().add(txtArea()));

        int s = tb.getSelectedIndex();
        s = tb.getTabCount()-1;
        tb.setSelectedIndex(s);
    }

    if(source==save) {
        int s = tb.getSelectedIndex()+1;
    }

每次点击"新"菜单项,代码使用新面板和textarea创建新选项卡(它类似于许多文本编辑器,如记事本++)。 点击"保存"在菜单栏中我想从焦点jtextarea获取文本。 请帮忙。

1 个答案:

答案 0 :(得分:1)

将文档侦听器添加到文本区域。

public JTextArea txtArea() {
    JTextArea area = new JTextArea();
    tstDocumentListener dcL = new tstDocumentListener();
    area.getDocument().addDocumentListener(dcL);
    String st = String.valueOf(tab);
    area.setName(st);

    return area;
}

<强> tstDocumentListener

public class tstDocumentListener implements DocumentListener
{
    public void changedUpdate(DocumentEvent e) {}       
    public void removeUpdate(DocumentEvent e)
    {
        String newString = "";
        int lengthMe = e.getDocument().getLength();
        try
        {
            newString = e.getDocument().getText(0,lengthMe);

            System.out.println(newString);
        }

        catch(Exception exp)
        {
            System.out.println("Error");
        }
    }
    public void insertUpdate(DocumentEvent e) 
    {
        String newString = "";
        int lengthMe = e.getDocument().getLength();
        try
        {
            newString = e.getDocument().getText(0,lengthMe);

            System.out.println(newString);
        }

        catch(Exception exp)
        {
            System.out.println("Error");
        }
    }
}

修改

关于在文本区域获得或失去焦点时获取文本

public JTextArea txtArea() {
    JTextArea area = new JTextArea();
    CustomFocusListener cFL = new CustomFocusListener();
    area.addFocusListener(cFL);
    String st = String.valueOf(tab);
    area.setName(st);

     return area;
}

<强> CustomFocusListener

public class CustomFocusListener implements FocusListener
{
    @Override
    public void focusGained(FocusEvent e)
    {
        String parseMe = "";
        JTextArea src;
        try 
        {
            src = (JTextArea)e.getSource();
            parseMe = src.getText();
            System.out.println(parseMe);
        }
        catch (ClassCastException ignored)
        {

        }
    }

    @Override
    public void focusLost(FocusEvent e)
    {
        String parseMe = "";
        JTextArea src;
        try 
        {
            src = (JTextArea)e.getSource();
            parseMe = src.getText();
            System.out.println(parseMe);
        }
        catch (ClassCastException ignored)
        {

        }
    }
}