How can i display multiple texts in text container of a wizard - java

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.

Related

Question about displaying text in LibGDX using a console

I had a quick question. I am writing a text based game using Java and LibGDX. I want to create something like a console. I would like to store all previous messages while being able to add new ones. However, I don't need users to input any data into the console. I saw that labels might work, but I am unsure of how to keep the existing text while adding new text to a label. I am fairly new to using LibGDX and thought someone could think of a better way to do this.
Thanks
Just a quick suggestion for where to start, since I haven't done this myself:
Label is the typical way to draw text, but it re-computes the glyphs for all the text every time you change the text. Under the hood, it uses BitmapFontCache for the rendering. BitmapFontCache lets you append text using addText() without recomputing what it has already done.
So what I would do is use BitmapFontCache directly for your text rendering, and append text to it. Maybe calculate how many lines of text it takes to fill the screen or half the screen, and create a new instance each time you reach that threshold. That way, you can selectively render only the ones that overlap the camera viewport so you aren't drawing hundreds of off-screen glyphs.
In a simpliest way you can just recreate your label every time. Do something like this.
String text = "text";
Lable createLabel(String addText) {
text = text + addText
return Label(text, skin)
}
Then just call this function where you need a label

JavaFX display text area appending - any way to highlight/make new text more visible?

I have a display text area in my JavaFX app.
It has several methods for appending strings to it.
What I want is for any newly appended text to have some kind of feedback so the user is more visually aware of the new text being displayed.
Ideally what I would like is one of the following, but am very open to other suggestions:
The appended text is 'typed out' as if someone were typing it into the display text area itself. Or..
Some kind of highlighting of the new text as it appears.
Although again, I'm open to suggestions!
Thank you.
As suggested in Highlighting Strings in JavaFX TextArea, you can select the new text.
Assuming the text area is not editable, and you are only ever appending to the end of the existing text, you can do:
textArea.textProperty().addListener((obs, oldText, newText) -> {
if (newText.length() > oldText.length()) {
Platform.runLater(() ->
textArea.selectRange(oldText.length(), newText.length()));
}
});
If your text area is editable, or you are using it more generally, then you can do something similar each time you append text. For example:
String message = ... ;
textArea.positionCaret(textArea.getLength());
textArea.appendText(message);
textArea.extendSelection(textArea.getLength());
Maybe try to use RichTextArea (https://github.com/TomasMikula/RichTextFX), then is many possibilites to show your text in different ways.

suggestion on how to read a large file to jtextarea

I want to read large file like 10-15k lines to jtextarea.
Beside that, I also have to add every line to List and to highlight some specific lines in jtextarea.
What I tried for now is, I pass file into FileReader into BufferedReader. Inside my SwingWorker, in doBackground method I call:
while ((line = br.readLine()) != null) {
textArea.append(line);
textArea.append(System.getProperty("line.separator"));
list.add(line);
highlightLine(lineNumber);
}
When I run the program, and I choose file and open read process, it instantly loads up to 700 lines, then program slows down and loads like 10 lines per second.
Another idea I have is to, read a whole file with JTextComponent read method (which seems to setText faster then append every line), and then, read again whole file or iterate through every line in jtextarea and add that line to List and also highlight, which I think is not very efficient. What do you suggest me?
I want to read large file like 10-15k lines to jtextarea
Use the read(...) method of the JTextArea class to read the entire file directly into the text area.
I also have to add every line to List
Why do you need two copies of the text? If you need a line of data you can get the text from the text area:
int start = textArea.getLineStartOffset(...);
int end = textArea.getLineEndOffset(...);
String text = textArea.getDocument().getText(...);
to highlight some specific lines
Use a Highlighter to highlight the lines after they have been loaded into the text area.
Highlighter highlighter = textArea.getHighlighter();
highlighter.addHighlight(...);
Again you can get the offsets of the line using code from above.
Use the Document interface. it is the model that holds the data of the view-component that is JTextArea. You can get it from JTextArea with getDocument or you can use one of the classes that already implement Document: AbstractDocument, DefaultStyledDocument, HTMLDocument, PlainDocument. Then add your Document of choice to JTextArea with setDocument.
You can use insertString(int offset, String str, AttributeSet a) to add content to the Document. It also supports several listeners and you could consider a using render(Runnable r) to style it the document.
I haven't tried this but I would suggest putting all the file contents into a String, then use the setText(String text) method to set the text of the JTextArea all at once.

Jtextpane copy coloured text and paste

I have a JTextPane which displays colored text . I use the followong piece of code to get text from JTextPane.
String temp = pane.getDocument().getText(0,pane.getDocument().getLength());
However when i try to set the temp variable content to pane again,
pane.select(0,pane.getDocument().getLength());
pane.replaceSelection(temp);
by this way i lose the color and get white text. Is there anyway where i can maintain the color of text without copying the content to clipboard.
Please help.
Actually it depends on EditorKit you use. The first part returns text (with styled info) of the selected fragment. E.g. in the RTFEditorKit it would be rtf content of the document's fragment.
The second part is not correct. Replace selection can't process the content properly. I guess in case of the RTFEditorKit it would be all the text with formatting inserted in the pane.
I would use
pane.setText(temp);
instead. If you need to insert the styled fragment use kit.read(...) passing the temp in the call
You can try the Kit as alternative of the default RTFEditorKit and see what happens
UPDATE: Sorry original comment was incorrect a bit. The code should be
pane.getEditorKit().read(
new StringReader(temp),
pane.getDocument(),
pane.getDocument().getLength())

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.

Categories

Resources