JScrollBar with highlighting position of selected text in JTextPane - java

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

Related

JTree vertical scrollbars not working

I have a JTree embedded in a JScrollPane. When the JTree grows bigger than the display can show, the remainder of the tree is located at the bottom outside of the visible range. I expected the JScrollPane to start displaying vertical scrollbars if this happens so that you can scroll the remainder into view. The vertical scrollbars do not appear at all if the JTree is expanded and doesn't fit. Here is how it looks like:
I have both, the horizontal and vertical scrollbarPolicy set to as needed. I also tried embedding the JScrollPane containing the JTree into a JPanel but it didn't help. I'm using the UI designer in IntelliJ IDEA to build the GUI by the way.
Any help on making the vertical scrollbars behave as expected? Forcing them to be shown at all times also doesn't work: They cannot be dragged down regardless.
not use preferredSize.
in UI designer choos JTree element, find property preferredSize and in context menu select Restore default value

How to make JTextPane wrapped in JScrollPane shrink to fit the text

I have a JTextPane with HTML text.
I used GroupLayout (using WindowBuilder).
I've set the minimum size of my JFrame to 800x600 so the user cannot make it smaller than that.
The app has a big scrolling JPanel the size of the entire window. The top part of the panel is taken up by a JTextPane wrapped in JScrollPane. I have disabled the scroll bars and sized the JScrollPane to make the entire text visible.
In group layout the JScrollPane is set to stay constant vertically, but size horizontally.
My issue is that when the user makes the window larger the JScrollPane also expands, but now there is a big white space left at the bottom of the text pane. Is there a way that I can make JTextPane shrink to fit its contents.
Also if you suggest a different layout, I would be willing to try it.
I used this TextPanePerfectSize example from #camickr to solve a similar problem. The example uses validate() and pack() to adjust to the preferred size. You might be able to adapt it to your situation.
Take a look at SpringLayout. It gives you far more control over the positioning of components. Look at the SpringLayout tutorial if you get stuck.
The trick in your case is to bind the bottom (south) of your JScrollPane to the top (north) of the screen.

Scrollable vertical list of text box inputs using Java Swing

I'm trying to build a simple interface for an assignment, in which multi-line word-wrapped input boxes can be stacked vertically in a single, fixed-width column. then the whole stack (if tall enough) has to scroll vertically inside of a scroll pane with the same fixed width and a fixed height.
The active box has to change height dynamically to fit the amount of text as it is being typed/deleted. This means the y position of all subsequent inputs in the column should change accordingly. A layout manager's job, right?
I started reading about the swing layouts, and it seemed like only the GridBagLayout could do this. Since this is my app's only interface window, it seemed like a clunky layout to achieve something simple.
So, which swing layout should I use, along with which text input class for word-wrapping and auto height adjustment? Thanks.
A BoxLayout might be what you are after for this use-case.

Java Swing FlowLayout Alignments

I'm fairly new to Java and I'm trying to create a GUI application with some labels, buttons, and textfields. The program is pretty simple and I just wanted to use a default layout, which is FlowLayout. I managed to place and size everything fine, but the only thing seem to be not working is the alignment. I want to place buttons and textfields with certain alignments, but whenever I set an alignment, it moves the text inside of whatever the object rather than the object itself. For example, I wrote:
button.setHorizontalAlignment(JButton.RIGHT);
but it seems like it aligns the text inside the button instead of the button itself.
Is there any way to align the button itself rather than the text inside of it?
I know the alignment stuff could be easier with some other type of layout (e.g. BoxLayout), but I just want to use the FlowLayout for this one, unless it is impossible to align them using the FlowLayout (which I don't think so).
Thanks in advance.
See the constructor FlowLayout(int align).
Constructs a new FlowLayout with the specified alignment and a default 5-unit horizontal and vertical gap. The value of the alignment argument must be one of FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.LEADING, or FlowLayout.TRAILING.
It seems you are after a FlowLayout.RIGHT as seen in this answer (the combo and check box at the top).
I don't think you can do this with a FlowLayout alone.
My suggestions would be:
Consider switching to MigLayout which is a much more powerful layout mechanism. MigLayout basically lets you position you components within a flexible grid, and you can set the specific alignment of a component within each grid cell.
When you want alignment of subcomponents, it also often makes sense to put them inside a nested JPanel. You can then use a separate layout for this JPanel (BorderLayout perhaps?) which will enable you to get the exact alignment that you want.
setHorizontalAlignment of AbstractButton sets the horizontal alignment of the icon and text not the position of the button. AbstractButton's default is SwingConstants.CENTER.
If you want to align the button..set the position while adding it to the panel or frame..something like this....
p.add(button, BorderLayout.SOUTH);//using `BorderLayout`
Flow layouts are typically used to arrange buttons in a panel. It will arrange buttons left to right until no more buttons fit on the same line.

JSplitPane resize behavior

I have a vertically split JSplitPane and when I move the divider down, it shifts the bottom component and the bottom gets cut off. Is there a way to specify the resize behavior of a JSplitPane so the top (of the bottom component) gets covered by the split pane and the bottom is the last thing to get covered?
thanks,
Jeff
The components are painted at the "(0,0)" position for each part of the split pane.
I guess you could create your own custom UI that does whatever you want.
A simpler approach might be to add the component to a scroll pane. You could then just let the scrollbars appear if required.
Or, if you really want only the bottom part of the component to be shown you could control the viewport position whenever the divider is moved. You can handle this by listening for a "dividerLocation" PropertyChangeEvent.
I think you might need to attach an event to the JSplitPane's resize event (I forget exactly what it's called). The event should then move the content up to suit.

Categories