java - JScrollPane doesn't work properly -


mr. hovercraft full of eels

i did said. apology still not showing :(

private void initialize() {     frame = new jframe();     frame.setbounds(100, 100, 760, 666);     frame.setdefaultcloseoperation(jframe.exit_on_close);     //frame.getcontentpane().setlayout(null);     //frame.getcontentpane().setlayout();      textarea = new jtextarea(20,20);     textarea.setpreferredsize(new dimension(100, 100));     textarea.setlinewrap(true);     jscrollpane sp = new jscrollpane(textarea, jscrollpane.vertical_scrollbar_always, jscrollpane.horizontal_scrollbar_always);     frame.getcontentpane().add(sp);     frame.setvisible(true); 

}


i'm following tutorial on youtube using windowbuilderpro in eclipse. want create (scrollable) jtextarea using jscollpane. though followed principles, didn't work! little thing consumes time decided ask guys :( kindly, can tell me why jtextarea isn't showing in jframe .

public class projectgui {  private jframe frame; private jtextfield urls; private jtextarea textarea;  /**  * launch application.  */ public static void main(string[] args) {     eventqueue.invokelater(new runnable() {         public void run() {             try {                 projectgui window = new projectgui();                 window.frame.setvisible(true);             } catch (exception e) {                 e.printstacktrace();             }         }     }); }  /**  * create application.  */ public projectgui() {     initialize(); }  /**  * initialize contents of frame.  */ private void initialize() {     frame = new jframe();     frame.setbounds(100, 100, 760, 666);     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.getcontentpane().setlayout(null);      jpanel panel = new jpanel();     panel.setborder(new titledborder(null, "x.509 extractor", titledborder.leading, titledborder.top, null, null));     panel.setbounds(18, 16, 722, 145);     panel.setlayout(null);     frame.getcontentpane().add(panel);      urls = new jtextfield();     urls.settext("for multiple urls, separate them comma ,");     urls.setforeground(color.blue);     urls.setbounds(59, 57, 646, 28);     panel.add(urls);      jbutton button1 = new jbutton("extract");     button1.setbounds(303, 97, 117, 40);     frame.setlocationrelativeto(null);     panel.add(button1);      textarea = new jtextarea();     textarea.setbounds(18, 212, 722, 414);     textarea.setlinewrap(true);     jscrollpane sp= new jscrollpane(textarea);     frame.getcontentpane().add(sp);     frame.setvisible(true);  } 

}

you're setting bounds of jtextarea, should never do, , preventing growing within jscrollpane.

your jtextarea isn't showing because you're using null layout , add component without specifying size or position, jscrollpane.

suggestions:

  • don't use null layout , setbounds
  • instead use appropriate combination of jpanels using layout managers
  • never set size or bounds of jtextarea.
  • yes, set rows , columns properties, that's fine.

edit
regarding latest post, let me add, clear:

  • don't set preferred size of jtextarea
  • don't set any size of jtextaera.

you see scroll bars when text inside of jtextarea larger bounds of jscrollpane's viewport. example:

import java.util.random; import javax.swing.joptionpane; import javax.swing.jscrollpane; import javax.swing.jtextarea;  public class jtextareatest {    public static void main(string[] args) {       random random = new random();       stringbuilder sb = new stringbuilder();       (int = 0; < 100; i++) {          (int j = 0; j < 20; j++) {             (int k = 0; k < random.nextint(5) + 5; k++) {                char c = (char) ('a' + random.nextint(26));                sb.append(c);                            }             sb.append(' ');          }          sb.append('\n');                      }        jtextarea textarea = new jtextarea(15, 40);       textarea.settext(sb.tostring());       jscrollpane scrollpane = new jscrollpane(textarea);        joptionpane.showmessagedialog(null, scrollpane);    } } 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -