开发者

JButton with HTML named action and a mnemonic

I have a JButton that is constructed using an Action and this action has a name that is contains html.

I then开发者_如何学Python go about setting the mnemonic on the JButton by first parsing out html to get the first character in the name.

For example, the JButton name might be "<html>Test<br>Button</html>", so after parsing the html the mnemonic key should be "T".

So now when the JButton is rendered I can push alt-T to activate the button, however the underline mnemonic indicator on the T is not present.

Would anyone know a way to get this to occur?


It is unclear to me what you mean by "setting the mnemomic on the JButton by parsing html". The mnemonic can be set on a JButton by calling the method setMnemonic in the JButton class. I tried below piece of code and when I press Alt+P I get the message I am pressed printed in the console.

public class HTMLButton extends JPanel implements ActionListener {
    JButton     b1;
    public HTMLButton() {
        super(new BorderLayout());
        b1 = new JButton("<html><b><u>P</u>ress</b></html>");
        b1.setMnemonic(KeyEvent.VK_P);
        b1.addActionListener(this);
        add(b1);
    }

    public void actionPerformed(final ActionEvent e) {
        System.out.println("I am pressed");
    }
}

Also see the section How to Use HTML in Swing Components in the Java tutorial.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜