Fam
Fam

Reputation: 698

Cannot enable JTextField on button click

I am not sure why my JTextField is not being enabled when it is clicked.

Here is the code where I create and set the JTextField:

PromptHandler ph = new PromptHandler(this);
textPanel = new JPanel();
arEntry = new JTextField(REGION_TEXT_FIELD_COLUMNS);
arEntry.setToolTipText(ENTER_NEW_REGION_ID_TITLE_TEXT);
arEntry.setActionCommand(ENTER_NEW_REGION_ID_TITLE_TEXT);
arEntry.addActionListener(ph);
textPanel.add(arEntry);

And here is the code in PromptHandler that takes care of the text field:

private HelloWorld hwApp;
private String id;

public PromptHandler (HelloWorld hw) {
    hwApp = hw;
}

@Override
public void actionPerformed(ActionEvent ae) {
    String command = ae.getActionCommand();
    switch(command) {
        case ENTER_NEW_REGION_ID_TITLE_TEXT:
            hwApp.arEntry.setEnabled(true);
            break;

Upvotes: 0

Views: 591

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

ActionListener has nothing to with mouse interaction. ActionListeners are used to provide notification of some nondescript action that can occur with a component. In the case of the JTextField, it normally means that the user has triggered some "apply" action (typically via the Enter key)

If you want to monitor a fields interaction with the mouse, then you need a MouseListener

public class TestField {

    public static void main(String[] args) {
        new TestField();
    }

    public TestField() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JTextField field = new JTextField(20);
            field.setEnabled(false);
            add(field);
            field.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    e.getComponent().setEnabled(!e.getComponent().isEnabled());
                }
            });
        }
    }
}

My concern with this is, why? If you're going to enable the field on click that what's the point of disabling it? The only thing that will do is prevent it from receiving key board focus until it is enabled.

Upvotes: 1

MathanMV
MathanMV

Reputation: 412

Are you looking to disable or enable your JTextField?

I'm pretty sure when you create a JTextField object, it is already enabled unless you disable it after creating it. So your action listener is doing nothing in terms of changing the state of the JTextField.

Upvotes: 1

Related Questions