How to calculate the words length in JTextPane - swing

I am not mean the length of a string, I mean the words length.... for example...
As you can see that "This is a testing." is shorter than "THIS IS A TESTING.". But how can I get the different between these two words length, also, I would like to know the position of the text, do I enable to get these information from JTextPane? Thanks.

JTextComponent has methods to convert between model and view coordinates:
int docEnd = myTextPane.getDocument().getLength();
Rectangle r = myTextPane.modelToView(docEnd);

Related

Find text in string, case insensitive

I have made a small script to check my expenses. In the description I look for a term. So I search within a string (like a supermarket brand). Unfortunately, it can only check case sensitive. How to fix this? I want to search within the string independent of the case. The search terms can be upper of lower case, easy to adjust.
for (i in uitgaven)
{
for(n=0;n<data.length;++n)// iterate row by row and examine data in column A
{
if(data[n][3].toString().match(uitgaven[i][0])==uitgaven[i][0]){ data[n][4] = uitgaven[i][1]
};
}
Just substitute this:
data[n][3].toString().toUpperCase()===uitgaven[i][0].toUpperCase()
for this:
data[n][3].toString().match(uitgaven[i][0])==uitgaven[i][0]

AS3: Change TextField Text on multiple instances of the same MovieClip

I have a MovieClip called "number" in the library. I need to add multiple instances of that MovieClip to stage. Instances should be called number1, number2,number3...and each one needs to have different text inside it.
Is it possible to do this without code, just using flash interface tools? If not, could someone help me with coding that?
Thanks!
For an Class linked MyNumber containing a text field named output:
const N:int = 3; // 3 instances
const TEXTS:Array = ['text 1', 'text 2', 'text 3']; // 3 texts
var n:MyNumber;
for (var i:int = 0; i < N; i++) {
n = new MyNumber();
n.y = 50 * i;
n.output.text = TEXTS[i];
this.addChild(n);
}
You have to use code - at least a little bit.
In addition to #helloflash's answer, here is a simpler solution (with some caveats described below).
On your movieClip, make your text box dynamic, and give it an instance name of txt (or whatever you'd like). Then, put the following line of code on the first frame of your movieClips's timeline:
txt.text = this.name; //works if your text is a simple word with no spaces/puntuaction/symbols and doesn't match any actionscript keywords
This will set the text to whatever the instance name of each movieClip is. Will work great if you text is something simple like "Hello" or "Player1".
Now, if you're text is a number (or starts with one), or your text matches a keyword or already defined variable (like this/continue/function/break/stop/play etc), you'll need make it a bit more complicated, something like this:
txt.text = this.name.replace("$MC_","");
Then give your instance name in this format: $MC_stop, the code will strip out the $MC_ part and show the rest. so the text field would be "stop".
Now, if you want to include spaces, or most symbols (dollar sign, underscore and dash I think are the only supported ones), you'll have to add a replace for each one and create a place holder for that character.
So if your text was "This is my text", you should give it an instance name of `this_is_my_text" and this should be the code:
txt.text = this.name.replace("_"," "); //replace all underscores with space
Add as many replace statements for as many characters you need.
So, if you text was "1. This is my text!!!" - The instance name could be: $MC_1$dot_This_is_my_text$ex$ex$ex and the code:
txt.text = this.name.replace("$MC_","").replace("_"," ").replace("$dot",".").replace("$ex","!"); //you can keep chaining on as many replace statements as you need.
Of course, at this point you might as well just use full on code like #helloflash's answer. But if you text isn't that complicated, this may be a good solution for you.

How to get the real height of a character (fontmetrics/graphics2d)

I am currently in need to get the real height of a character. I am aware of the functions like getDecsent(), getAscent(), ... but they only allow to get values regarding the hole font (in its context). I also tried the way using getStringBounds(), but that is the same story.
Like the title says, I am looking for a way to get the height value of just one char at a time.
For example 'N' is higher then 'n', 'I' just a little bit higher then 'i' and so on
Thanks for your time
Use this
Rectangle2D bounds = font.getStringBounds("put your string here", context);
//font can be set to whatever you want
//my suggestion is that you use the same font for both characters
//context is a object of FontRenderContext
System.out.println(bounds.getHeight());
//Instead of just printing it you could do this a second time and compare the 2 strings

Use Textfield as a Variable Possible ? don't know

I am currently trying to do the following.
I have 2 Textfields, One is Taken from the Dataset (in the Pictures below i have used 400 as a plain number to make things more straight Forward).
now what i want is for one of them to be used as a Variable (not sure if this is the right Term)
i want the value that Text1 is given to automatically be divided from Text2.
Below you will see the Text1 (named Divide) Properties.
and this is Text2 where I am trying to use the Value of Text 1:
I Know everything about having to Convert The field into an Int or some Number, and everything,
what i don't get is how I am supposed to use Textfield1 in Textfield2 ?
Thanks for your Answers in Advance.
What programming language is that?
if it's C# you can do something like:
int x = int.Parse(Text2.Text);
this will convert the string of Text2 (Textfield) to an integer,
if it's java it would be like:
int x = Integer.parseInt(Text2.getText());
But if the user enter some character that isn't a number the problem will crash.
Here is the Answer, thanks again to Visakh16
=400/ReportItems!Divide.value
and here the link: Use Textfield as a Variable Possible ? - Social MSDN

Read Text File with AS3 and Tween Each Word from Small to Large out of the screen

I am looking for some advice on the best way to read in like 200k words and have them each tween from the center of the screen as small dots and tween up to the word filling the SWF then "fly through my head" not literally, but you probably get it...
What would be the best way in AS3 to go about this? I am fairly new to it.
Thanks!
Depending on the origin of your text, you may have to use regular expressions to get rid of punctuation and replace any amount of spaces by a set delimiter.
You could then use the split method of the String class to turn your text into an Array of words.
Each word can then be assigned to a Textfield. Since the Textfield is a DisplayObject, all the manipulations you are mentioning above become possible.
You may be able to streamline all this by creating a class that extends Textfield and defining various methods for the motions you want to implement.
You'll probably want to look at the Timer class and some tweening libraries
Pseudo Code
- Clean up String with regular expressions
-> expected result var cleanString:String = "word1;word2;...wordn";
- Turn String into Array
var words:Array = cleanString.split( ";" );
- Create a class that extends Textfield and define a manipulate() method
var tf:MyTextField = new MyTextField();
//this method could take parameters
//such as x, y, scale , time , delay , ease etc...
tf.manipulate();
- Create an Array( Vector ) of Textfields
loop thru words Array to return array of Textfields
var objects:Array = [ tf1, tf2 , etc...]
- Manipulate objects
loop thru objects Array to manipulate them
if you don't want to do it by hand (like PatrickS described) maybe this will help you ...
http://www.greensock.com/splittextfield/