How to detect changes in text in many JTextPane

75 Views Asked by At

I would like to create simply text editor with dynamic amount of tabs. Every tab consist of text field, where someone can load text file and edit or write own text.

I would like to detect changes in tabs, I mean when someone change file and wants close then I'd like to show a dialog about whether you want save changes or no. this is reason why I'd like to follow changes which user committed.

So I have

JTabbedPane tabbedPane with JtextPane textPane

private LinkedList<Boolean> changedList = new LinkedList<Boolean>(); // here I thought of collecting information about changes but it was silly idea. 

This is function which I create new Tab

 public void newTab()
    {
        tabbedPane.addTab("tab-" + counter++, new JTextPane());
        int totalTabs = tabbedPane.getTabCount();
      
        selected = tabbedPane.getComponentAt(totalTabs-1);
        changedList.add( totalTabs-1, false);
        textPane = (JTextPane)selected;

        textPane.getDocument().addDocumentListener(this);
    }

these are function from interface

   @Override
    public void insertUpdate(DocumentEvent e)
    {
       changedList.add(tabbedPane.getSelectedIndex(), true);
    }

    @Override
    public void removeUpdate(DocumentEvent e)
    {
        changedList.add(tabbedPane.getSelectedIndex(), true);
    }

    @Override
    public void changedUpdate(DocumentEvent e)
    {
        changedList.add(tabbedPane.getSelectedIndex(), true);
    }

This is how I've tried to save

 public void saveAfterChange()
        {
            if (changedList.get(tabbedPane.getSelectedIndex()))
            {
                int reply = JOptionPane.showConfirmDialog(null, "Save?", null, JOptionPane.YES_NO_OPTION);

                if (reply == JOptionPane.YES_NO_OPTION)
                {
                    save();
                }
            }
        }
0

There are 0 best solutions below