Style TreeViewer's scrollbar - java

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 :)

Related

How to choose gaps between components suitable for current Look and Feel?

When creating Swing GUI's, how can I best choose the horizontal and vertical gaps to be used to separate components? Something like asked here Windows Layout Look and Feel, but platform independent.
Generally I simply used 5 pixels for everything, that looked reasonable with the Windows Classic theme, but looks somewhat odd when running under Windows 8, especially when there are multiple nested container components (e.g. Dialog -> TabbedPane -> ScrollPane).
I have looked at UIDefaults, but there doesn't really seems to be any hints about component spacing there (I was hoping to find at least a few values that could be used as hints, but LayoutManagers seem to generally use hardcoded deafults, e.g. FlowLayout uses a hardcoded 5 pixel spacing).
What approaches can I take to make my layouts more look and feel aware (I am generally using the System Look and Feel) ?
If you want a layout of components that change a bit to fit your user's platform, then perhaps just use the user's system look and feel with:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Edit to provide solution for specifying more specific gaps.
To specify margin and padding between components you can try using a box layout. You might not need to use a box layout throughout the entire Swing code you have either, perhaps have a utility function you use right before you add a component that accepts a Swing component, and a left right top and bottom margin/padding. This function would then return you a JPanel with a box layout with the margin/padding passed, along with containing one component, the one you passed in.

How to auto resize java swing elements?

I'm trying to auto resize the left side of my application. I have a JTextField and a JTree on the left and 3 JButtons on the right. But I just don't know how to make the left side auto resizeable.
I did it with the Netbeans GUI Creator (or whatever it is called) but I don't know how to to it without Netbeans. (I usually don't program with Netbeans, this was just an exception to see if it's even possible to do so with Swing.
Here is the code Netbeans created: http://pastebin.com/ERwY4rBC
It's not that the code is completely unusable but I wanted to try it manually.
The GroupLayout looks nice, but the Oracle site says it's mainly for the use for GUI tools. So, using GroupLayout would be not "Java like" or how do I have to understand it? Or is there even a better way to achieve this without GroupLayout?
Thanks!
So, using GroupLayout would be not "Java like" or how do I have to understand it
GroupLayout is to put it simply really hard to hand-code, and results mostly in a lot of code. But it is not "not Java like", it is just not something you want to do by hand, and the code afterwards is hard to read as it is rather verbose.
What you try to achieve (according to the screenshot) is easily achievable using some 'nested layouts'. If your main panel uses a BorderLayout where you put the left, resizable panel in the BorderLayout.CENTER and the other, non-resizable panel in the BorderLayout.EAST you will obtain the desired resize behavior.
Then you just have to decide which LayoutManager to use for those individual panels. I think that both the BoxLayout as well as the FlowLayout will do just fine.
Do yourself a favour and use MigLayout for all your layout needs. It is especially convenient for coding UI by hand.
There is a WebStart application on their site that demos different layout situations with code samples provided.

Resize gui elements by dragging the corners

I have a swing project and noticed that the elements inside the JFrame is statically located inside the JFrame and I do not have the option of resizing the elements as I want to.
My gui app inside the designer window looks like the following:
I want to resize the button (E.g.) by dragging the corners of the button, but I am not allowed to?
As you can see on the following picture, the dragging is not allowed per pixel, but only per section in the JFrame:
How can I disable the static placement of the elements/Enable the self-dragging of elements inside the designer window?
Most likely you will need to disable the LayoutManager. On Netbeans, Setting this to null would provide you full control over the location and dimension of the child elements, so I am assuming that something similar should work here (although you seem to be using Eclipse, if that is the case, please state what plugin you are using).
It is to be noticed however that usually you want to have a layout manager taking care of how your components are rendered. I would recommend you take a look here for some more information on layout managers prior to removing them completely.
Setting size and position of components in a GUI should be left to the JRE at run-time, using:
The preferred size of the component (with current content, in the current PLAF).
The borders/padding/insets set to the component.
The layout used.
The component spacing defined in the layout constructors.
The layout constraints used when adding components to the layout (especially important to e.g. BorderLayout and the much maligned GridBagLayout, as well as many 3rd party layouts).
Generally, complex effects are created by using a nested layout.

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.

How to do smooth scrolling for lists in Swing

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);

Categories