pavithraCS
pavithraCS

Reputation: 691

Implementing a clear button for a JFrame

I'm implementing a clear button for a JFrame which consists of four JPanels. Each JPanel has several text fields, radio buttons and checkboxes.

When the forms loads "clear" buttons should be disabled. It should be only enabled when the user entered some value to any of those fields in any panel.

I tried by adding a KeyListener to the panels. But It does not get the events properly. Do I have to register KeyListener for all the UI components? Any other good method?

Thanks in advance!

Upvotes: 2

Views: 9263

Answers (4)

nIcE cOw
nIcE cOw

Reputation: 24626

For making changes you can add ItemListener to your JCheckBox and JRadioButtons and for JTextField you can add CaretListener.

This small program might help you :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.CaretListener;
import javax.swing.event.CaretEvent;

public class StateChangedEventClass extends JFrame
{
    private JPanel contentPane, panel1, panel2;
    private JButton clearButton;
    private ItemListener itemChangeAction;
    private CaretListener caretAction;

    public StateChangedEventClass()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(2, 2));
        clearButton = new JButton("CLEAR");
        clearButton.setEnabled(false);

        itemChangeAction = new ItemListener()
        {
            public void itemStateChanged(ItemEvent ce)
            {
                clearButton.setEnabled(true);
            }
        };

        caretAction = new CaretListener()
        {
            public void caretUpdate(CaretEvent ce)
            {
                clearButton.setEnabled(true);
            }
        };

        panel1 = new JPanel();
        panel1.setLayout(new GridLayout(2  , 2));
        JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
        JTextField userField = new JTextField(10);
        userField.addCaretListener(caretAction);
        JLabel passLabel = new JLabel("PASSWORD : " + JLabel.CENTER);
        JTextField passField = new JTextField(10);
        passField.addCaretListener(caretAction);
        panel1.add(userLabel);
        panel1.add(userField);
        panel1.add(passLabel);
        panel1.add(passField);
        contentPane.add(panel1);

        panel2 = new JPanel();
        panel2.setLayout(new GridLayout(2, 1));
        JRadioButton maleButton = new JRadioButton("MALE", false);
        maleButton.addItemListener(itemChangeAction);
        JRadioButton femaleButton = new JRadioButton("FEMALE", false);
        femaleButton.addItemListener(itemChangeAction);
        ButtonGroup bg = new ButtonGroup();
        bg.add(maleButton);
        bg.add(femaleButton);
        panel2.add(maleButton);
        panel2.add(femaleButton);
        contentPane.add(panel2);

        add(contentPane, BorderLayout.CENTER);
        add(clearButton, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
    }

    public void caretUpdate(CaretEvent ce)
    {
        clearButton.setEnabled(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new StateChangedEventClass();
            }
        });
    }
}

Upvotes: 5

Juvanis
Juvanis

Reputation: 25950

It should be only enabled when the user entered some value to any of those fields in any panel.

So you should implement KeyListener interface for all these fields. Upon detecting typing action of the user you should enable the button.

jButton.setEnabled(true);

Do I have to register KeyListener for all the UI components?

Of course no. Write a class which extends JTextField and implements KeyListener. Derive your objects (all text fields) from this class. If you implement what you should do upon key strokes, all these objects will obey your rule.

Upvotes: 1

mKorbel
mKorbel

Reputation: 109813

KeyListener isn't designated for Listening a Keyboards events in the Swing GUI, for Swing is there KeyBindings

Upvotes: 4

alnasfire
alnasfire

Reputation: 720

You must adding ActionListeners to your JTextFields, after this check text value in text fields, for example:

jButton.setEnabled(!jTextField.getText().equals("")); 

Upvotes: 1

Related Questions