java - Automatic Resize of jPanel as text is added -



java - Automatic Resize of jPanel as text is added -

i have text game has buttons. when button clicked, text appears. text appears within jpanel, within jscrollpane. jpanel automatically create more vertical space lines of text added. have been doing hand lot more time consuming. there anyway this, or maybe pack jpanel somehow. pretty new if info needed help me out sense free ask. thanks.

i utilize component can automatically -- jtextarea. automatically enlarge more text added.

if need more specific help or code example, please post own little compilable , runnable test illustration program, , can seek modify it.

you state:

i don't want utilize jtextarea because don't want user able highlight or delete of text there in first place.

no problem. create jtextarea non-focusable , non-editable.

i have been using jlabels equal "" , when button pressed, jlabel given new value.

try this:

import java.awt.event.actionevent; import java.awt.event.keyevent; import javax.swing.*; @suppresswarnings("serial") public class addnewlines extends jpanel { private jtextarea textarea = new jtextarea(10, 15); private jbutton addlinebtn = new jbutton(new addlineaction("add line", keyevent.vk_a)); public addnewlines() { textarea.seteditable(false); textarea.setfocusable(false); textarea.setwrapstyleword(true); textarea.setlinewrap(true); textarea.setopaque(false); jscrollpane scrollpane = new jscrollpane(textarea); scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always); add(scrollpane); add(addlinebtn); } class addlineaction extends abstractaction { private int count = 0; public addlineaction(string name, int mnemonic) { super(name); putvalue(mnemonic_key, mnemonic); } @override public void actionperformed(actionevent e) { if (count != 0) { textarea.append("\n"); } textarea.append("line of text: " + count); count++; } } private static void createandshowgui() { addnewlines mainpanel = new addnewlines(); jframe frame = new jframe("add new lines"); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.getcontentpane().add(mainpanel); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }

java swing resize jpanel jscrollpane

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -