aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes CTRL Mask on JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "CTRL Mask on JButton" Watch "CTRL Mask on JButton" New topic
Author

CTRL Mask on JButton

Ikram Soomro
Ranch Hand

Joined: Oct 20, 2001
Posts: 42
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
R.Joe
Ranch Hand

Joined: Jun 16, 2004
Posts: 31
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 ]
Ikram Soomro
Ranch Hand

Joined: Oct 20, 2001
Posts: 42
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.
R.Joe
Ranch Hand

Joined: Jun 16, 2004
Posts: 31
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);

}
}
Ikram Soomro
Ranch Hand

Joined: Oct 20, 2001
Posts: 42
Hello Ravi
Thank you for sending me right solution;
It worked...

Ikram Soomro
(SCJP,SCWCD)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CTRL Mask on JButton