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
Related
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
I have a few records of data (less then 10). Each record consists of a few lines of text.
I want to present records to the user in a kind of grid, where user can select one of the records.
I was thinking about List component or jTable, but I couldn't make them displaying more then one line of text. What component should I use then, or how to approach this?
In subject I suggested AWT because size does matter, i.e. I want use this functionality in the applet and would like to avoid any extra libraries.
Thanks in advance
Thanks to maksimov's link I found examples of how to tackle this issue, and also very interesting link I missed somehow - http://docs.oracle.com/javase/tutorial/uiswing/components/html.html
To specify that a component's text has HTML formatting, just put the
tag at the beginning of the text, then use any valid HTML in
the remainder. Here is an example of using HTML in a button's text:
button = new JButton("<html><b><u>T</u>wo</b><br>lines</html>");
In my case it was just enough to set height of the row and add tag just before string data to be displayed. HTML tagging also let me use extra formatting, colors, etc,
Brilliant,
Thank you maksimiov
I m doing a project using eclipse plugin to create a IDE.
I have a view which consists of a textbox,when i enter some text in it and press the add button the text should replace a particular string in the editor.
I m able to find the current editor but i dono how to find the position of the string to be replaced.
Can anyone help me in doing this?
Rather than do this yourself, wouldn't it be easier to use String.replaceFirst() or String.replaceAll() to do the replacement?
All you'd need to do is "get" the current text String from the target editor, do the replacement and then "set" the resulting String back to the target editor.
I'm not familiar with Eclipse, but most text fields will probably implement an abstract class like JTextComponent which has handy methods to getText() and setText().
If you have the reference to the editor, and if it is an ITextEditor you can use
IDocumentProvider provider = editor.getDocumentProvider();
IDocument doc = provider.getDocument(editor.getEditorInput());
String content = doc.get();
Now you have the content and you are able to find the position of some code within this string.
If you have the offset to replace the text and the length to replace you can call
doc.replace(offset,length,newText);
If you work with Editors and Views in eclipse, please read first the concepts of an Editor, and how the data is provided and shared among mutliple editors.
Adreamus
/edit: I am not able to post a comment, though I will post it here: The other answer from Paul Webster uses a deprecated function "search". It is recommendet to use a FindReplaceDocumentAdapter instead. The method 'find' returns an IRegion, wchich contains the offset and length in the document connected with this Adapter.
In order to be able to display a sentence on a, say, JPanel with a GridLayout(1,0) [i.e., only one line/row] and then be able to draw a syntax tree (or similar) above it, I want to display the sentence as a row of Strings, which each include one word.
The single Strings should then be either selectable (as in a JList), or I should at least be able to get their Location on the JPanel via getLocation().
Up to this point I have tried the following options, and had the following issues:
- Single Strings as JLabels: The JLabels are stretched out to fill the JPanel width, re-sizing them to fit the single String they're displaying seems complicated. I would want to be able to do this, however, to make the sentence look like a sentence and not like a badly layed out table.
- JList: All the functionality I want, but I'm unaware of an option to re-size the "cells" of a single String (cf. JLabel above). Also, I'm having difficulties restricting display of the JList to a single line/row (cf. another of my questions).
- JTextArea: I couldn't get my head round how to get the Location of the single Strings that I had appended to the JTextArea.
I'm aware that drawString() might be an option, but I'm afraid to use it since I don't want to mix AWT and Swing. Also, I would need to calculate the int values for x and y for every single String. And I'm not sure whether I'd be able to get their Locations at all (although I could of course save their ints in a Map or Vector since I have to calculate them anyway).
Thankful for any suggestions! Thanks!
I would use JTextArea and method modelToView()/viewToModel() to get x,y for position in nthe string and position in the string for coordinates x and y.
Also use Utilities class getWordStart() getWordEnd() getRowStart() getRowEnd() methods.
EDIT: As noted by camickr in the comments, setSize() is not an appropriate way to lay out Components (as this is automatically done by the respective LayoutManager, I have removed the respective code from my answer.
Triggered by StanislavL's answer, I have found a solution to do it via JTextField, albeit by using one for each String rather than just one (as suggested by StanislavL).
I can now easily getLocation() for each JTextField. Simple, really!
I'd like to thank StanislavL for his answer, without which I'd never have though about this, and camickr for his comment.
Okay, thus may seen kind of odd, but I wanted to get some suggestions from everyone here. I am a beginning Java developer (after 2 years of ASP.NET web development) and I have recently began working on my first Java project - a calculator. I realize that their are tons of calculators out there, but I thought it would be a good beginner project.
Anyway, here is what I need help with. Currently, I am using a Scrolling JTextArea for display (instead of a simple JTextField) that is approximately 5 rows tall. I want the user to be able to scroll through the list to see previous entries and such. The format of the box will be equation on one line and the program will generate the answer on the next and so on.
My real question is, how is the best way to implement this? My fist idea was to read through the JTextArea when equals is pressed, down to the last line and try to search that line for the operator (+, -, etc.) and the operands. Is this the best way to go about this? Although, this would work would work, I think it could get cumbersome and sounds very inefficient. I am open to any suggestions, even possibly replacing the JTextArea is some other component would work better.
Thanks!
There's no need to read through the JTextArea contents - use JTextArea.append() to add to the end. Here are some examples of JTextArea content manipulation:
JTextArea ta = new JTextArea("Initial Text");
// Insert some text at the beginning
int pos = 0;
ta.insert("some text", pos);
// Insert some text after the 5th character
pos = 5;
ta.insert("some text", pos);
// Append some text
ta.append("some text");
// Replace the first 3 characters with some text
int start = 0;
int end = 3;
ta.replaceRange("new text", start, end);
// Delete the first 5 characters
start = 0;
end = 5;
ta.replaceRange(null, start, end);
If you are open to different interfaces, you might want to try something like a JTextField at the top of your view, from which you can receive as input your 'new' inputted equation, and then below it with the same width a JList that would scroll to have all of the previous equations and their results. That would make parsing of the current formula much easier, and you would also have an easy time of keeping your previous formula and their results in a scrollable list, with the easy option of keeping the most recent on top.
your idea is interesting. so you would have a line such as.
2+2
then when pressing calculate would add the line
4
and so on then you could type in another equation.
it could work but as you said it wouldn't be the most efficient implementation... but that's just a tradeoff of getting the desired functionality.
If i were going to implement it the way you discribed (with a JTextArea) I'd use scanner, and scan the value string a line at a time.
if the line has +/- in it then do the calculation and add both the original line and the answer to a string.
the new string is the new value of the text field.
this method would get pretty cumbersom as you would be continually recalculating the users old entries more were added.
I guess if you continually stored the last line of the document, when you run out of lines, calculate the last stored and append the answer, then it wouldn't be so bad.
Here's what I would do:
use a JTextField to enter in the calculations, and a JList to display the old ones and their answers.
You could treat each line as a single operation. That way you could use the String array returned directly by:
String [] operations = textArea.getText().split("\n");
And then you'll know that exactly each one of them as a complete operation ( may be invalid, but that' another story )
Is this what you asked or do I totally misread you?
I think a simpler solution would actually use two components. A TextArea to hold the "history" of what's happened so far, and a textfield where the user inputs new entries.
Thanks to everyone who replied. You all gave me some ideas to think about. I think right now, I am going to go with my original idea of using a single JTextArea and try to find ways to optimize the process. If that gets too difficult (which is very possible), I will follow the majority's advice and use two separate fields. Thanks for replying everyone!