java - Adding JScrollPane in JTextArea using GridBagLayout -
i'm having issue adding jscrollpane in jtextarea using gridbaglayout. program runs fine when scrollbar isn't needed layout gets messed , content gets cut off when is. relevent code follows
import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class testgui extends jframe { public static string name; static jtextfield textfield = new jtextfield(30); static jtextarea textarea = new jtextarea(30,30); public static void main( string[] args) { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.settitle("checkem"); frame.setlocation(500,400); frame.setsize(800,800); jpanel panel = new jpanel(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); jscrollpane scrolltxt = new jscrollpane(textarea,jscrollpane.vertical_scrollbar_as_needed,jscrollpane.horizontal_scrollbar_never); scrolltxt.setwheelscrollingenabled(true); scrolltxt.getverticalscrollbar().isvisible(); panel.add(scrolltxt, c); jlabel label = new jlabel("enter name of file:"); c.gridx = 0; c.gridy = 0; c.insets = new insets(2,2,2,2); panel.add(label,c); c.gridx = 0; c.gridy = 1; panel.add(textarea,c); jbutton button = new jbutton("search"); c.gridx = 1; c.gridy = 1; panel.add(button,c); c.gridx = 1; c.gridy = 0; panel.add(textfield,c); frame.getcontentpane().add(panel, borderlayout.north); frame.pack(); frame.setvisible(true); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent ae) { checkem record = new checkem(); name = textfield.gettext(); string [] print = record.run(name); (int i=0;i<print.length;i++) { if(print[i] == null || print[i].isempty()) { continue; } else { textarea.append(print[i] + "\n"); } } } }); } }
i'm new swing , i'm @ loss go here. help.
- first please learn java naming conventions, makes bit easier other person understand java code.
now actual thingy :-)
- why not use jtextarea.setlinewrap(true) , jtextarea.setwrapstyleword(true) instead of defining
jscrollbar
policy, nice on view :-) - moreover, instead of specifying
setsize()/setlocation()
methods, useframereference.pack()
,frame.setlocationbyplatform(true)
, wonderful answer regarding benefit of latter mentioned in answer, how best position swing guis - do not make many static fields in class, smells bad programming design, , makes class less extensible.
- you extending
jframe
testgui
class , inside it'smain()
method creating instance of same. again, try give more weightage composition on inheritance, since on here, not trying modify defined features ofjframe
, instead using them is, there no need extendjframe
in case atleast :-) - read concurrency in swing
here modified code :
import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class testgui { private string name; private jtextfield textfield = new jtextfield(30); private jtextarea textarea = new jtextarea(30,30); private void displaygui() { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.settitle("checkem"); jpanel panel = new jpanel(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); textarea.setlinewrap(true); textarea.setwrapstyleword(true); jscrollpane scrolltxt = new jscrollpane(); scrolltxt.setviewportview(textarea); scrolltxt.setwheelscrollingenabled(true); jlabel label = new jlabel("enter name of file:"); c.gridx = 0; c.gridy = 0; c.insets = new insets(2,2,2,2); panel.add(label,c); c.gridx = 0; c.gridy = 1; panel.add(scrolltxt,c); jbutton button = new jbutton("search"); c.gridx = 1; c.gridy = 1; panel.add(button,c); c.gridx = 1; c.gridy = 0; panel.add(textfield,c); frame.getcontentpane().add(panel, borderlayout.north); //frame.setsize(800,800); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent ae) { /*checkem record = new checkem(); name = textfield.gettext(); string [] print = record.run(name); (int i=0;i<print.length;i++) { if(print[i] == null || print[i].isempty()) { continue; } else { textarea.append(print[i] + "\n"); } }*/ } }); } public static void main( string[] args) { runnable r = new runnable() { @override public void run() { new testgui().displaygui(); } }; eventqueue.invokelater(r); } }
Comments
Post a Comment