I was reading about the cross-platform look & feel ie the metal L&f that looks the same across all platforms. I wanted to know what will be the shortcut keys for this l&f. I'm facing an issue where in MAC OS, if I have to copy/paste I have to use window shortcuts(ctrl + v/c) for copy/paste actions.I'm unable to use mac shortcuts (cmd+v/c). I'm wondering if it could be due to the l&f. Please help.Thanks in advance.
Asked
Active
Viewed 163 times
1
-
It makes sense that the cross-platform PLAF would use the same shortcut keys across the platforms. Given Windows / *nix machines have no OS X 'command key', why would the PLAF try to use it? – Andrew Thompson Mar 24 '20 at 08:15
-
For windows I'm using the Windows_L&F,for linux and mac I'm using Metal_L&F. So you're saying since there is no cmd key on windows. PLAF is using ctrl to make it work across platforms. I see there's Aqua_L&F for mac but it has some issues with the jdk version I'm using. That's why I was forced o use Metal. Any suggestions how can I achieve this? – wazza Mar 24 '20 at 08:20
1 Answers
0
Yes, the look and feel can do this. One thing you can do, which I hope somebody has a better answer, is to set the input map for the components you want to behave differently. Here's a way to use the platform default input map for a text field.
public static void startGui(){
try{
LookAndFeel aqua = UIManager.getLookAndFeel(); //aqua
UIDefaults def = UIManager.getDefaults();
Object b = def.get("TextField.focusInputMap");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
UIDefaults def2 = UIManager.getDefaults();
def2.put("TextField.focusInputMap", b);
}catch(Exception e){
e.printStackTrace();
}
JFrame frame = new JFrame("title");
JTextField field = new JTextField();
frame.add(field);
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
EventQueue.invokeLater( ()->startGui() );
}
If you run that, then cmd+v should paste, but if you commend out the line def2.put(...)
then ctrl+v will paste.
Verified with jdk11 on OSX.

matt
- 10,892
- 3
- 22
- 34
-
In this approach I have to set the input map for each component. Trying to avoid this. If I use aqua l&f , then would the mac shortcuts be enabled by default?I don't know much about this since it's my first time testing my code on mac. – wazza Mar 24 '20 at 10:04
-
@wazza yes, if you stay with the aqua look and feel you do not have to set the input map, it should be the default (I suspect it is native vs cross-platorm) input maps. It seems cumbersome to have to set the input map for all of the components but I could not find away around it. You could loop over all in the XYZfocusInputMap and set them to the default from aqua, otherwise maybe somebody will come up with a better way. – matt Mar 24 '20 at 10:54
-
I'm thinking of using the default L&F for mac. Do you think that will fix this issue? – wazza Mar 24 '20 at 11:24
-
Absolutely, If I don't set my look and feel then cmd+v / cmd+c pastes and copies respectively. – matt Mar 24 '20 at 12:11
-
@wazza *In this approach I have to set the input map for each component.* - as you were already told in your last question on this topic (https://stackoverflow.com/a/60724872/131872) you do NOT need to set it for every component. Changing the parent InputMap will change it for all instances. – camickr Mar 24 '20 at 14:24
-
I meant for each swing component ie Table,Panel, TextField etc. If changing the L&F doesn't work then I guess I have to set the input map. – wazza Mar 24 '20 at 14:29
-
@wazza what do you actually want? If you don't change the look and feel, then you'll get the platform look and feel and then the controls you expect will work. You've got a couple ways to set the input map if you do want to change your LAF. – matt Mar 24 '20 at 15:06
-
@matt I'm trying the Mac Os default L&F. Will test it out a bit to see if there's some issues or not. If that works then I think the shortcut key issue will also be resolved by using the default L&F. If there are some issues with using the default L&F, then I'll have to make do with Metal. – wazza Mar 25 '20 at 08:02