0

I've made a program recently in which a MouseListener is set on the content pane. The Problem is when I click on a JTextField, even tho I called setEnabled(false) and setEditable(false) on it, my mousePressed() doesn't happen. The rest is working fine. So my question: How do I disable the default MouseListener that JTextField contains?

edit: for test purpose:

import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class test extends JFrame implements MouseListener{

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    test frame = new test();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    test(){
        setBounds(0,0,500,500);
        addMouseListener(this);
        setLayout(null);
        JTextField n = new JTextField("test");
        n.setEditable(false);
        n.setBounds(200,200,40,20);
        add(n);
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        System.out.println("test");


    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Possible duplicate of [how to remove MouseListener / ActionListener on a JTextField](https://stackoverflow.com/questions/2627946/how-to-remove-mouselistener-actionlistener-on-a-jtextfield) – vinS Dec 19 '17 at 11:27
  • 1
    You've disabled the field, and you're wondering why your listener doesn't work? – Kayaman Dec 19 '17 at 11:27
  • I disabled it, so you can not select the characters of it. The MouseListener is from its parent. If I click on just the contentPane or any JLabels on it, it works. Just the JTextField don't want to. – Ragnaroek511 Dec 19 '17 at 11:31
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) The text field is consuming the event, that is why the content pane does not detect it. But before we go further, see [What is the XY problem?](http://meta.stackexchange.com/q/66377) – Andrew Thompson Dec 19 '17 at 13:21
  • Why do you need a mouse listener on the panel? – camickr Dec 19 '17 at 15:51
  • I think you meant to ask: **Why does my global MouseListener not function on a JTextField even when it is not enabled/editable.**. You could do `n.addMouseListener(this);` – Joop Eggen Dec 19 '17 at 16:03

1 Answers1

1

I disabled it, so you can not select the characters of it.

You can prevent selection of text in any text field by using a custom Caret to always make the start/end of the selection the same:

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

public class NoTextSelectionCaret extends DefaultCaret
{
    public NoTextSelectionCaret(JTextComponent textComponent)
    {
        setBlinkRate( textComponent.getCaret().getBlinkRate() );
        textComponent.setHighlighter( null );
    }

    @Override
    public int getMark()
    {
        return getDot();
    }

    private static void createAndShowUI()
    {
        JTextField textField1 = new JTextField("No Text Selection Allowed");
        textField1.setCaret( new NoTextSelectionCaret( textField1 ) );
        textField1.setEditable(false);

        JTextField textField2 = new JTextField("Text Selection Allowed");

        JFrame frame = new JFrame("No Text Selection Caret");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textField1, BorderLayout.NORTH);
        frame.add(textField2, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288