java - Setting event listener for two JTextArea's -
i developing image processing software (just fun) , 1 of features has image resizing option. window pops up, 2 jtextarea
components desired image width , height resizing. there jcheckbox
keeping aspect ratio if user desires it. problem is. when check box selected , user supposedly inputs either width or height first. want other text area update accordingly every time alter made maintain ar. have developed code deals this, not provide want due lack of understanding listener component should assign.
code:
string height, width; if (checkboximage.isselected()){ // aspect ratio = width / height width = widtharea.gettext(); height = heightarea.gettext(); double aspectratio = (double) images.get(tabbedpane.getselectedindex()).getwidth() / images.get(tabbedpane.getselectedindex()).getheight(); /** * do, update width, height area * closest user input */ if(heightarea.gettext().length() != 0 && heightarea.gettext().length() <= 5 && heightarea.gettext().charat(0) != '0'){ //parsing string integer try{ int heightnum = integer.parseint(height); int widthnum = (int) math.round(aspectratio * heightnum); widtharea.settext(string.valueof(widthnum) ); widtharea.updateui(); frameimgsize.repaint(); } catch(numberformatexception e1){joptionpane.showmessagedialog(error,e1.getmessage(),"error", joptionpane.error_message);} } //width has been entered first else if(widtharea.gettext().length() != 0 && widtharea.gettext().length() <= 5 && widtharea.gettext().charat(0) != '0'){ try{ int widthnum = integer.parseint(width); int heightnum = (int) math.round(aspectratio * widthnum); heightarea.settext(string.valueof(heightnum) ); heightarea.updateui(); frameimgsize.repaint(); } catch(numberformatexception e1){joptionpane.showmessagedialog(error,e1.getmessage(),"error", joptionpane.error_message);} } }
is ever valid have non-numeric values in width , height fields?
if not, utilize jspinners
or jformattedtextfields
instead of jtextfields
. if so, (say illustration allow "units" entered width , height) should attach documentlistener
jtextfields monitor changes content of underlying text documents. here's example:
widthfield.getdocument().adddocumentlistener(new documentlistener() { public void changedupdate(documentevent e) { update(); } public void removeupdate(documentevent e) { update(); } public void insertupdate(documentevent e) { update(); } // method handles document alter event public void update() { if( aspectcheckbox1.isselected() ) { // parse width , height, // constrain height aspect ratio , update here } } });
you'd add together similar documentlistener heighttextfield.
note if utilize jtextfields need parse contents, read units (where applicable) , handle numberformatexceptions in case user enters invalid numeric values.
to reply question add together handlers...
the update of width should happen when there document alter height gui element. update of height should happen when there document alter width gui element.
you'll need gracefully handle split 0 errors (or restrict input greater 0), perform calculations using doubles , preferably utilize math.round()
best integer values preserving aspect.
ie:
int calculateheight(int width, double aspect) { if( aspect <= 0.0 ) { // handle error status } homecoming (int)math.round(width / aspect); }
for tracking aspect ratio, store in fellow member variable , add together actionlistener
jcheckbox... because updating target aspect ratio on every value alter of width , height fields result in aspect-ratio "creeping" due integer round-off.
here's illustration on tracking aspect every time aspect ratio check state changes:
private double aspect = 1.0; aspectcheckbox.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { preserveaspectactionperformed(evt); } }); private void preserveaspectactionperformed(java.awt.event.actionevent evt) { seek { double w = double.parsedouble(widthfield.gettext()); double h = double.parsedouble(heightfield.gettext()); aspect = w / h; } catch(numberformatexception ex) { // ... error occurred due non-numeric input // (use jspinner or jformattedtextfield avoid this) } }
the of import thing avoid using wrong input type job:
jtextareas multiline text jtextfields single line text jformattedtextfields text constrained specific format jspinners numeric entry.hope helps you.
java swing jtextarea event-listeners
No comments:
Post a Comment