I want to create button with custom look and feel. I have got different images to be set as background of button for normal, mouse over, mouse click, button disabled etc. I have created my own class, extending javax.swing.JButton and overrides paintComponent method. How can i change my button background for all the above given states.
In addition to Steve De Caux's answer, you can:
Add a MouseListener which changes an enum variable, let's call it state on your extended JButton
In your overridden paintComponent take into consideration the current state and paint different backgrounds. Like
if (!getModel().isEnabled()) {
} else if (state == ButtonState.MOUSE_OVER) {
} else if (state == ButtonState.MOUSE_CLICKED) {
}
JButton has a series of simple set methods for rollover, pressed, selected, disabled and disabled selected states - for example
button.setPressedIcon(new ImageIcon("images/button-down.png")
the other methods are :
button.setRolloverIcon()
button.setSelectedIcon()
button.setRolloverSelectedIcon()
button.setDisabledIcon()
button.setDisabledSelectedIcon()
...have fun !
By the way, O'Reilly has a fun book called Swing Hacks with lots of little goodies for playing with swing : Swing Hacks
You could create a custom button UI delegate. This blog entry: http://blog.elevenworks.com/?p=4 has an example for a custom tabbed pane, but the principle is the same. Extend BasicButtonUI, implement the custom rendering your want for the button, and then call setUI() on the button.
This will probably take longer to implement than using the existing button API methods to change the appearance, but it gives you a lot more control.
ImageIcon icon = new ImageIcon("images/icon.gif");
JButton button = new JButton(icon);
Related
Java provides a simple method to disable its buttons with button.setEnabled(false);
However doing so will grey out the entire button thus affecting the visibility of the text and images on the disabled button.
Question: Are there any available methods/ways in Java to allow buttons to be disabled yet does not grey out?
Certainly, I am not expecting manual tweaking on the ActionListener of the buttons to achieve this.
Companies spend millions of dollars to develop a UI can is common and can be used by all users.
How is the user suppose to know that the button is disabled if there is no visual indication?
I am not expecting manual tweaking on the ActionListener of the buttons to achieve this.
Why? What is wrong with this approach? It is better than trying to create a custom LAF for all platforms your code might run on.
Anyway (rant finished) you could use a custom ButtonModel:
button.setModel( new DefaultButtonModel()
{
#Override
public boolean isArmed()
{
return false;
}
#Override
public boolean isPressed()
{
return false;
}
});
Should work for all LAF's and the button won't be painted as a pressed (which I suppose is better than just removing the ActionListener).
This is a little bit of work, and may not be the best coding style or solution, but it might satisfy your requirements. I'm going to assume you have an icon for when the button is enabled and another for when it is not.
Basically, create and manage your own, virtual "disabled" state for the button (but don't touch the built-in enabled or disabledIcon methods).
In the code that manages the enabling/disabling, when you disable "virtually", set the regular icon ( setIcon() ) to be your disabled Icon graphic, and set a boolean to reflect the virtual "disabled" state of the button.
When you "enable", use setIcon() to return the default Icon, and set the boolean to reflect "enabled".
Then, when the button is clicked, in the ActionListener, you can inspect the boolean and do nothing if the boolean says the virtual state is "disabled".
How to create a spoiler effect in java swing like in HTML+JS.
For example, there are a few buttons "show" and when you click on one of them, showing under it image or text:
What method can be implemented like this?
I assume this can be implemented using JTree but will must drawing own components.
You can use MySpoiler.setVisible(true) within the "show" button callback.
If you want this to toggle between visible and non-visible, you will have to change the listener after each click to reverse the visibility of the image. The image itself can be placed within a swing JLabel.
I have a left Panel with multiples Jlabels which i use them as buttons to change a Main Panel's content which is layouted with a CardLayout.
I cant work perfectly with these events:
mouseEntered : to make highlight effect to the jlabel
mouseExited : to take off the highlight effect.
mouseClicked : to change the content of the main Panel and start some threads
The problem here that can't found an event or a method tell me that another Jlabel has been clicked so i can stop my threads started in the mouseClicked event,
OR
an event or method tell me that a JPanel in the CardLayout has been displayed or hidden.
Your problem is not finding an appropriate event. I think you are doing this using a visual GUI builder and expect to solve everything out-of-the-box. It's not going to work that way, you will need to write some real code. For example, write a method that you will call from the mouse click listener of each of the three JLabels. Thus you will have arranged for this method to be called for each JLabel click. Then in the method do the appropriate handling. This is just a rough outline, you haven't provided much detail to give any further advice.
It sounds like you need FocusEvents and FocusListeners. These are supported by all JComponents like JPanel, JLabel, and JButton, such as by calling addFocusListener();
Basically a FocusListener can tell you when a JComponent gains focus (such as by clicking on the JComponent) and when it looses focus (such as by clicking on a different JComponent).
Refer to http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/FocusListener.html for further information
After what I learned from my previous question, I would like to use a texture to paint the text on an extended JButton while it's being pressed. The first step was setting up the button's ChangeListener and stateChanged method; I have these working and can set the foreground color in the method, so that the text will be one color while the button's pressed and another while not.
Building on this, I replaced the setForeground call with the drawString code I used for my toggleButtons. It works, but immediately after the text is drawn it's overwritten by the button being automatically repainted. I tried throwing the code in a "while (model.isPressed())" loop, but that had some pretty terrible results (system hang). How would I go about manually repainting my button, so that it's only redrawn during the stateChanged method?
It seems to me that you are going the wrong way to change the look of your button. I think it would be easier for you to create a class that would handle the look and feel of your button, instead of manually handling the drawing parameters of your button inside the button's code. Blocking the repaint() calls isn't really the way to go I believe in your case.
I would personally create my own ButtonUI implementation that would handle all the paint rules (foreground color based on button state for instance), then I would call the setUI on the button, specifying an instance of this new ButtonUI as a parameter. If you don't want to handle all the drawing stuff, you can always use your new class as a proxy to the button's already existing UI handler (through JButton's getUI() method), and make changes only where you need them (I haven't tested it myself, but I'm pretty sure it would work just fine).
Of course, this represents more coding for you, but it would localize your look and feel handling in a single class, and it would fit in Swing's way of working. There are a few resources on the web to get you started (here, here and here).
I have a class that extends JButton because the custom look and feel I'm using ignores the isOpaque() call. What I mean is, the background of the button is rendered even though I have called setOpaque (false) on it and all parent panels. I have confirmed that this is an issue with the LAF from the companies design people so there is nothing I can do but extend the class.
So my question is, how can I implement the paint() method to not render the background and just the button icon?
Cheers
SOLVED: The answer I was after in case anyone is interested was to use button.setContentAreaFilled(false);
Painting is done by three methods: paintComponent, paintBorder, and paintChildren. They're called in that order and it's the first that paints the component's background. If you overload that one and leave the other two, you should be fine.