how to make a JButton not clickable in java? - 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>");

Related

Is it possible to get a JLabel to appear on the same GUI after a button click?

I have a JList with a setSelectedIndex(-1). I have a JButton , "More Details" which only works if an option is selected from the JList. If no option from the JList is selected and "More Details" is pressed, nothing happens, which is expected. But as a user you want to know when that button should be pressed.
A simple JLabel with the instruction saying "Select an option to obtain more details" would suffice. However, is there a way to get the JLabel to appear on the gui with the instruction after "More Details" is clicked? I do not want the instruction to be on the GUI all the time, as the "More Details" JButton will be rarely used. As a result of this I was wondering if it is possible to place text on the same GUI the button is located on with the instructions?
It might depend a little bit on the LayoutManager you use, but all JComponents have a method setVisible(boolean). With this, you could in your ActionListeners for the JButton just call myLabel.setVisible(true).
This is quite possible. You could actually add your JLabel directly in your GUI and call setVisible(false) before showing it. When you hit the button and you want to show it, call setVisible(true)
Also, another way you can do this... Easier in many ways - is to use JOptionPane to pop up a warning / error message
nothing happens, which is expected. But as a user you want to know when that button should be pressed.
Use ListSelectionListener to monitor when the selection changes in the JList and enable/disable the button as required.

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

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.

How do I "combine" JButton with JComboBox?

How do I "combine" JButton with JComboBox and place it on a JToolbar, as shown in this image:
?
You'll want to use a custom renderer I suspect, and have it display clickable buttons (with the appropriate actions attached, etc).
Just create regular combo box and put image as an item. I did it once. Unfortunately I do not have a source code here but as far as I remember it was not a problem. You have to implement your custom 1ListCellRenderer. Its methodgetListCellRendererComponent()` should return for example Label with your image.

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