开发者

Java ActionListener history

I made a java program with graphical user interface which simply reads txt files. I have some fetures such as show table of contents, go to a specific page of the txt file or search for a 开发者_运维知识库word in that txt file. Also I have bunch of buttons to perform these features. One of my buttons simply aims to go back like a "back" button.

I tried to save the JTextArea's contents to a String object and, add it to stack. Everytime I press the back button, I get the last string object that I put to my stack, and set the JTextArea to this string object.

Unfortunately, it doesn't look efficient to me. I'm facing a lot of errors. Are there any other ways to to that: saving actions?


If you are updating the text in the JTextArea (via the setText() method) something like this should work:

class TextAreaHistory {

HashMap<Integer, String> textAreaHistory = new HashMap<Integer, String>();

int counter = 0;

public void addToHistory(String s) {
   textAreaHistory.put(counter, s);
   counter++;
}

public String getHistory() {
   return textAreaHistory.get(this.counter-1);
   counter--;
}
}


It might be best to store index references (eg number of characters into the file) rather than Strings. Then have a method which shows a page from the text file, starting with the given index character.

Whenever the user clicks on a page in the table of contents or searches for a word etc, add the index of the first character of the displayed page to the stack. When the back button is pressed, pop the index off the stack and display the appropriate page.

It would only be useful to store Strings if you were implementing a text editor (with an Undo function) rather than a text viewer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜