Set Caret position with JTextArea in JScrollPane - java

Right now I have a JTextArea inside of a JScrollPane. For the current content it has both a vertical and horizontal scroll bar showing up. I'm trying to implement a search functionality where a user can search for a certain string and it will set the caret position to the first occurrence of that string. However it seems that JScrollPane only scrolls vertically when I set my caret position. So matching strings going off the JTextArea horizontally will completely get missed and the horizontal scroll bar won't scroll at all.
I'm using the basic function
setCaretPosition() for the JTextArea
Does anybody have any idea why my JScrollPane isn't moving horizontally using setCaretPosition()
Edit:
It appears the horizontal scroll bar is scrolling but it moves so little that it's barely noticeable. I can only see the very first pixel of the character. Is there a way to have the scrollbar center (or as much as possible) to the caret position?

You should be able to use the Visible Caret Listener.
Or you can also look at Center Line in Scroll Pane. It only centers the line vertically, but you could customize the code to do horizontal as well.

Related

Removing JavaFX Scroll Pane Hotkeys

I'm trying to put a text box (custom rendered) into a ScrollPane except when I hit space to put a space into the text box, space is also the hotkey to scroll down in the scroll pane so it enters the space and than scrolls down in the scroll pane. How do i remove the hotkey that space scrolls down in the ScrollPane? I've been searching through ScrollPane looking for anything relating to vertical scroll bar or hotkey but didn't find anything.
See this link
JavaFX: scrolling vs. focus traversal with arrow keys
i think it's what you want ,try to change switch-case method with something like this
switch (event.getCode()) {
Case SPACE:
...
}

need noRepaint() when i validate()

I have a JScrollPane with a View. I'm doing a chat application , it's why i need to have my ScrollBar to the Maximum(). To get the right Maximum of the View i have to validate before. When I validate my ScrollPane an automatic repaint is doing. I don't want this repaint because it makes a double repaint when i set the ScrollBar to the Maximum : when the ScrollBar is at the top and a another when it is a the bottom.
my code :
Main.getWindow().getMainPanel().getScrollPaneCenter().validate();
scrollPaneCenter.getVerticalScrollBar().setValue(scrollPaneCenter.getVerticalScrollBar().getMaximum());
PS : I want to disable the repaint of my conponent or maybe you have a solution to have a reversed JScrollPane( to always have the ScrollBar to Bottom) .
I'm doing a chat application
I'm guessing your are using a a JTextArea or JTextPane.
it's why i need to have my ScrollBar to the Maximum().
You don't need to validate or set the scrollbar manually. You juat append the text to the bottom of the Document. See Text Area Scrolling for a couple of solutions.

JScrollBar with highlighting position of selected text in JTextPane

I have JTextPane inside JScrollPane. When I highlight some word in text pane I want to have their position highlighted on JScrollBar (Similar to highlighting errors in source code in Eclipse).
Is this possible with Swing?
The "immediate" problem you have is the fact that the scroll pane does not support the concept of "row footers", which would provide you an area on the right hand side of the scroll pane you could render you highlight points
Choice #1
What you need is an implementation that does. You could take a look at JideScrollPane which provides not only support for row footers, but column footers as well.
From there it's a simple case of using the same concept as decorating a normal scroll pane (row and column header).
Check out Providing Custom Decorations for some hints.
Choice #2
The other choice (I can think of) would be to place a normal JScrollPane onto a JPanel using a BorderLayout so that the scroll pane occupied the center position. This would then allow you to place a custom component to the EAST position that would act as you "marker" pane.
This is slightly simpler, as it doesn't require a lot of additional changes to be made. You would then need to calculate the position of the text in the view port as a percentage of the it's height, which would allow you to translate it back to the "marker" pane

GWT - How to center contents inside ScrollPanel

I have a FormPanel inside a ScrollPanel. The ScrollPanel is located in the center part of DockLayoutPanel. I want to vertially and horizontally center the FormPanel inside the ScrollPanel. I tried a few ways to do this but no success.
I have tired putting a verticalPanel/horizontalPanel inside the scroll panel, and use it to wrap the formPanel. I set both scroll panel and horizontal panel to 100% width and height. However, the scroll panel is automatically resized according to the size of center part of DockLayoutPanel whereas the horizontal panel's size is always equals to the size of its child- form panel. So I cannot center the formpanel inside of horizontalPanel since their height and width are the same. I try to make the horizontalPanel's size be always the same as scrollPanel, but I have no idea how to do this. Setting horizontalPanel's size to 100% is not working.
So My question is this:
1.How do you center something in scrollPanel. I don't mind using css method if you know how to achieve this.
2.In my case above, is it possible to make the horizontalPanel to be always the same size as its parent container - scroll panel. If it is possible, my 1st question is solved then.
I had to center an image inside a horizontal scroll-panel. Sometimes I just have one image and sometimes there is a list of images to be centered and shown. I have fixed the size of the GWT image object and the width of the scroll panel.
I computed the scroll position inorder to center all the items and I used set scroll position (position can be -ve or +ve values).
When user scrolls the items then I would respect the user's decision and scroll them accordingly. However, if the user scrolls to extreme right of left, I would ensure that the scrolling positions are re-adjusted to scroll back to the boundary and not exceed it.
When the page is refreshed by scroll panel centres the items automatically.
In short, you need to center the items programmatically and by default the scroll panel would keep the items on one side (left and top).

How does a scroll pane do scrolling

In windows, Java, etc, the scroll pane scrolls the widgets inside. What I'm wondering is, how exactly does it do its scrolling? Does it change the location of each nested widget, or does it have a content widget that it moves around? Or is it something else? Also, when both scrollbars are present, how does it mask that little square at the bottom right? That square is sometimes used to resize. Is it a separate nested widget?
Thanks
I think it just changes the location of the widget, button, or thing-a-ma-bober.
But my second guess would be it just draws the components "outside" of the scroll pane without being seen and when you scroll it just redraws dynamically.

Categories