Trying to get the start and end point of each line within a text pane - jtextpane

Trying to get the start and end point of each line within a text pane:
The text pane contains (note target is the end of each line not including the blank space line):
(blank space line)
MVESMKKVAGMDVELTVEERN000TAQEGDHGSHVYTKQKEENKGGEDKLKMIREYRQMVETELKLICCDILDVLDKHDDDDDKVFYYKMKGDYHRYLAEFATGNDRKEAAENSLVAYKAASDIAMTELPPTHPIRLGLALNFSVFYYEILNSPDRACRLAKAAFDDAIAELDTLSEESYKDS00000VQVGQQRSDMQGDGKKKAAAEEQNKEALQDVEDENQtarget
MVESMKKVAGMDVELTVEERN000TAQEGDHGSHVYTKQKEENKGGEDKLKMIREYRQMVETELKLICCDILDVLDKHDDDDDDVFYYKMKGDYHRYLAEFATGNDRKEAAENSLVAYKAASDIAMTELPPTHPIRLGLALNFSVFYYEILNSPDRACRLAKAAFDDAIAELDTLSEESYKDS00000VQVGQQRSDMQGDGKKKAAAEEQNKEALQDVEDENQtarget
MVESMKKVAGMDVELTVEERN000TAQEGDHGSHVYTKQKEENKGGEDKLKMIREYRQMVETELKLICCDILDVLDKHDDDDDDDFYYKMKGDYHRYLAEFATGNDRKEAAENSLVAYKAASDIAMTELPPTHPIRLGLALNFSVFYYEILNSPDRACRLAKAAFDDAIAELDTLSEESYKDS00000VQVGQQRSDMQGDGKKKDDDDDDDEEQNKEALQDVEDENQtarget
//This is what I have
Element root = jTextPane1.getDocument().getDefaultRootElement();
Element one = root.getElement(0);
while (one !=null){
int one1 = one.getStartOffset();
int two1 = one.getEndOffset();
System.out.println(one1);
System.out.println(two1);
one = root.getElement(two1);
}
This is what I get (1st and 2nd element) and then hangs:
0
1
1
232

You mix model and view. Document is just model but amount of rows depends on view (width of content).
Use javax.swing.text.Utilities.getRowStart()/getRowEnd()
See an example of the code usage http://java-sl.com/tip_row_column.html

Related

C# Read data from a text file and display in Text areas parallelly

I need to develop an application in C# with two screens, each contains 2 text Areas/Boxes in it.
When user presses the Start button on first screen, the application should start reading the text file ( having some data on each line ) and populate the odd lines in one text area and even lines on other text area parallelly.
Also, there will be a separate screen ( with 2 text areas ) where you will send data from first screen via TCP to fill the 2 text areas based upon the odd/even line #.
Please help me on this.
try this
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
textbox1.Text = File.ReadAllText(openFileDialog1.FileName);

How to organize photos within data in a text field?

Currently I am able to load all of the photos onto the page, however, they all load in the center of the app on top of one another. I want to have the images display before the data it belongs with.
For example:
[photo]
Name:
Sex:
Age:
[photo]
Name:
Sex:
Age:
...
This is my code currently (data loaded from xml url) and I have tried to look at rect and other properties, but I have not been able to have any luck.
repeat for each line tChild in tChildren
add 1 to x
put revXMLChildNames( tInfo, "ArrayOfXmlNode/"&tChild&backslash, return, "adoptableSearch", true) into tAdoptable
put revXMLNodeContents( tInfo, "ArrayOfXmlNode/"&tChild&"/"&tAdoptable&"/Photo") into tURL
put url tUrl into tPhoto
if the result is empty then
set the text of img x to tPhoto
else
beep
end if
put "Name: " & revXMLNodeContents( tInfo, "ArrayOfXmlNode/"&tChild&"/"&tAdoptable&"/Name") & return after tData
...
end repeat
Thank you in advance for any help!
As you have no creation of the images in your code I assume that you have created them before the repeat loop, then you will need to lock the size and position of the image.
My other suggestion is to create a template group with an image and one or more fields (name, sex, age etc). You can then create a nice layout for each part. When done you can lock everything within the group but still easily set the location for the complete group. Finally you can then move the template group outside the card or hide it.
put 50 into tTop
put 20 into tLeft
put the width of group "templateGroup" into tTempGW
put the height of group "templateGroup" into tTempGH
put 0 into tCount
repeat for each line tChild in tChildren
copy group "templateGroup" to this card
put it into tGroupID # It holds the long id after a copy operation
put url tUrl into tPhoto
if the result is empty then
set the text of img 1 of tGroupID to tPhoto
else
beep
end if
# Position the new group
set the topLeft of tGroupID to tLeft,tTop
add tTempGW to tLeft
if tLeft + tTempGW > the width of this stack then
# Next item will be partly outside so we move down
add tTempGH to tTop
put 20 into tLeft
end if
# Set some proper name
add 1 to tCount
set the name of tGroupID to ("Person" && tCount)
put revXMLNodeContents( tInfo, "ArrayOfXmlNode/"&tChild&"/"&tAdoptable&"/Name") into field "name" of tGroupID
...
end repeat
You can of course use other values for top and left. I usually use an empty group to hold all other groups then you use the boundaries for that group as top, left, right.

Formatted String from List Psychopy

My task is a variation of a multiple object tracking task. There are 7 circles on the screen. It randomly selects 3 circles to change the color (red, green, blue) briefly to indicate to the participant to track these circles. After the color change, all the circles will change to the same color and the circles will move for a period of time. When the circles stop moving, a response prompt will appear, where the participant is to select one of the three colored circles ('select the red/green/blue circle'). I am having difficulty inserting which color circle to select into the formatted string. I keep getting the error message: unsupported operand type(s) for %: 'TextStim' and 'list'
I'm not sure if I need to or how to convert these lists, so any help would be much appreciated!
n_targets = 7 #seven locations
circles = [] #setting up the circle stimuli
for i in range(n_targets):
tmp = visual.Circle(win,radius = 27,units = 'pix',edges = 32,fillColor='white',lineColor = 'black',lineWidth = 1, pos=(posx[i],posy[i]))
circles.append(tmp)
cols = ['blue','red','green'] #3 colors the circles will change to
targets = random.sample(circles,3) #randomly select 3 of the 7 circles
TrialTarget = random.sample(targets, 1) #select 1 of the 3 circles to be the target for the trial
#code for movement would go here (skipping since it is not relevant)
#at end of trial, response prompt appears and ask user to select target and is where error occurs
ResponsePrompt = visual.TextStim(win, text = "Select the %s circle") %TrialTarget
In this line, you are trying to create a formatted string from a TextStim object and a Circle stimulus object rather than a string object and another string object:
ResponsePrompt = visual.TextStim(win, text = "Select the %s circle") %TrialTarget
i.e. ResponsePrompt is clearly a visual.TextStim, as you are creating it as one, and I think TrialTarget is a visual.Circle stimulus, as you randomly sample it from a list of Circles.
I'm guessing that you actually want to incorporate the colour label into the prompt text. So to fix both problems (the type incompatibility and the formatting syntax), you need to actually get one of the elements of cols, called say trialColour, and use something like this:
ResponsePrompt = visual.TextStim(win, text = "Select the %s circle" % trialColour)
i.e. here trialColour is actually a string, and the formatting operation is brought inside the brackets so it applies directly to the text string "Select the %s circle"
That should hopefully fix your immediate problem. You might also want to investigate using random.shuffle() to shuffle lists in place instead of random.sample().

Highlight predetermined locations in textbox

I have been using python 3.4 and tkinter to create an application to parse logs and format data and display results in a text widget. I would like to highlight text that is located at a known position on each line in the text window. I have seen similar highlighting questions regarding highlighting text in text widgets on this site and it has been very helpful.
My problem is that I don't need to search for the string or characters to highlight. I have the locations that I want to highlight and it could be any character in that location including white space. For example: I would like to highlight positions 0, 20, 40 on each line (eg: index 1.0, 1.20, 1.40, 2.0, 2.20, etc).
Since it is large files being written to the textbox I have to do this for the entire scrollable text window, so I need to maintain the textbox line number position.
When referring to a location in a text widget, you can append modifiers to indicate relative positioning. For example, given the position "1.0", the next position can be identified as "1.0+1c" (or "1.0+1char"). So, to highlight a single character at a given offset, make the start of the range the offset, and the end of the range one character greater.
Here's a quick hack that takes one or more "positions" and highlights that position on each line:
def highlight(text, tag, *positions):
last_line = int(text.index("end-1c").split(".")[0])
for linenumber in range(1, last_line+1):
line = text.get("%s.0" % linenumber, "%s.0 lineend-1c" % linenumber)
line_length = len(line)
for pos in positions:
if pos <= line_length:
start = "%s.%s" % (linenumber, pos)
end = start + "+1c"
text.tag_add(tag, start, end)
usage:
text = Text(...)
text.tag_configure("highlight", ...)
...
highlight(text, "highlight", 0, 20, 40)

How to limit the items in popup?

How to limit the items in the popup. "Sar" array contains lot of words, with respect to the selection. if my first popup contains 15 items and did the change. when going to the next popup, if it contains 10 items, the remains 5 items from the first also added to the second popup options.
How to kill that buffer in popup menu items? Iam using this code
global jar,myjar,sam,mySam,Dic,Sar
on mouseUp pMouseButton
put the selectedText of field "MytextField" into Ftext
if pMouseButton = 3 then
put the number of lines of (the keys of sam) into mySam
repeat with i = 1 to mySam
if sam[i] contains Ftext then
put sam[i] into Sar
split Sar by comma
end if
end repeat
put the text of button "M" into tText
put the number of words of (the keys of Sar) into mylength
repeat with x = 1 to mylength
put Sar[x] into line x of tText
end repeat
put mylength into kk---now iam using this code for delete the buffer
----repeat with j = kk to 100
---put "" into line j of tText
---end repeat
set the text of button "M" to tText
popup button "M" at the clickLoc
else
--popup button "M" at the clickLoc
-- do other stuff here
end if
end mouseUp
Is any option for limit the items in popup. Example:- if popup contains 15 items, i want to see only 10 items, remaining 5 could be see only clicking the bottom button in the popup
You are using globals which might not be what you want.
Try emptying the global with put empty into Sar before adding some new values or use locals which will be emptied after your code is done.

Resources