summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-25 22:11:28 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-25 22:11:28 -0400
commitfb7d03388e298258563c22abda1bd46cdaf991b7 (patch)
treeff2caeda751acd733ea00c089800ff380211f0b3 /BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java
parent42f7d379a430aaf2fad169f0170de04072b08b10 (diff)
General code cleanup, and some more GUI controls
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java66
1 files changed, 66 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java
new file mode 100644
index 0000000..f11c3a8
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/gui/FormattedInputPanel.java
@@ -0,0 +1,66 @@
+package bjc.utils.gui;
+
+import java.util.function.Consumer;
+
+import javax.swing.JFormattedTextField;
+import javax.swing.JFormattedTextField.AbstractFormatter;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
+import bjc.utils.gui.layout.HLayout;
+
+/**
+ * A simple panel allowing for input of a single formatted value
+ *
+ * @author ben
+ *
+ * @param <InputVal>
+ * The type of value being formatted
+ */
+public class FormattedInputPanel<InputVal> extends JPanel {
+ private static final long serialVersionUID = 5232016563558588031L;
+ private JFormattedTextField field;
+
+ /**
+ * Create a new formatted input panel
+ *
+ * @param label
+ * The label for this panel
+ * @param length
+ * The length of this panel
+ * @param formatter
+ * The formatter to use for input
+ * @param valueReciever
+ * The action to call whenever the value changes
+ */
+ @SuppressWarnings("unchecked")
+ public FormattedInputPanel(String label, int length,
+ AbstractFormatter formatter,
+ Consumer<InputVal> valueReciever) {
+ setLayout(new HLayout(2));
+
+ JLabel lab = new JLabel(label);
+ field = new JFormattedTextField(formatter);
+
+ field.setColumns(length);
+ field.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
+ field.addPropertyChangeListener("value", (event) -> {
+ // This is safe, because InputVal should be the type of
+ // whatever object the formatter is returning
+ valueReciever.accept((InputVal) field.getValue());
+ });
+
+ add(lab);
+ add(field);
+ }
+
+ /**
+ * Reset the value in this panel to a specified value
+ *
+ * @param value
+ * The value to set the panel to
+ */
+ public void resetValues(InputVal value) {
+ field.setValue(value);
+ }
+}