问题
I have a string which is say "AAAABB". I have a JFormattedField of MaskFormatter
. so I have ? ? ? ? ? ?
in my Frame. I have two buttons A and B. when the user presses A button, the JFormattedField of the MaskFormatter
should replace ?
with Letter A for all occurrences of A. i.e, index 0,1,2,3 in this example.
I have my code that gets the list of indices to be replaced with A. But I am having difficulty in implementing setLetter(String Letter, int Position)
method, that takes letter and indices to be replaced in the JformattedField. example if I pass setLetter("A",2), I should get ? ? A ? ? ?
in the above example. Please try this code to see the frame.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;
public class TestMain extends JPanel{
JFormattedTextField input;
private MaskFormatter formatter;
public final String WORD = "ABBAABBA";
public TestMain() {
try {
JLabel label = new JLabel("Guesss");
String s="";
for (int i =0;i<WORD.length();i++){
s+="? ";
}
formatter = new MaskFormatter(s);
formatter.setPlaceholderCharacter('?');
input = new JFormattedTextField(formatter);
input.setColumns(20);
add(label);
add(input);
} catch (java.text.ParseException exc) {
System.err.println("formatter is bad: " + exc.getMessage());
System.exit(-1);
}
JButton buttonA = new JButton("A");
JButton buttonB = new JButton("B");
buttonA.addActionListener(clickedbutton());
buttonB.addActionListener(clickedbutton());
add(buttonA);
add(buttonB);
}
private ActionListener clickedbutton() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton pressedButton = (JButton) e.getSource();
String letter = e.getActionCommand();
try {
//System.out.println("actionCommand is: ---" + letter);
//Get the list of indices
int index = WORD.indexOf(letter);
ArrayList<Integer> indices = new ArrayList<Integer>();
if (index>=0){
for(int j=0;j<WORD.length();j++){
if (WORD.charAt(j)==letter.charAt(0)){
indices.add(j);
}
}
//System.out.println(indices);
}
for (int k =0 ; k < indices.size(); k++){
setLetter(letter, k);
}
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
};
}
public void setLetter(String letter, int position) throws ParseException {
String word="Hello";
input.setValue(letter);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
TestMain tm = new TestMain();
frame.add(tm);
frame.pack();
frame.setTitle("formatter");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
回答1:
I don't think a mask formatter is what you want for this because you aren't really formatting a string here. If you just want to take a string and set characters at certain positions you should formulate the string yourself.
String text = "";
for (int i = 0; i < WORD.length(); i++) {
if (String.valueOf(word.charAt(i)).equals(letter)) {
text += letter + " ";
} else {
text += "? ";
}
}
input.setText(text);
Also a style note: all caps is for identifiers of static variables.
来源:https://stackoverflow.com/questions/19458286/how-to-insert-characters-at-particular-index-in-jformattedtextfield-of-maskforma