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.

thanks

Recommended Answers

All 2 Replies

MouseListener

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.

Be a part of the DaniWeb community

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