How to do smooth scrolling for lists in Swing - java

On the iPhone scrolling through lists is animated smoothly. Ignoring the inertia effect (though having it would be cool as well) - how would I go about making a Swing listbox scroll smoothly like that?
There are other examples as well, like browsers, but those use native controls which I cannot use with Swing.

If you are using JList try overwrite getScrollableUnitIncrement(Rectangle, int, int) and getScrollableBlockIncrement(Rectangle, int, int) to return custom increment .. e.g. 2 pixels instead of default values computed from list entry height.

I'm not sure that this answers your question, but I had a similar issue. My problem was that dragging vertical JScrollPane knob made JList to scroll by blocks instead of by units, that is, the top cell was always displayed in whole except when list was scrolled to the bottom.
Curiously, this happened only when using Windows look and feel. By using default L&F I got the behavior I wanted. And after much digging, I found this post which revealed that there is L&F property for that. By default this is true for Windows L&F, but you can set it to false like this:
UIManager.put("List.lockToPositionOnScroll", Boolean.FALSE);

Related

Synthetica JButton Margins

So, I have a Calendar solution for my employers where I am using a custom look and feel (Synthetica) and each cell in the table holds a JPanel with a list of buttons and a PAGE_AXIS BoxLayout. I am trying to reduce the gaps between each button so that they abut each other, and I've tried setting the borders to null except that destroys the button appearance. I have used the following (as recommended by Oracle to view the actual size of the components) code;
setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.red),
getBorder()));
And this is what I get;
Setting the margin does not work with whatever border version the buttons are using. So, is there any way to find the current border that it's using? So that I can set that border's insets manually, or something like that. Basically, I need the buttons to abut each other. Any ideas?
Clarification: I want the button's themselves to stay the same size, but I want the white space around them (highlighted inside of the red borders) to be gone,
Several possibilities should be considered:
Manipulate the button's bound properties: setBorderPainted(false), et al., as suggested here.
See if a suitable sizeVariant is available, as shown here.
Use a custom UI delegate based on BasicButtonUI, as shown here and here.
I tried to get the Synthetica L&F to have a look but seems as though you have to create an account which I did not want to do. Anyways, a quick look around the website and I found a page which indicates how you can see and configure some of the values set in the L&F. Might be worth having a look there.
http://www.jyloo.com/synthetica/customize/

Find standard JScrollBar width or height

I'm trying to place a component on the corner of the window. However, my window has a scrollbar and the scrollbar is placed on top of the component.
So I'm trying to change the position of the component so that it is adjacent to the scrollbar rather than have it be under it.
Thus, I need the standard width of a scrollbar (vertical) and the standard height of a scrollbar (horizontal).
I could try getVerticalScrollBar.getWidth() at runtime, but unfortunately I need to place the coordinates before I create the scrollbar themselves.
Also, creating an empty JScrollBar and calling getWidth() is returning 0.
Thanks for your help.
Not sure if it helps you right now after a year ago, but:
int scrollBarSize = ((Integer)UIManager.get("ScrollBar.width")).intValue();
I found it here on this discusion: http://www.coderanch.com/t/341287/GUI/java/Calculating-width-JList-vertical-scrollbar
Don't get the size or set the size but rather let the layout managers do the work for you. Consider
adding the scrollbars by default to the JScrollPane via the setVerticalScrollBarPolicy(...) and setHorizontalScrollBarPolicy(...) method pair so that the layout managers take the scrollbars into consideration when laying everything out from the get go.
Avoid null layouts at all costs as this will take away one of the most powerful tools for creating flexible workable Swing GUI's.
If these recommendations don't help, then consider creating and posting an sscce.
I'm not sure about other OS's, but on windows XP it is 17 pixels.
What I would do is temporarily include this line in your program:
System.out.println(jScrollPane.getVerticalScrollBar().getWidth())
then use the value printed.

Style TreeViewer's scrollbar

I have an editor that has both draw2d tree and Nebula's GridTreeViewer (basically a JFace TreeViewer). What I need is working, except scrollbars. In draw2d I get scrollbars provided by org.eclipse.draw2d.ScrollPane and in GridTreeViewer I get scrollbars from JFace. I am not sure why, but scrollbars look different, I would expect them to look the same since they are calling OS's native controls if I am not mistaken? Is there a reason why scrollbars look different and is there a way "downgrade" JFace's scrollbars to look like draw2d's? Perhaps there is mistake in setting up ScrollPane's scrollbars?
Here is the screenshot, this is on Ubuntu 13.04:
There is no mistake on your side. Leftmost tree has a custom-drawn scrollbar (I assume that's Draw2d tree) and rightmost tree shows native scrollbar (I guess those are the ones you call "JFace"). You can notice that most applications outside of Eclipse show scrollbars like the "JFace" ones. That is, if Ubuntu overlay scrollbars are enabled.
There is no easy fix for this. IMHO, you could try to get in Draw2d sources and try disabling their custom-drawn and replacing them with native ones (from SWT). Or you could use a custom tree instead on Nebula control...
Please note that "JFace" toolbars will look drastically different on Mac OS X or Windows, will have different size and such. And the users will expect them :)

JScrollPane: Disable Scroll-by-dragging

I am using some JGraphX components but this question can be applied to a general purpose of java.
I have a component inside of a JScrollPane. The issue I am having is that I have the ability to pan in the view by "grabbing" the view and moving it around like Google maps. but if the cursor leaves the viewport the scrolling changes directions. I have done some research and found the issue causing the problem. It can be found part of the way down on this page where it starts talking about how the scrollpane works.
http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
Move the cursor over the image and press the cursor. Continuing to
press the cursor, drag to a point outside the image and pause. The
visible area of the image moves toward the cursor. This
scroll-by-dragging functionality is enabled by the scroll pane, and
with the JComponent API, but it is implemented by the custom component
that displays the image.
It has the little demo so you can see how it works as well as the code is available for download.
So by default the scrollpane has a scroll to drag implemented but it is actually opposite of how I need mine to scroll. The JGraphX library I am using has extended most of the classes used to benefit itself in some way but the basic idea still applies. I really just need to know of the easiest way to disable this. Yhe page says its enabled by the scrollpane and the JComponent API, but what actually gets enabled?
Not sure but I think you need to use:
component.setAutoscrolls( false );
This method from JComponent looks like the best starting point to investigate. You could retrieve the component in your scroll pane and call that method with false on it. That's the default, but it's possible JGraphX has it enabled by default.
Alternatively, you could get the MouseListeners and/or MouseMotionListeners from the scroll pane and replace them with something that overrides the dragging behaviour. Since those are interfaces, you could use proxy objects for that. Seems somewhat hacky, though.

Component for glass pane console-style text display

I'm trying to provide a progress report for a slow operation, in the form of text scrolling up from the bottom of the screen with details on what's going on - it's an effect you may have seen a few times in video games when they're loading maps, making network connections and suchlike.
Glass pane seems to be the way to get the text overlay, that much I have working. My problem is exactly what component to use for the actual text display.
JTextArea can display text, but as far as I can see, it can only do it from the top of the screen down - is there a way to make it scroll text up from the bottom of the screen?
JLabel by contrast can align the first line of text to the bottom of the screen, and even take appended text on that line, but when I add more lines separated by newline characters, it just seems to swallow them up even after calling repaint and validate. Is there a way to make it scroll up with the new text?
Or is there another component I should be using instead?
I really like JXLayer for effects layered over Swing components. JXLayer was at one point scheduled to be included in Java 7. Unfortunately the moving around that has been going on Java.net lost all the good content that the author had. There are still some other great resources around (Java 7 required for this one) on the web. I use JXLayer to provide panels with a busy state having a web-like spinner and greyed out appearance.
Another alternative (not as capable as JXLayer IMHO) is MigLayout has absolute positioning, which is maybe easier than the GlassPane.
JLabel would be the easiest. Otherwise you will have to override paintComponent to do anything fancy like animating the text movement.

Categories