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

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.

Related

Swing - Custom JButton messes with other components at rollover

On a JPanel I have a few buttons and JScrollPane containing a JTable.
One of the buttons is custom made, I have overridden the paintComponent method to draw an image.
However, if I set the contentAreaFilled or isOpaque properties to false (so that the background is not displayed) whenever I roll over the button the content of the scroll pane is hidden (a grey image appears inside, instead of the table contents). I tried using different layouts for my panel, but got the same result. The only fix I found is setting rolloverEnabled to false for my custom button, but that is not really an option for me, as I want the button image to change at rollover.
The weird thing is, it only happens when I set one of those two properties mentioned above to false. The button acts perfectly if I do not hide it's background. Just out of curiosity, I tried the same on my other two buttons, which are just JButtons with nothing customized, except their size, and it had the same effect on the scroll pane, so I'm pretty sure it doesn't have anything to do with the implementation of the custom button. Has anyone encountered this problem or know anything that may help me with this?

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/

Swing JCheckbox: Focus with customized icons

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

How do I change the tooltip look and feel to the system's?

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

Java Swing group of Jbuttons without raised portion

I wanted to know how to display a group of JButtons to look like smooth panel without raised portion.
thanks
button.setBorder(null);
You may want to look at some of the other "setXXX" method that control painting as well.
I've often just used standard JLabels and added mouseListeners to make them clickable. Alternatively, you could get more advanced and create your own ButtonUI class if you want to really fine-grained control over the rendering of the buttons.
If you want the buttons to be in a row, you can put them in a JToolBar and set Rollover to true. This will make flat buttons that, with mouse over, look raised.

Categories