StyledText nullpointer - java

Im working with SWT StyledText to display data to the user in one part of the window. In another part I have a graph, over which I slide my mouse pointer. As I slide my mouse over a point in the graph, it highlights the corresponding entry in the StyledText Area.
I want my textArea to automatically scroll to the newest change, so I am using .setTopIndex().
To determine the index I need to be able to look at which entries in the textArea changed from not highlighted to highligted, I use the following (to check if my styleRange changed):
styledText.getStyleRangeAtOffset(offset)
So far my program functions correctly. My next check is:
styledText.getStyleRangeAtOffset(offset).isUnstyled
or
styledText.getStyleRangeAtOffset(offset).foreground
or something like that. Here enters the problem. When I call any of these I get a nullPointerException.

Thank you for all the pointers :) after some debugging I discovered that the unedited styleRange is null. That explains the nullPointer I kept on receiving

Related

scrolling text in a bar

I'm making a program in java that recive some strings and then display them in a new frame scolling them in one line, like the news, (shown below)
There, the text are displayed in a line, like what I need, but my problem is that I don't know what I should use in java, I though about put a panel on a frame and then a textfield inside the panel, then I realize that it's impossible because for every message I may need to change the color of background for an highlight message...
So, if anyone can help me, can you tell me what should i use for the diferent message and how i can get the text scrolling?
Thank you all in advance!

Input Text fields don't move cursor on mouse click?

I have an issue with all of my Input Text fields, maybe someone has an idea:
I'm using GWT, and just discovered that the text in of my Input Fields cannot be selected using Mouse. Also clicking inside the text does not move the cursor.
But giving them Focus using the cursor works. Selecting all with CTRL+A as well as moving the cursor with Arrow Keys works as expected.
I inspected the TextBox specific onBrowserEvents() method and can see, that click events are received well. Just somehow they don't modify the text input control in the browser.
I tested different machines and different browsers, which all behave the same. So it must be something in my Application.
Maybe somebody has an idea what I could have done wrong? I can't even imagine anything that could produce this behaviour on all of my input fields.
It just turned out to be a CSS issue:
I had -moz-user-select: -moz-none; to prevent doubleclick issues for some of my components. I now excluded the input element from it and everything works fine again.
I use this to solve the problem with selecting text and moving the cursor on the text in the inputs:
*, ::before, ::after
-webkit-user-select: text

Formatting JEditorPane's cursor, tooltips, links

Right now, I'm using Java Swing to create a JEditorPane primarily for its ability to have hyperlinks. I've successfully been able to display links and have them execute behavior upon a click, but I'm running into a few problems with formatting.
How can I set the cursor so that it normally is an arrow, but changes to a text cursor when hovering over text? (In essence, the behavior a cursor has within a web browser). I tried
EditorPane.setCursor(new Cursor(Cursor.TEXT_CURSOR))
but that made it a text cursor everywhere, even when not hovering over text. Right now, hovering over a link shows a pointer hand; I'd like to maintain that functionality as well.
What is the best way to show tooltips or mouseover text when hovering over a link? I tried modifying the title attribute of the link but nothing showed up.
I was trying to implement links to skip down to a subsection of the page, much like http://en.wikipedia.org/wiki/Xkcd#History would take you directly to the History subsection of Wikipedia's xkcd page. How can I do this?
An answer to any of these would be great (and multiple would be awesome xP). Thanks a lot for your help!
As you said one can simply give answers to a single point as well, let me try one by one, here is the answer for your last Point 3
Just provide an id to your tag like this
<h1><a id = "top"></a>First Line</h1>
Now somewhere in the bottom of your page write this :
<p>Return to TOP</p>
Clicking this link, you will reach the above area of the PAGE.
Points 1 & 2 may be addressed using the approach mentioned here. In particular, the view/model conversion methods will let you condition setCursor() and getToolTipText(), respectively.
You can get source from here http://java-sl.com/JEditorPaneStructureTool.html
It shows how to obtain text view bounds. First you get caret position for current mouse poiunter using viewToModel() method. Then go down the Views tree achieving leaf view and calcualte it's bounds. See this http://java-sl.com/tip_view_rectangle.html
If your mouse pointer in the view's rectangle then your mouse over text.
You can check whether the text in caret position is link and show your tooltip.
Use this http://java-sl.com/tip_links_in_editable.html to see how to detect whether mouse is over link.
Point 3.rd is answered by #nIcE cOw

Dynamically add Textbox to Window when checkbox is checked

I'm just figuring out my way around SWT. I have a little problem that i cant seem to solve. I have a check-box in my window. When the check-box is checked, i would like add a multi-line, read-only, text box below it, lets say 200x200. I want the height of the window to increase to accommodate this text-box. When the check-box is unchecked I'd like the opposite to happen.
Could you help me with this? I can't find an example but maybe I'm not using the right keywords. Cheers.
--EDIT
the.duckman'ss answer was very helpful. I've managed to get it working to some extent. I'm adding a multi-line textbox 480px high. How do I automatically resize the window to accommodate the text box? When the user checks the checkbox, the textbox shows up but the height of the window doesn't increase to accommodate the textbox. My code is a little long so I've put it in PasteBin — http://pastebin.com/01RxKeEr
Thanks.
I recommend looking at the SWT Snippets to every beginner - that's probably the best place to go to with SWT questions.
This snippet does exactly what you want.
Edit
Ooops, I ignored the second half of your question, sorry. Simply add this line to your listener:
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));

jList in Scrollpane, seeking and displaying value of selectedIndex

I have a JList inside a Scrollpane. If you click on the list and move the arrow keys up and down it works like you expect, you can move your selection index and display around just fine.
Now, what I want to do is basically have a text box and i'm typing in the text box like "comic" and want it to seek to the index of that value. This WORKS just fine.
Where the problem is when the value of the list box is below, or above the viewable area. When it is, the selected index seeks, but does not change the position of the scrollable region. However, if I press the up or down arrows and requestFocus() to the list, and move up and down it seeks to the right viewable area.
What am I missing to make this happen WITHOUT changing focus. I want to be able to just type in the list all I want and have it show me what is selected. I feel i'm missing something obvious here.
If I understand the question then you should be able to use:
list.setSelectedIndex(...);
list.ensureIndexIsVisible(...);
If that doesn't help then post your SSCCE demonstrating the problem.

Categories

Resources