Modifying a MaskFormatter to allow right to left input
I've been looking at various way to format a JFormattedTextField to do a custom entry.
I need a display to use colon as a separator such as in a time field in the form of 12:34:56
. This can obviously be done easily using a custom maskFormatter which would hold an initial value of 00:00:00
until the user starts to enter data.
This works relatively well but I needed the entry to work from right to left (similar to a calculator ~ entering digits starting from the right and filling in to the left as you type. Again using a DateFormatter works and I like the feature that you can use the up/down arrows when in a selection to inc/dec the time for each of the fields but again this doesn't allow you to enter a time such as 00:00:90
similar to how a microwave oven timer works nor does it have an overwrite function (or at least not that I can see).
I've seen a few examples using number formatter and document filters here on SO but again they fall short from my requirements.
Here is a basic class that shows all 3 examples in the simplest form.
What I'm looking for is a combination of the SimpleMaskFormatter and the SimpleDateFormat (leaning more towards the SimpleDateFormat with an overwrite feature). I was thinking I need to write a custom document filter for the date format but thought I would put this out to see if anyone had suggestions on how to work this out?
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import net.miginfocom.swing.MigLayout;
public class FormattedTextInput extends JFrame {
private FormattedTextInput() {
setLayout(new MigLayout());
JFormattedTextField textField1 = new JFormattedTextField(new SimpleMaskFormatter());
textField1.setColumns(10);
textField1.setHorizontalAlignment(JTextField.RIGHT);
add(textField1, "wrap");
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
JFormattedTextField textField2 = new JFormattedTextField(dateFormat);
textField2.setColumns(10);
textField2.setHorizontalAlignment(JTextField.RIGHT);
textField2.setText("00:00:00");
add(textField2, "wrap");
DecimalFormatSymbols colonSeporator = new DecimalFormatSymbols();
colonSeporator.setGroupingSeparator(':');
DecimalFormat decimalFormat = new DecimalFormat("00:00:00", colonSeporator);
decimalFormat.setGroupingUsed(true);
decimalFormat.setMaximumIntegerDigits(6);
decimalFormat.setGroupingSize(2);
JFormattedTextField textField3 = new JFormattedTextField(decimalFormat);
textField3.setHorizontalAlignment(JTextField.TRAILING);
textField3.setColumns(10);
add(textField3, "wrap");
}
private class SimpleMaskFormatter extends MaskFormatter {
SimpleMaskFormatter() {
try {
setMask("##:##:##");
} catch (ParseException e) {
e.printStackTrace();
}
setPlaceholderCharacter('0');
setCommitsOnValidEdit(true);
}
}
public static void main(String args) {
FormattedTextInput window = new FormattedTextInput();
window.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
java jformattedtextfield
add a comment |
I've been looking at various way to format a JFormattedTextField to do a custom entry.
I need a display to use colon as a separator such as in a time field in the form of 12:34:56
. This can obviously be done easily using a custom maskFormatter which would hold an initial value of 00:00:00
until the user starts to enter data.
This works relatively well but I needed the entry to work from right to left (similar to a calculator ~ entering digits starting from the right and filling in to the left as you type. Again using a DateFormatter works and I like the feature that you can use the up/down arrows when in a selection to inc/dec the time for each of the fields but again this doesn't allow you to enter a time such as 00:00:90
similar to how a microwave oven timer works nor does it have an overwrite function (or at least not that I can see).
I've seen a few examples using number formatter and document filters here on SO but again they fall short from my requirements.
Here is a basic class that shows all 3 examples in the simplest form.
What I'm looking for is a combination of the SimpleMaskFormatter and the SimpleDateFormat (leaning more towards the SimpleDateFormat with an overwrite feature). I was thinking I need to write a custom document filter for the date format but thought I would put this out to see if anyone had suggestions on how to work this out?
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import net.miginfocom.swing.MigLayout;
public class FormattedTextInput extends JFrame {
private FormattedTextInput() {
setLayout(new MigLayout());
JFormattedTextField textField1 = new JFormattedTextField(new SimpleMaskFormatter());
textField1.setColumns(10);
textField1.setHorizontalAlignment(JTextField.RIGHT);
add(textField1, "wrap");
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
JFormattedTextField textField2 = new JFormattedTextField(dateFormat);
textField2.setColumns(10);
textField2.setHorizontalAlignment(JTextField.RIGHT);
textField2.setText("00:00:00");
add(textField2, "wrap");
DecimalFormatSymbols colonSeporator = new DecimalFormatSymbols();
colonSeporator.setGroupingSeparator(':');
DecimalFormat decimalFormat = new DecimalFormat("00:00:00", colonSeporator);
decimalFormat.setGroupingUsed(true);
decimalFormat.setMaximumIntegerDigits(6);
decimalFormat.setGroupingSize(2);
JFormattedTextField textField3 = new JFormattedTextField(decimalFormat);
textField3.setHorizontalAlignment(JTextField.TRAILING);
textField3.setColumns(10);
add(textField3, "wrap");
}
private class SimpleMaskFormatter extends MaskFormatter {
SimpleMaskFormatter() {
try {
setMask("##:##:##");
} catch (ParseException e) {
e.printStackTrace();
}
setPlaceholderCharacter('0');
setCommitsOnValidEdit(true);
}
}
public static void main(String args) {
FormattedTextInput window = new FormattedTextInput();
window.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
java jformattedtextfield
Check out: stackoverflow.com/questions/17074758/…. It currently is used for numbers but you should be able to modify it to handle the colon separator.
– camickr
Nov 23 '18 at 16:13
add a comment |
I've been looking at various way to format a JFormattedTextField to do a custom entry.
I need a display to use colon as a separator such as in a time field in the form of 12:34:56
. This can obviously be done easily using a custom maskFormatter which would hold an initial value of 00:00:00
until the user starts to enter data.
This works relatively well but I needed the entry to work from right to left (similar to a calculator ~ entering digits starting from the right and filling in to the left as you type. Again using a DateFormatter works and I like the feature that you can use the up/down arrows when in a selection to inc/dec the time for each of the fields but again this doesn't allow you to enter a time such as 00:00:90
similar to how a microwave oven timer works nor does it have an overwrite function (or at least not that I can see).
I've seen a few examples using number formatter and document filters here on SO but again they fall short from my requirements.
Here is a basic class that shows all 3 examples in the simplest form.
What I'm looking for is a combination of the SimpleMaskFormatter and the SimpleDateFormat (leaning more towards the SimpleDateFormat with an overwrite feature). I was thinking I need to write a custom document filter for the date format but thought I would put this out to see if anyone had suggestions on how to work this out?
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import net.miginfocom.swing.MigLayout;
public class FormattedTextInput extends JFrame {
private FormattedTextInput() {
setLayout(new MigLayout());
JFormattedTextField textField1 = new JFormattedTextField(new SimpleMaskFormatter());
textField1.setColumns(10);
textField1.setHorizontalAlignment(JTextField.RIGHT);
add(textField1, "wrap");
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
JFormattedTextField textField2 = new JFormattedTextField(dateFormat);
textField2.setColumns(10);
textField2.setHorizontalAlignment(JTextField.RIGHT);
textField2.setText("00:00:00");
add(textField2, "wrap");
DecimalFormatSymbols colonSeporator = new DecimalFormatSymbols();
colonSeporator.setGroupingSeparator(':');
DecimalFormat decimalFormat = new DecimalFormat("00:00:00", colonSeporator);
decimalFormat.setGroupingUsed(true);
decimalFormat.setMaximumIntegerDigits(6);
decimalFormat.setGroupingSize(2);
JFormattedTextField textField3 = new JFormattedTextField(decimalFormat);
textField3.setHorizontalAlignment(JTextField.TRAILING);
textField3.setColumns(10);
add(textField3, "wrap");
}
private class SimpleMaskFormatter extends MaskFormatter {
SimpleMaskFormatter() {
try {
setMask("##:##:##");
} catch (ParseException e) {
e.printStackTrace();
}
setPlaceholderCharacter('0');
setCommitsOnValidEdit(true);
}
}
public static void main(String args) {
FormattedTextInput window = new FormattedTextInput();
window.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
java jformattedtextfield
I've been looking at various way to format a JFormattedTextField to do a custom entry.
I need a display to use colon as a separator such as in a time field in the form of 12:34:56
. This can obviously be done easily using a custom maskFormatter which would hold an initial value of 00:00:00
until the user starts to enter data.
This works relatively well but I needed the entry to work from right to left (similar to a calculator ~ entering digits starting from the right and filling in to the left as you type. Again using a DateFormatter works and I like the feature that you can use the up/down arrows when in a selection to inc/dec the time for each of the fields but again this doesn't allow you to enter a time such as 00:00:90
similar to how a microwave oven timer works nor does it have an overwrite function (or at least not that I can see).
I've seen a few examples using number formatter and document filters here on SO but again they fall short from my requirements.
Here is a basic class that shows all 3 examples in the simplest form.
What I'm looking for is a combination of the SimpleMaskFormatter and the SimpleDateFormat (leaning more towards the SimpleDateFormat with an overwrite feature). I was thinking I need to write a custom document filter for the date format but thought I would put this out to see if anyone had suggestions on how to work this out?
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;
import net.miginfocom.swing.MigLayout;
public class FormattedTextInput extends JFrame {
private FormattedTextInput() {
setLayout(new MigLayout());
JFormattedTextField textField1 = new JFormattedTextField(new SimpleMaskFormatter());
textField1.setColumns(10);
textField1.setHorizontalAlignment(JTextField.RIGHT);
add(textField1, "wrap");
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
JFormattedTextField textField2 = new JFormattedTextField(dateFormat);
textField2.setColumns(10);
textField2.setHorizontalAlignment(JTextField.RIGHT);
textField2.setText("00:00:00");
add(textField2, "wrap");
DecimalFormatSymbols colonSeporator = new DecimalFormatSymbols();
colonSeporator.setGroupingSeparator(':');
DecimalFormat decimalFormat = new DecimalFormat("00:00:00", colonSeporator);
decimalFormat.setGroupingUsed(true);
decimalFormat.setMaximumIntegerDigits(6);
decimalFormat.setGroupingSize(2);
JFormattedTextField textField3 = new JFormattedTextField(decimalFormat);
textField3.setHorizontalAlignment(JTextField.TRAILING);
textField3.setColumns(10);
add(textField3, "wrap");
}
private class SimpleMaskFormatter extends MaskFormatter {
SimpleMaskFormatter() {
try {
setMask("##:##:##");
} catch (ParseException e) {
e.printStackTrace();
}
setPlaceholderCharacter('0');
setCommitsOnValidEdit(true);
}
}
public static void main(String args) {
FormattedTextInput window = new FormattedTextInput();
window.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
java jformattedtextfield
java jformattedtextfield
asked Nov 23 '18 at 14:20
jboltjbolt
39121230
39121230
Check out: stackoverflow.com/questions/17074758/…. It currently is used for numbers but you should be able to modify it to handle the colon separator.
– camickr
Nov 23 '18 at 16:13
add a comment |
Check out: stackoverflow.com/questions/17074758/…. It currently is used for numbers but you should be able to modify it to handle the colon separator.
– camickr
Nov 23 '18 at 16:13
Check out: stackoverflow.com/questions/17074758/…. It currently is used for numbers but you should be able to modify it to handle the colon separator.
– camickr
Nov 23 '18 at 16:13
Check out: stackoverflow.com/questions/17074758/…. It currently is used for numbers but you should be able to modify it to handle the colon separator.
– camickr
Nov 23 '18 at 16:13
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448394%2fmodifying-a-maskformatter-to-allow-right-to-left-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448394%2fmodifying-a-maskformatter-to-allow-right-to-left-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Check out: stackoverflow.com/questions/17074758/…. It currently is used for numbers but you should be able to modify it to handle the colon separator.
– camickr
Nov 23 '18 at 16:13