simple questions, hopefully simple answers:

How do i give a JButton the capability to accept a right click?

When a JButton is being clicked it changes to a darker color, is there a way to make it a completely different color?

i cannot find these answers in the API or SUN tutorials, but if you can, a link or sample code would be appreciated.


8 Years
Discussion Span
Last Post by Ezzaral

Right-click is easy to handle

jButton1.addMouseListener(new MouseAdapter(){
    public void mouseClicked(MouseEvent e){
        JOptionPane.showMessageDialog(null,"Right-click stuff");

Changing the button select color is easy if you are wanting to change it for all buttons in your program - even the ones that appear on dialogs and such. For that you can set "Button.select" property in the UIManager properties

UIManager.put("Button.select", Color.BLUE);

If you want only specific buttons to have the different selected color, you'll have to install your own button UI delegate that extends BasicButtonUI and override the

protected void paintButtonPressed(Graphics g, AbstractButton b)

method and perhaps delegate all other operations to the current look and feel ButtonUI, which may get tricky if you aren't used to pluggable look and feel UI delegation model. There is a complete custom ButtonUI implementation here: http://www17.homepage.villanova.edu/william.pohlhaus/is/doc/Java%20Look%20and%20Feel.htm#_Toc49238503 if you want to wade through it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.