java - JTextArea in JScrollPane will not display -
i have been having big problem gui. first problem when press button causes jtextarea display string, text area changes , pushes around of buttons , else in gui. tried many solutions nil worked.
i set text area in scroll panel may or may not have worked. dont know because no text displaying in scroll panel. can @ code , tell me doing wrong?
thanks
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class myclass extends jframe implements actionlistener{ public jbutton image = new jbutton(); public jtextarea description = new jtextarea(10,30); public jbutton b1 = new jbutton(); public jtextarea c1 = new jtextarea(); public myclass (string stp){ super(stp); makegui(); } public static void main(string[] args){ myclass test = new myclass("story"); } public void actionperformed(actionevent event){ object source = event.getsource(); if (source==b1){ description.settext("hello world"); c1.settext("hello world"); } } public void makegui(){ jscrollpane scroll = new jscrollpane(description); jpanel pane = new jpanel(new gridbaglayout()); container con = this.getcontentpane(); setbounds(50,50,600,600); //(x,-y,w,h) north west setdefaultcloseoperation(jframe.exit_on_close); // image gridbagconstraints gbc = new gridbagconstraints(); gbc.fill=gridbagconstraints.none; gbc.ipadx=260; gbc.ipady=310; gbc.insets=new insets(5,5,5,5); gbc.gridx=0; gbc.gridy=0; gbc.gridwidth=2; gbc.gridheight=3; pane.add(picture,gbc); // button 1 b1.addactionlistener(this); gbc.fill=gridbagconstraints.none; gbc.ipadx=0; gbc.ipady=10; gbc.insets=new insets(5,5,5,5); gbc.gridx=0; gbc.gridy=3; gbc.gridwidth=1; gbc.gridheight=1; pane.add(b1,gbc); //caption 1 gbc.fill=gridbagconstraints.horizontal; gbc.ipadx=0; gbc.ipady=30; gbc.insets=new insets(5,5,5,5); gbc.gridx=1; gbc.gridy=3; gbc.gridwidth=3; gbc.gridheight=1; c1.seteditable(false); c1.setlinewrap(true); c1.setwrapstyleword(true); pane.add(c1,gbc); // description !this part im having problem with! gbc.fill=gridbagconstraints.both; gbc.ipadx=100; gbc.ipady=170; gbc.insets=new insets(10,10,10,0); gbc.gridx=2; gbc.gridy=0; gbc.gridwidth=2; gbc.gridheight=1; description.seteditable(false); description.setlinewrap(true); description.setwrapstyleword(true); scroll.add(description); pane.add(scroll,gbc); con.add(pane); setvisible(true); } }
try pressing little button in bottom left corner. should see text in both text areas, 1 works.
this mutual mistake. elaborate solution reimeus bit:
the jscollpane
complex component contains several sub-components. may contain viewport
, perchance jscrollbar
s, shown in this image how utilize scroll panes tutorial. these components arranged special layout manager (particularly, scrollpanelayout
).
when phone call scrollpane.add(componentthatshouldbescrolled)
, new component may replace 1 of existing components, or screw whole layout unpredictably.
instead of manually adding contents scrollpane, pass jscollpane
constructor:
jscrollpane scrollpane = new jscrollpane(componentthatshouldbescrolled);
alternatively, if have replace "component should scrolled", call
scrollpane.setviewportview(componentthatshouldbescrolled);
java swing user-interface jscrollpane jtextarea
No comments:
Post a Comment