English 中文(简体)
Java Swing JButton
原标题:

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:

  1. Add a MouseListener which changes an enum variable, let s call it state on your extended JButton
  2. 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);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签