java - JScrollPane won't display -
i'd create simple gui based 'echo' application ability scroll , down through previous input. far, working except when add jtextpane jscrollpane, lose input , scroll never appears.
can point me in right direction?
here code have far:
import java.awt.*; import javax.swing.*; public class filereadergui { private jframe frame; private jpanel inputpanel; private jtextfield userinput; private jscrollpane scrollpane; private jtextpane display; jbutton print; public void show() { frame = new jframe(); frame.setsize(400, 200); frame.setdefaultcloseoperation(jframe.exit_on_close); buildinputpanel(); builddisplaypanel(); frame.getcontentpane().add(inputpanel, borderlayout.south); frame.getcontentpane().add(scrollpane, borderlayout.center); frame.setvisible(true); } public void builddisplaypanel() { display = new jtextpane(); scrollpane = new jscrollpane(display); } public void buildinputpanel() { inputpanel = new jpanel(); userinput = new jtextfield(); userinput.setcolumns(20); inputpanel.add(userinput); print = new jbutton("print"); print.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { string current = display.gettext(); string input = userinput.gettext(); string newtext = string.format("%s\n%s", current, input); display.settext(newtext); } }); inputpanel.add(print); } }
and, here caller:
public class filereader { public void go() { filereadergui gui = new filereadergui(); gui.show(); } public static void main(string[] args) { new filereader().go(); } }
i'd should have scrollpane = new jscrollpane(display);
i agree above.
did reorder code make sure created text pane before creating scrollpane?
//scrollpane = new jscrollpane(); //display = new jtextpane(); //scrollpane.add(display); display = new jtextpane(); scrollpane = new jscrollpane(display);
if doesn't post sscce
includes main() method execute code.
Comments
Post a Comment