Is it Possible to Remove the Shadow on a JButton Icon? - java

My questing is pretty much already asked in the title. When you click a (Java Swing) JButton you get a type of hover effect or a "shadow" over the icon of the JButton. Is it possible to remove this shadow to make the button icon appear in the same way as it does when not clicked?
Thanks in advance!

You could call setRolloverIcon with the result of getIcon.

You can try button.setBorder(null); This will remove all of the graphics for the button and therefore the button will look the same whether it has been pressed or not.

Related

Create a JLabel dynmaiclly

Hiļ¼Œthere is a problem in my java application, the text in my Jbutton is too long so it only show"..." on the button. Now I want to add some component to help show the actual text on the button.
What method can I use to solve such problem rather than adjusting the text font or button size?
Thank you
Instead of a label, you should add a tooltip to the button:
jb.setToolTipText("The full text of the button");
A small downside is that this tooltip will also be shown if the button text is fully visible, and in that case the tooltip doesn't provide any additional information, which will be confusing. I don't know off my head how to solve this, but it's definitely possible.

how to make a JButton not clickable in java?

I'm looking for a substitute of .setEnabled(false), so that the button is not clickable.
I have read that I could remove the ActionListener from the button, but I just want the specific buttons to be not clickable, and do not know how to do it anyway.
Or how can I get rid of the "selected" effect after clicking a button?
Because in my application I have a grid of JButtons that are used as icons
Then don't use buttons, use a JLabel which also supports an Icon.
Or maybe use a JList which can also support a grid.
Or if you use a JButton then you need code like:
JButton button = new JButton(...);
button.setBorderPainted( false );
button.setFocusPainted( false );
I assume that the problem is how the button looks grayed out, because setEnabled really is the way you disable a button - which just means making it not clickable (or did you want it to respond to keyboard input?). If that's the case, then you can change the way it looks by using html:
button.setText("<html><font color=black>3</font></html>");

how keep visible ToolTip while mouse is over it?

Is there any way to keep a JToolTip visible while mouse is over the component who owns it, or the tooltip itself?
have you try using setToolTipText() method. Here btnNext is JButton.
btnNext.setToolTipText("Next");
ToolTipManager.sharedInstance().setDismissDelay(Integer.MAX_VALUE);

Customize JButton style

I'm working on a project. I want to add a toolbar to the software so I put some buttons in a panel . However the default button style doesn't meet my need. I want the button to have the following effects:
When the mouse doesn't hover over the button, the button should looks like a JLabel. The icon in the button just looks like an image on the panel, i.e. all we can see is the icon in the button and other things are transparent.
When the mouse hovers over the button, the button's border appears. It looks like a real button.
Example: Just like the buttons on the eclipse's toolbar.
Why not use a JToolbar instead of a JPanel?
I got it. The answer to my question is the setContentAreaFilled() method. When the mouse hovers over the button, call the setContentAreaFilled(true). Otherwise call the setContentAreaFilled(false). Here is a relative code: link text
So, you want to customize your JButton renderings ?
First, for an all-inclusive soluition, you can take a look at existing LnF like Substance (obviously, it's a far too powerful solution for your need, however it may give you some inspiration).
Then, if you want to solve that by yourself, you'll have to override the paintComponent method.
For that, the first move is to subclass JButton.
Then, in your subclass, start by redefining the paintComponent(Graphics) method.
Notice that if all that is overcomplicated to you, you can also take a look at setBorderPainted(boolean) method.
Extend JButton and:
Just add an Icon instead of Text to
the button.
Add MouseMotionListener to capture
hovering to show/hide border.

Keeping selection after clicking a JButton to style text

I'm making a fairly simple text editor, and I have a question about my style buttons. When I highlight text and click my "bold" button, the text bolds as expected, but my selection is not longer visible. I can still unbold the selection, italicize it, or underline it, but you just can't see what is selected. So, I'm wondering if there is a setting that will allow me to click the button, but keep my selection? I tried a JMenuItem instead of a JButton, and that seemed to work, but then it made my toolbar look quite bad. Sample code below.
//frame and pane creation up here
JToolBar tool = new JToolBar();
JToggleButton boldButton = new JToggleButton("Bold");
boldButton.addActionListener(new StyledEditorKit.BoldAction());
tool.add(boldButton);
Any help is appreciated.
So, I'm wondering if there is a setting that will allow me to click the button, but keep my selection?
boldButton.setFocusable( false );
As you noticed, the selection is still there but clicking on the toolbar button removes the focus from the text pane and hides the selection. You need to set the focus back using requestFocus. However, you will need to write your own action listener to add the focus code - you could extend BoldAction to do this.

Categories