0

I have this code:

    static JFrame inputFrame = new JFrame();
    static JTextField myTextfield = new JTextField();
    static JButton myButton = new JButton("Hi!");

private static void inputGUI() {
    inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    inputFrame.setTitle("The INPUT");
    inputFrame.setLayout(new FlowLayout());
    inputFrame.setSize(1366,768);
    inputFrame.setVisible(true);

    inputFrame.add(myTextField);
    inputFrame.add(myButton);

    myButton.setEnabled(false);
    myTextField.addActionListener(myListener);
}
static ActionListener myListener = new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==inputFrame){
            //What Do I put here?
        }
    }
}

What I want to do is this:
When I type something in myTextField without pressing enter, the button,myButton is enabled( e.g. myButton.setEnabled(true);).

Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Glynn Bacanto
  • 439
  • 2
  • 6
  • 12

3 Answers3

1

The best solution is using DocumentFilter . so it can check when your JTextField contains something then your can trigger your button to be active.

Here, I remodified your codes :

  import java.awt.*;
  import javax.swing.*;
  import javax.swing.text.AbstractDocument;
  import javax.swing.text.AttributeSet;
  import javax.swing.text.BadLocationException;
  import javax.swing.text.DocumentFilter;

class checkText extends DocumentFilter {
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
        AttributeSet attrs) throws BadLocationException {
        super.replace(fb, offset, length, text, attrs);
       GuiTest.enableButton();
       }
      }

 public class GuiTest extends JFrame {
static JFrame inputFrame = new JFrame();
static JTextField myTextfield = new JTextField(10);
static JButton myButton = new JButton("Hi!");

public GuiTest() {
    inputGUI();
}

private static void inputGUI() {
    inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    inputFrame.setTitle("The INPUT");
    inputFrame.setLayout(new FlowLayout());
    inputFrame.setSize(1366, 768);
    inputFrame.setVisible(true);

    inputFrame.add(myButton);

    DocumentFilter filter = new checkText();
    ((AbstractDocument) myTextfield.getDocument()).setDocumentFilter(filter);
    inputFrame.add(myTextfield);
    myButton.setEnabled(false);
}

public static void enableButton() {
    myButton.setEnabled(true);
}

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

before :

enter image description here

after :

enter image description here

Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19
-1

You can just insert this code:

JTextField jTF = (JTextField)e.getSource();
if( jTF.getText().length()>0 )
    myButton.setEnabled(true);

Answering comments:

  • Reading the javadoc of isEmpty(), it returns true if and only if length==0, so it's just the same.
  • I didn't put any comments because the original question was "What code do I put here", so I posted the code, there was really nothing wrong in the first place.
Kvothe
  • 19
  • 2
-2

try this instead

myTextField.addKeyListener(new java.awt.event.KeyAdapter() {

        public void keyPressed(java.awt.event.KeyEvent evt) {

            if(myTextField.getText().isEmpty()){
                myButton.setEnabled(false);
             } else{
                myButton.setEnabled(true);
             }
            }
         });

I hope it will help!

Gilles
  • 1
  • 2