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/
Related
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.
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.
I've used
chkBox.setIcon();
chkBox.setSelectedIcon();
chkBox.setDisabledIcon();
chkBox.setDisabledSelectedIcon();
to set custom icons for my JCheckbox. But now, if the focus moves to one of the checkboxes, there is no border shown around them or anything else, which tells that the checkbox has focus.
Does anyone know, how to give some feedback when a customized checkbox has focus?
Thanks
Your problem definitely depends on Look and Feel (L&F) that you are using in your application (if you don't setup one - i guess you are using MetalLookAndFeel?).
Anyway, there might be a lot of solutions:
Check that your JCheckBox is actually focusable and focus painted. Be aware that some L&F might switch off focus painting - check checkBox.setFocusPainted() method.
If you are not satisfied with default focus painting - you might want to create your own CheckBoxUI that paints a better focus indicator. That requires some basic knowledge in UIs creation though.
If you want to paint focus indication straight on the check icon itself you can create your own Icon-based implementation that paints it together with current check state. I have posted a custom Icon example in other topic about state-dependant icon if you want to see a real example.
There might be other solutions but they depends on the L&F you are using...
You can use this ready-to use checkbox alternative:
http://codetoearn.blogspot.com/2013/01/swing-fantasy-checkbox-with-customized.html
I have added the line to set look and feel to system. This is found to work because the scroll bars have changed.
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
But when I set a tooltip to my JTabbedPane's tab, it does not show the system's look and feel for tooltips. Why is this? Screenshots are below.
System's look and feel:
Java's look and feel(?):
I want to set my tooltip to look like the system's which is in the first screenshot. How do I do this?
Much work without the complete effect:
for getting the background you need a custom implemenation of ToolTipUI: basically extend BasicToolTipUI and take over the painting
register the custom ui delegate with the UIManager so that it is used instead of the default
theoretically, you can achieve the rounded corners of the window (the one containing the tooltip) by using a shaped window (public api in jdk7, a half-official workaround available for jdk6), you would need a shaped window. Practically, there is no way (except extreme hacking) to make the ToolTipManager use that window
I'm reasonably new to java GUIs and have been using netbeans to help me out.
I've set up a jButton such that when clicked its label changes. My issue is that the size of the button refuses to remain fixed despite setting a maximum and minimum size as well as the setting the preferredSize method. Do I need to change my layout? Should I go through and place each button on a panel or is there a simpler way?
I feel like this should be an easy problem to fix yet I've been at it for over an hour now. I'd appreciate any ideas. Thanks
If you are new to Swing don't use a GUI builder as you will run into all sorts of issues like this one.
It sounds like your Layout is preventing resizing. Make sure you are using the correct Layout Manager for your designed look. Double check any constraints that you have set for the layout. You could experiment with a different layout manager like FlowLayout to check to make sure your setPreferredSize () calls are working correctly etc.
There are a number of ways to handle this:
A clean and easy way would be to create image icons for the different buttons, making them the same size. This lets you completely control what they will look like.
A quick-and-dirty way to do this is the add spaces until the buttons are approximately the same size. This won't be perfect because the fonts that appear on JButons are typically not fixed-width.
The 'proper' Swing way would be to use a custom Layout. For instance, if you use a GridBagLayout to arrange your components, and set the 'weightx' and 'weighty' for the JButton to 1.0, then it will take up as much space as possible, which will keep it the same size.