0

So I have a case like this:

I have a JFrame, let's call it frame1. There's a JButton named Submit in frame1.

Inside the frame1, there is a JPanel, call it panel1. There's a JTextField in that JPanel.

The frame1 and panel1 are two different Java files.

I want to do something like this:

When I user edit, delete or insert the JTextField, there's a DocumentListerner to check if the input match with a regex. If it matches, then the Submit button is enabled, otherwise the Submit button is disabled.

I already know how to implement the DocumentListener on JTextField1 with regex check. The thing I don't know how to do is to make the frame1 listen to the boolean outcome of that regex check, so the Submit button will be enabled/disabled.

Could anyone help please ?

Frakcool
  • 10,915
  • 9
  • 50
  • 89
LiemLT
  • 93
  • 2
  • 5

1 Answers1

1

One way to do this could be to have the JButton on the same class as the JTextField, for a simple GUI as the one you have this is doable.

But we know there are times that this isn't possible so another way that you could have this done up to Java 8 is using the Observer pattern, as shown in this example. After Java 9, Observer and Observable are deprecated so you should use PropertyChangeEvent and PropertyChangeListener from java.beans package1.

So, if you're not using Java 9 or higher and don't care about the caveats of Observer and Observable then you could have a sample code like this one:

EnableButtonInOtherClass

import java.awt.BorderLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class EnableButtonInOtherClass implements Observer {
    private JFrame frame;
    private OtherClass pane;
    private JButton button;
    private ObservableField observableField;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new EnableButtonInOtherClass()::createAndShowGUI);
    }
    
    private void createAndShowGUI() {
        frame = new JFrame(getClass().getSimpleName());
        
        observableField = new ObservableField();
        observableField.addObserver(this);
        
        pane = new OtherClass(observableField);
        button = new JButton("Click me!");
        
        button.setEnabled(false);
        
        frame.add(pane);
        frame.add(button, BorderLayout.SOUTH);
        
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("Text is a digit: " + arg);
        button.setEnabled((boolean) arg);
    }
}

OtherClass

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

@SuppressWarnings("serial")
public class OtherClass extends JPanel implements DocumentListener {
    private JTextField field;
    private ObservableField observableField;
    
    public OtherClass(ObservableField observableField) {
        field = new JTextField(10);
        
        add(field);
        this.observableField = observableField;
        
        field.getDocument().addDocumentListener(this);
    }
    
    @Override
    public void changedUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        updateState();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        updateState();
    }
    
    private void updateState() {
        String patternString = "[0-9]+";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(field.getText());
        observableField.updateState(matcher.matches());
    }
}

ObservableField

import java.util.Observable;

public class ObservableField extends Observable {
    public void updateState(boolean state) {
        setChanged();
        notifyObservers(state);
    }
}

The above example makes use of the ObservableField class to handle the Observable class, that has an update method that notifies the observers (EnableButtonInOtherClass) that there has been a change every time you type into the JTextField on OtherClass and based on the matcher.matches() value determines if the JButton should be enabled or not.

But again, for a simple UI as the one you have it's easier to move the JButton to the same file and / or on JButton's ActionListener evaluate what's inside the JTextField rather than enable / disable the JButton.

Here are some screenshots of how the program looks like.

enter image description here enter image description here enter image description here

1Taken from this answer

Frakcool
  • 10,915
  • 9
  • 50
  • 89