java - shortcut in jbutton tooltip not displaying in other themes apart from matalic -
We need to show key board shortcuts in the Jebton tooltip, we have added an action map and all required code for the toolmap but Shortcut If we change the application and feel from metal to system or any other form and feel, then the short cut tool is not showing in tool tip.
Below is the example program, which shows the 2J button with shortcuts, shortcuts are displaying properly with the appearance and feel of the metal, but if I use any other form and If I feel like a system or a motif, the shortcut in the J button tool tip has been stopped. For another example to look and feel for the example behavior below, please see and feel the metatics, for example and enable and feel the system or shape which is currently commented:
< Code> import java.awt.gridLayout; Import java.awt.event.ActionEvent; Import java.awt.event.InputEvent; Import java.awt.event.KeyEvent; Import javax.swing.AbstractAction; Import javax.swing.Action; Import javax.swing.JButton; Import javax.swing.JComponent; Import javax.swing.JFrame; Import javax.swing.KeyStroke; Import javax.swing.UIManager; Import javax.swing.UnsupportedLookAndFeelException; Public category test {public static zero main (string array []) {try} {UIManager.setLookAndFeel ("javax.swing.plaf.metal.MetalLookAndFeel"); // UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ()); // UIMNanger. Settleke Endfell ("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); } Catch (Clasnotfoundive Express E1) {// Todo Auto-Generate Cache Block e1.printStackTrace (); } Grip (InstantiationException e1) {// TODO Auto-generated Cache Block e1.printStackTrace (); } Hold (IllegalAccessException e1) {// TODO auto generate generated blocks e1.printStackTrace (); } Grip (unsupported lock and field expansion E1) {// Todo Auto-Generate Cache Block e1.printStackTrace (); } Jeffre Frame = New Gefram ("Keystroke Sample"); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Last Button Button A = New Pocket ("One Button"); ButtonA.setMnemonic ('one'); Frame.setVisible (true); Button A. Settling text ("One button"); The last button button B = new pocket ("another button"); ButtonB.setMnemonic ('B'); Frame.add (buttonA); Frame.add (buttonB); Button B.setToolTipText ("Another Button"); Action Action = New Object Action () {Public Zero Action Perfid (Action Event E) {if (EGS Source). Equal (button A)) {System.out.println ("Button A"); } If (e.getsource (.) Equal (button B)) {System.out.println ("buttonB"); }}}; ButtonA.addActionListener (action); ButtonB.addActionListener (action); // button ("ALT + A"); ButtonA.getInputMap (JComponent.WHEN_IN_FOCUSED_WINDOW) .put (KeyStroke.getKeyStroke (KeyEvent.VK_A, InputEvent.ALT_MASK), "left_button_pressed"); Button A.Actactimap (). Put ("left_button pressed", action); // button B ("CTRL + Shift + A"); ButtonB.getInputMap (JComponent.WHEN_IN_FOCUSED_WINDOW) .put (KeyStroke.getKeyStroke (KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_MASK), "right_button_pressed"); Button B. GateActionMap () Put ("right_buttons", action); Frame.setLayout (new grid layout (2, 2)); Frame.setSize (400, 200); }
}
Display the accelerator string in the tooltip Which is implemented only by default in the metal (I tried all other LF "only") This behavior is defined in the MetalToolTipUI
class, which is responsible for setting up and displaying tooltips.
Good luck UIManager
set a ToolTipUI
class allowing for you. You can use metal for any other LAF:
Put UIManager.getDefaults () ("ToolTipUI", "javax.swing.plaf.metal.MetalToolTipUI");
The visual style of tooltip (background color and fonts) has been optimized for the ALF, but there can be cases where there will be no visible mail. In this case, you have ToolTipUI
or its subcategories to suit your own needs.
In addition to that, you may want to
check UIManager subclass .getDefaults () getBoolean ("ToolTip.hideAccelerator") .;
returns false
to a LAF if it is not and the use of tooltip UI respects this property, the accelerator will not be displayed. You can set it to true
in any case.
Comments
Post a Comment