1

I had posted a similar question before but it got deleted by accident.

My JTextField is supposed to appear after clicking a button which it does but only after minimizing the window. Another problem with it is that it accepts single characters when it's supposed to accept a phrase of only a certain length.

Here's a bit of the code since it's so long:

 static String answer = "MY PUZZLE", puzzle = "M- -U----"; 

  //inside constructor
  guess = new JTextField("Write in Caps");
  guess.setVisible(false); 
  guess.addActionListener(this);
  board.add(guess);



  //actionperformed
  guess.setVisible(true);
  gueStr = guess.getText();

  if (gueStr.length() != answer.length()) //if it is not the same length
  {
    gueStr = "";
  }

the String "puzzle" is changed if a letter button (code not shown here) that is a character from the "answer" String is pressed or if the user's guess is the same phrase as the answer String

  if ( gueStr.equals(answer)) //if the guess is the answer
  {
    puzzle = answer; 
  }


    for(int x=0; x < answer.length(); x++) //go through answer
    {
        if(letter == answer.charAt(x)) //if the letter pressed matches a character in answer
        {
          puzzle = puzzle.substring(0,x) + letter + puzzle.substring(x+1); //substitute in letter 
        }

    }

If more code is needed for understanding, I can post it. I'd be happy for a few pointers :D

Edit:

Thanks for all help guys, but I haven't been successful in understanding the second part yet.

Right now, I can get the textfield to show, but the problem lies with accepting a certain character length. It has to be the exact same length as another string, no more, no less.

I have tried implementing it with the links you guys gave but only ended up getting confused (sorry). Can someone please offer a specific example?

Edit:

Resolved after using validate() and adding guess.setActionCommand("1").

  • setVisible comes to mind, for the rest, check this thread: https://stackoverflow.com/questions/10136794/limiting-the-number-of-characters-in-a-jtextfield – Stultuske Nov 14 '17 at 06:42
  • I already used setVisible and that page is about the limiting a maximum number of characters while I want to make it the same length as another string. Nevertheless, it was helpful, thank you. – TheDeadline Nov 14 '17 at 06:52
  • 1
    ehm ... yes, "originalString".getLength(), and you combine the result of that with what you found in the link I posted – Stultuske Nov 14 '17 at 07:07

2 Answers2

1

Use InputVerifier to validate your text input

Refer below example :

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JTextField;

class PassVerifier extends InputVerifier {

    public boolean verify(JComponent input) {
        JTextField tf = (JTextField) input;

        String text = tf.getText();

        // do your  validation 
//        if (text.equals(answer)) //if the guess is the answer
//        {
//            puzzle = answer;
//        }
//        for (int x = 0; x < answer.length(); x++) //go through answer
//        {
//            if (letter == answer.charAt(x)) //if the letter pressed matches a character in answer
//            {
//                puzzle = puzzle.substring(0, x) + letter + puzzle.substring(x + 1); //substitute in letter 
//            }
//        }
        return true; // return true or false according to validation  
    }
}

Set to InputVerifier to your JTextField

 JTextField tf1 = new JTextField ("Type \"pass\" here");
 tf1.setInputVerifier(new PassVerifier());
Akila
  • 1,258
  • 2
  • 16
  • 25
1

frame.validate()

The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.

This should work instead of minimizing the window.

Also for the second part, the API provides an example on how to custumize text fields: https://docs.oracle.com/javase/8/docs/api/javax/swing/JTextField.html

Do something similar to how they only accepts uppercase characters, but instead check the length with string.length().

Touniouk
  • 375
  • 1
  • 13