swing - Java Scroll bar -



swing - Java Scroll bar -

i want add together scroll bar text area if user inputs number greater 20 text should have scroll bar. trying create application user inputs number wants multiplication table , inputs number wants table displayed.but application show table 20 e.g 12 x 20 = 240. , rest hidden.

public class layoutm extends jframe implements actionlistener { private jtextfield num1; private jtextfield num2; private jtextarea answer; private jbutton go; private int num11; private int num22; public layoutm(){ super("multiplication"); setlayout(new flowlayout()); dimension numdim = new dimension(60,20); dimension ansdim = new dimension(200,300); dimension godim = new dimension(60,20); num1 = new jtextfield("number"); num1.setpreferredsize(numdim); num2 = new jtextfield("upto"); num2.setpreferredsize(numdim); go = new jbutton("go"); num2.setpreferredsize(godim); reply = new jtextarea(20,20); answer.setpreferredsize(ansdim); answer.seteditable(false); add(num1, borderlayout.center); add(num2,borderlayout.center); add(go,borderlayout.center); add(answer,borderlayout.south); go.addactionlistener(this); } public static void main(string[] args){ layoutm ob = new layoutm(); ob.setdefaultcloseoperation(jframe.exit_on_close); ob.setvisible(true); ob.setsize(300,400); ob.setresizable(false); ob.setlocationrelativeto(null); } public void actionperformed(actionevent event){ try{ answer.settext(" "); num11 = integer.parseint(num1.gettext()); num22 = integer.parseint(num2.gettext()); for(int count = 1; count < num22+1;count++){ answer.append(num11+ " x "+ count+" = " + num11*count+" \n"); } }catch(exception e){ joptionpane.showmessagedialog(null, "no decimals allowed"); } } }

you should set answer object new jscrollpane object, , add scroll pane layoutm.

so, in fields should add:

private jscrollpane scroll;

instead of using

add(answer,borderlayout.south);

you should use

add(scroll,borderlayout.south);

and in actionperformed() method, should alter number of rows according number got user. set before for loop:

if ( num22 > 20 ) { answer.setrows(num22); } else { answer.setrows(20); }

java swing

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -