Forums Register Login

CTRL Mask on JButton

+Pie Number of slices to send: Send
i want to apply CTRL Mask on JButtons (i.e. i have Copy and Paste Buttons not menues), i want to apply CTRL+C for copy and CTRL+V fro paste just like we do in menus.


SOOMRO
+Pie Number of slices to send: Send
Hello Sir,
u can do the ctr mask on your button like this
JButton b1 =new JButton("Copy");

b1.setMnemonic('C');
when u pressed alt +c it works
[ June 25, 2004: Message edited by: Ravi Lekhwani ]
+Pie Number of slices to send: Send
Hello Thank You for your reply.

I know that setMnemonic can used to apply shortcutkey but it is with ALT Key

b1.setMnemonic('C'); = ALT + C

my question was how can i apply CTRL + C on JButton.
+Pie Number of slices to send: Send
Hello Ikram,
First Of all i m sorry 4 not sending the right solution, Now I made a code for you which gives u a exact solution ,just have a look into it, Please give me a reply weather it solves your solution or not

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class Test4 extends JFrame implements ActionListener
{
JButton but1,but2;

public Test4()
{
getContentPane().setLayout(new FlowLayout());
but1 =new JButton("Close");
but2 =new JButton("Background");

but1.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
if (ke.getKeyCode()==KeyEvent.VK_C && ke.isControlDown())
{
dispose();
}
else if (ke.getKeyCode()==KeyEvent.VK_B && ke.isControlDown())
{
getContentPane().setBackground(Color.cyan);
}
}


});

getContentPane().add(but1);
getContentPane().add(but2);
but1.addActionListener(this);
but2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==but1)
{
dispose();
}
else if(ae.getSource()==but2)
{
getContentPane().setBackground(Color.cyan);
}
}

public static void main(String[] args)
{
Test4 tst = new Test4();
tst.setVisible(true);
tst.setSize(300,400);

}
}
+Pie Number of slices to send: Send
Hello Ravi
Thank you for sending me right solution;
It worked...

Ikram Soomro
(SCJP,SCWCD)
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3915 times.
Similar Threads
Cut Copy Paste n SelectAll in JTextPane
JMenuItems tricks for displaying short cut text
disabling windows paste for text components
Need to restrict ctrl+c, ctrl+v, ctrl+x
How to enable CTRL+C to perform action instead of coping to clipboard?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:19:07.