Horizontal placement of buttons in BoxLayout depending on OS - java

I am placing [OK] and [Cancel] buttons horizontally in a Box (which has BoxLayout). Later I add this Box to BorderLayout.PAGE_END in content pane of JDialog. This works perfect in Windows, so [OK] is to left of [Cancel] - just like I have added.
When I test in Linux, the placement of buttons is same as Windows - [OK] to left of [Cancel]. This, again, follows from code.
However, in Linux (Ubuntu), the default placement for buttons is [Cancel] to left of [OK]. This contrasts with my code and placement of buttons.
So, my question is : Whether Java has some kind of constant - say OS_PLACEMENT, which can be set somewhere which will make [Cancel] button appear to left of [OK] in Linux? I am asking this because I know Java supports constants for RTL and LTR layout. Thus, I thought, there might be some constant(s) related to this kind of placement.
I wish to mention that I do set L&F of my Swing app to System L&F. Following is my first line in main()
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

When you say Linux (Ubuntu) you mean Gnome, right? The button placement by default (OS) of Gnome (Gtk) is placing Cancel button to the left.
You can try to change the Look and Feel of Swing :
How to Set the Look and Feel

No, Java will not do this automatically for you.
Consider using the free "JGoodies Form" library. It has a class called "ButtonBarBuilder" which will lay out buttons in the correct order according to the user's OS.
Otherwise you must manually inspect the value of System.getProperty("os.name") and lay out the buttons according to the value.

Some LayoutManagers automatically do this for you, for example DesignGridLayout and MigLayout detect the OS the application is running on, and based on it, will choose the right position for OK and Cancel (and also other specific buttons).
For example, with DesignGridLayout you would do:
DesignGridLayout layout = new DesignGridLayout(myDialog);
layout.row().bar().add(okButton, Tag.OK).add(cancelButton, Tag.CANCEL);

Related

Java Swing JDialog - how to modify after it is displayed? [duplicate]

I am developing a Java Swing-based application with different perspectives. For the "main menu" perspective I do not want the window (JFrame) to be decorated, while in other perspective I do want the window to be decorated. In other words, I need want to change the decoration attribute dynamically.
I have tried to use setUndecorated(false) and setUndecorated(true), but I seems I can only set this once, before actually showing the window.
Is there a way to achieve this?
From the javadoc:
Disables or enables decorations for this frame. This method can only be called while the frame is not displayable.
Therefore, once the JFrame is packed and/or displayed, you can no longer change that value. If you want to change the undecorated state of a JFrame you will need to dispose() it first, then change the state and eventually make it visible again.
After all, I had to take a different approach.
The former solution did work, as I stated in my last comment.
However, it was showing the default LAF window decoration, while I was using a different LAF.
So the result was graphically inconsistent with the rest of the LAF. Finally, I came with the right solution, I used setUndecorate(true) for my frame. Then, when I had to change my perspective to one using decorations I simply had to use the following code
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
And when I had to revert to the non decorate perspective, I use
contentPane.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
This approach didn't need to dispose the window and show it again (which actually produced a brief but still visible hide/show of the window)

Java Swing - How to make the JButton's boundaries invisible?

In the screenshot below, you can see four buttons in the selected area. I want to mimic this kind of buttons in my GUI application. Each of these buttons has an image on them (play,stop, forward, rewind). I can use the icon property of the button to add an image to it.
When the user hovers the mouse pointer over a button, three things happen:
it changes color- I don't need this feature.
It displays a tool tip. I know how to do it by using the tooltip
text property of a button.
Most importantly, these buttons don't have a border around them,
That is their bounds are not visible at all. It's just the image
which is visible. I want to do something like this. But when I add
an image to a button, its border does not go away (I mean it's
bounds are clearly visible in the form of a line - as you can see in
the second image)
So what property of an button should I manipulate, or what method should I use, to make its borders (and every visible trace of the button except the image present on it) invisible?
A border is painted when setBorderPainted is set to true, otherwise not:
setBorderPainted:
Sets the borderPainted property. If true and the button has a border,
the border is painted. The default value for the borderPainted
property is true. Some look and feels might not support the
borderPainted property, in which case they ignore this.
Note that some look and feels may ignore this property.
Update:
The default look and feel is called CrossPlatformLookAndFeel. This is not a look and feel but an indicator of the default one. What you get as default depends on the platform you are using. See How to Set the Look and Feel for details. I personally loke the Nimbus Look and Feel, but I have encountered some problems with it. I am not sure if it respects setBorderPainted, but I will not be surprised if it does not.

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.

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

How to make Java tabbed panes display tabs from right to left?

In C#, to make the tabs go from right to left, I set RightToLeft to Yes and RightToLeftFormat to true. How do I do the same thing in Java? How do I set a JTabbedPane's tabs to display from right to left? Please see the image link below to see what I mean by displaying tabs right to left.
http://lh4.ggpht.com/_1bcR6vegNNc/TPDRekNVqWI/AAAAAAAAAB8/TwCqgajEuoI/s640/AdminDashboard.jpg
I assume by "tabbed pane" you are referring to javax.swing.JTabbedPane.
To answer your question: AFAIK there is no easy way to do what you want. The BasicTabbedPaneUI(or the TabbedPaneUI which is defined by your application look and feel) which is responsible for painting the tab area of the tabbed pane would require some changes to be able to do what you need.
If you do not have the time to write your own UI, you could look for solutions in the web. I doubt that there would be a ready to use solution to your question though, but then again, who knows?
Hope this helps you a bit.
Then i'm refer from "How to Use Tabbed Panes" to create a new project based by JTabbedPane component...
It's enough to use "setComponentOrientation" method to change orentation of JTabbedPane.
link text

Categories