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.
Related
I currently have an app that essentially let's the user enter text in an editText. And when the user clicks the button "create", it creates a textView with the text of what was in the editText(the user can make as many as he or she wants). Simultaneously, I have a sharedPreference that will save the String entered by the user when a textView is made. My app then allows the user to go onto another activities page that has them doing something else with each piece of text they entered on the previous page. So, I need my app to retain the textViews that were created. This is why I have the sharedPreference saving the text of each textView. My question is, how can I take each String saved into my sharedPreference, and then put each onto a textView that will be created when the activity starts. I won't know the names of the key for each value, because it will be whatever the user typed in, and I won't know how many pieces of text the user will make, because there isn't a limit or minimum to how many he or she can make.
Use
Set<String> keys = pref.getAll().keySet();
to get a set of all the keys in the shared preference. Then display them however you want (a listView sounds like a good idea).
Set<String> keylist = pref.getAll().keySet();
for (String s : keylist) {
//Use sharedpreference with s as the key and get the values
}
I would like to display multiple texts in a text wizard container. I have used the setText method but it displays only the last text. Please can someone gives me a solution
As its name suggests Text.setText sets the text displayed in the control and discards any previous text. So you need to construct a string containing all the data you want to show in the control and call setText with this. You could use a StringBuilder for this, something like:
StringBuilder buff = new StringBuilder();
for (Object element : al)
{
buff.append(element).append("\n");
}
text.setText(buff.toString());
You will need to create the control specifying the SWT.MULTI style and make sure it is sized to show multiple lines.
Sorry, I'm kind of a beginner to GUI interfaces (well, a beginner to java, really), and I was wondering: How does one make a list of items that the user can add items to or remove items from, with the press of a + or - button?
What I really want (sorry if I'm being a little vague here) is one of those lists you sometimes see in application windows, which looks like a text box but cannot be typed in. Right now my application (it's a small app to organize the schedule of a hospital) just reads from a text file and writes to another text file. No GUI, no window, the user just writes a bunch of names in the text file, one per line, then runs the jar and opens the output file and the schedule is there. I want them to just be able to add or remove names from a list with buttons — adding a name by clicking 'plus' and typing the name, and removing a name by selecting the name and clicking 'minus'. I still, however, want it to save to a text file, so that the next time the user opens the app, all the names are still on the list.
Just to be clear, I don't want a list displaying the output (i.e. the names organized into a schedule), just one containing the input.
Thanks a lot for any help you can give.
You probably want a JList with a couple of JButton instances to add/remove items.
See:
How to Use Lists
How to Use Buttons, Check Boxes, and Radio Buttons
In traditional java jTextArea we had the insert(String value, int Postion) method to insert text at a specific location. But i couldn't find the same functionality in the gwt equivalents.
I can take the whole string, insert what i want where i want it and then set the result back to the textarea but taking the whole text out and setting it back EVERYTIME doesn't seem very efficient. Is there a way of inserting the text at a location of my choice?
You can use Formatter to insert HTML at the current cursor position.
RichTextArea.Formatter formatter = myRichTextArea.getFormatter();
formatter.insertHTML(html);
You still need to add your text to a String and replace TextArea contents if you need a different position. I don't think users will even notice this - it happens very fast.
Woomma .. Our Gwt seniours came across all these brain storming issues and implemented some children libraries for us ..
I strongly beliving you are looking for this and hope it will helpful for you
Replace string for GWT
All you need to do is just add the small method to your class and use as
String returnedString = replace(text, searchString, replacement, -1);
Coming to your EVERYTIME replacing issue... We have no other option as of now
I used to make my UI by making declaring new JTextField in a for cycle while attaching an action listener by anon class to each JTextField, which means that you have to press enter fire an event which will read the text of the field and put it into an array here is the code
Getting data from JTextField that is one of several local variables without a few minor changes . Now I have to modify it to so that i press a button like Apply in order to have values written into an array. While I found two ways to do this wonder what is the optimal way to do this .
The horrible way . Create an array that acts as temporary storage ,
replace ActionListeners with DocumentListeners that will place
the values into this temp array . And a button that will on press
iterate through the the temp array placing its values into the
target array.
A better way which I found while searching , create a JTextField
array as public and simply have a button that will on press iterate
through the JTextField array and place its values into the target
array.
Adder is an example that maintains a List<JFormattedTextField> to enforce formatting. It uses a PropertyChangeListener & FocusListener to update on navigation events, such as the default key binding to Tab and Shift-Tab.