swing - java JFormattedTextField -
i have jformattedtextfield dateformat. format "ddmmyy". format allows quick input.on focus lost want text in field change localdate easier read:
input: "200295". converting localdate getvalue() gives localdate of 20. february 1995. , good, text "1995-02-25" (localdate.tostring()
).
when field loses focus, want text displayed in field change localdate.tostring()
without actual value of field change 200295/ 20. feb 1995.
is there way make text overlay field instead of changing value/text of it?
sscce of have been thinking far:
main class:
public class formatdatetest { public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { new theframe(); } }); } }
frame class:
public class theframe extends jframe{ jpanel panel; jpanel textpanel; jformattedtextfield datefield; jbutton button; jtextarea textarea; dateformat format; public theframe() { button = new jbutton("click"); button.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent arg0) { //temporarily crates date converted. date date = (date) datefield.getvalue(); localdate localdate = date.toinstant().atzone(zoneid.systemdefault()).tolocaldate(); // sends different values of textarea textarea.append("the value: " + datefield.getvalue() + "\n"); textarea.append("the date: " + date.tostring() + "\n"); textarea.append("the localdate: " + localdate.tostring() + "\n"); } }); //sets text localdate prettyness button.addfocuslistener(new focuslistener() { @override public void focuslost(focusevent arg0) { date date = (date) datefield.getvalue(); localdate localdate = date.toinstant().atzone(zoneid.systemdefault()).tolocaldate(); datefield.settext(localdate.tostring()); } @override public void focusgained(focusevent arg0) { datefield.settext(""); } }); textarea = new jtextarea(); panel = new jpanel(); textpanel = new jpanel(); panel.setlayout(new borderlayout()); textpanel.setlayout(new borderlayout()); //datefield , format format = new simpledateformat("ddmmyy"); datefield = new jformattedtextfield(format); textpanel.add(textarea,borderlayout.center); panel.add(datefield,borderlayout.north); panel.add(button, borderlayout.center); add(panel,borderlayout.north); add(textpanel,borderlayout.center); pack(); setsize(400, 300); setvisible(true); setdefaultcloseoperation(exit_on_close); } }
use jpanel
cardlayout
. put 2 components in - input field , formatted 1 (i assume jtextfield
do). on focus of them, bring formatted field front (using methods on cardlayout
), , let user input data. on focus lost, process value (remember deal errors!) and, if parsing goes well, put formatted value in jtextfield
, bring front.
-- update based on remark --
lightweight solution: use jlabel
instead of jtextfield
formatted part. remember call setfocusable(true)
.
even lighter: subclass jtextfield
. override paintcomponent
such that: a) when component focused, delegate drawing super
. b) when not focused, paint formatted text yourself.
Comments
Post a Comment