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.


Recommended Answers

All 2 Replies


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 "" property in the UIManager properties

UIManager.put("", 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: if you want to wade through it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.