Is there a way, if so how, to set a single character within a FormattedJTextField as uneditable? I am trying to format a JTextField for currency and have read quite a few articles such as this, though it doesn't seem to clarify what I am trying to achieve.
What I am trying to create is a JTextField which will always have the "£" sign within it and will allow the user to enter there chosen price. As a result of this, I thought maybe a way to accomplish this was to the the "£" as uneditable whereas the rest of the JTextField can be edited.
An example:
categoryB = new JLabel("Category B: ",JLabel.CENTER);
centerP.add(categoryB);
categoryBText = new JTextField("");
centerP.add(categoryBText);
contentP.add(centerP);
setPriceFrame.setVisible(true);
When the GUI is opened, I want categoryBText
to always start with a "£" and is not removable from the JTextField.
Is there a better way to approach this?
Solution:
try {
MaskFormatter catA£ = new MaskFormatter("£###.##");
catA£.setPlaceholderCharacter(' ');
categoryAText = new JFormattedTextField(catA£);
centerP.add(categoryAText);
} catch (Exception ex) {
}