Results 1 to 5 of 5

Thread: jTextField Right Click sensor

  1. #1

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    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

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    Re: jTextField Right Click sensor

    Thanks for you help but I couldn't get it to work, here is what I tried:

    vb Code:
    1. private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    2. // TODO add your handling code here:
    3.        
    4.         //JTextField txtField = new JTextField();
    5.     jTextField1.addMouseListener(new MouseListener() {
    6.  
    7.       //  @Override
    8.         public void mouseClicked(MouseEvent e) {
    9.         if (e.getButton() == MouseEvent.BUTTON3) {
    10.             JOptionPane.showMessageDialog(null, "Righ Click", "Test",
    11.                 JOptionPane.INFORMATION_MESSAGE);
    12.         }
    13.         }
    14.  
    15.       //  @Override
    16.         public void mouseEntered(MouseEvent e) {
    17.         }
    18.  
    19.       //  @Override
    20.         public void mouseExited(MouseEvent e) {
    21.         }
    22.  
    23.      //   @Override
    24.         public void mousePressed(MouseEvent e) {
    25.         }
    26.  
    27.      //   @Override
    28.         public void mouseReleased(MouseEvent e) {
    29.         }
    30.  
    31.     });
    32.     //this.add(txtField,BorderLayout.NORTH);
    33.        
    34.        
    35.        
    36.     }


    Nothing happens, how can i fix it to work with this format

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  5. #5

    Thread Starter
    Hyperactive Member xxarmoxx's Avatar
    Join Date
    Mar 2007
    Posts
    378

    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
  •  



Click Here to Expand Forum to Full Width