This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CTRL Mask on JButton

 
Ikram Soomro
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ravi
Thank you for sending me right solution;
It worked...

Ikram Soomro
(SCJP,SCWCD)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic