|
-
Jul 23rd, 2007, 06:17 PM
#1
Thread Starter
Hyperactive Member
jTextField Right Click sensor
How can I execute special code when the user right clicks into a jTextField control? How can I sence that right click?
Last edited by xxarmoxx; Jul 23rd, 2007 at 06:18 PM.
Reason: I have bad grammar
-
Jul 23rd, 2007, 11:45 PM
#2
Re: jTextField Right Click sensor
Try this code:
Code:
JTextField txtField = new JTextField();
txtField.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
JOptionPane.showMessageDialog(null, "Righ Click", "Test",
JOptionPane.INFORMATION_MESSAGE);
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
});
this.add(txtField,BorderLayout.NORTH);
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 24th, 2007, 12:26 PM
#3
Thread Starter
Hyperactive Member
Re: jTextField Right Click sensor
Thanks for you help but I couldn't get it to work, here is what I tried:
vb Code:
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//JTextField txtField = new JTextField();
jTextField1.addMouseListener(new MouseListener() {
// @Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
JOptionPane.showMessageDialog(null, "Righ Click", "Test",
JOptionPane.INFORMATION_MESSAGE);
}
}
// @Override
public void mouseEntered(MouseEvent e) {
}
// @Override
public void mouseExited(MouseEvent e) {
}
// @Override
public void mousePressed(MouseEvent e) {
}
// @Override
public void mouseReleased(MouseEvent e) {
}
});
//this.add(txtField,BorderLayout.NORTH);
}
Nothing happens, how can i fix it to work with this format
-
Jul 24th, 2007, 02:13 PM
#4
Re: jTextField Right Click sensor
I don't know why you put it in this method. take a look:
Code:
import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class TestFrame extends JFrame {
private static final long serialVersionUID = -7356866315584500438L;
/**
* @throws HeadlessException
*/
public TestFrame() throws HeadlessException {
super("Test Project");
JTextField txtField = new JTextField();
txtField.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
JOptionPane.showMessageDialog(null, "Righ Click", "Test",
JOptionPane.INFORMATION_MESSAGE);
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
});
this.add(txtField,BorderLayout.NORTH);
}
/**
* @param args
*/
public static void main(String[] args) {
TestFrame frm = new TestFrame();
frm.setSize(400, 400);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jul 25th, 2007, 05:33 PM
#5
Thread Starter
Hyperactive Member
Re: jTextField Right Click sensor
Here is the thing, im using netbeans and im using its auto encoded jTextField. How do I make that work with jTextField1 which is already been created by netbeans? Thats why I originally put it in jTextField1ActionPerformed. Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|