java - How to create a scrollbar for my JTextArea using JScrollPane? -
the program compiles properly, scroll pane text area not created. don't know why happening. defined jscrollpane , implemented scrollpane = new jscrollpane
this code:
import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.table.*; public class guiproject5 extends jframe { private static final int width = 400; private static final int height = 300; private jpanel lowerpanel; private jlabel widthl, areal; private jtextarea areata; private jtextarea ta; private jscrollpane scrollpane; private jtextfield lengthtf; private jcheckbox gergsc; private jbutton exitb; //button handlers: private exitbuttonhandler ebhandler; public guiproject5() { areal = new jlabel("label: ", swingconstants.right); lengthtf = new jtextfield("textfield"); lowerpanel = new jpanel(); areata = new jtextarea("textarea", 6, 8); ta = new jtextarea("stuff", 6, 8); scrollpane = new jscrollpane(); gergsc = new jcheckbox("checkbox"); //specify handlers each button , add (register) actionlisteners each button. exitb = new jbutton("button"); ebhandler = new exitbuttonhandler(); exitb.addactionlistener(ebhandler); settitle("my gui in java"); container pane = getcontentpane(); pane.setlayout(new gridlayout(4, 2)); //add things pane in order want them appear (left right, top bottom) pane.add(exitb); pane.add(lengthtf); pane.add(lowerpanel); ta = new jtextarea("stuff", 6, 8); ta.setlinewrap(true); ta.setwrapstyleword(true); lowerpanel.add(new jscrollpane(scrollpane)); pane.add(new jscrolscrollpane); pane.add(gergsc); pane.add(areal); setsize(width, height); setvisible(true); setdefaultcloseoperation(exit_on_close); } public class exitbuttonhandler implements actionlistener { public void actionperformed(actionevent e) { system.exit(0); } } public static void main(string[] args) { guiproject5 rectobj = new guiproject5(); }
}
you should initialize jscrollpane
like
jsp=new jscrollpane(textareaobj);
and add panel.
jscrollpane
is container class. should define component on want scrollbars in jscrollpane() constructor
, add jsp object in layout
Comments
Post a Comment