java - How to force JContainer to re-layout itself? -
i have jframe
borderlayout
: jtextarea
in north
, jbutton
in south
. pack()
in beginning.
my code changes font size text area. how force dialog window , components re-layout itself?
so far tried combinations of:
- another
pack()
repaint()
revalidate()
it not seem help.
is there guaranteed brute force approach? proper way achieve such result?
update:
while creating scce (see below) found 2 mistakes in original code , fixed them. the frame re-sizing nicely now.
i still have question whether proper way it.
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class myframe extends jframe implements actionlistener{ private jtextarea txt; private jbutton bis; private jframe frame; int size = 10; private void buildmaingui() { txt = new jtextarea("this line of text"); bis = new jbutton("increase size"); jpanel p1 = new jpanel(); bis.addactionlistener(this); borderlayout bl = new borderlayout(); p1.setlayout(bl); p1.add(txt, borderlayout.north); p1.add(bis, borderlayout.south); frame = new jframe(); frame.setcontentpane(p1); frame.setvisible(true); frame.pack(); frame.setdefaultcloseoperation(exit_on_close); } @override public void actionperformed(actionevent e) { size += 2; font newfont = new font("courier", font.plain, size); txt.setfont(newfont); frame.revalidate(); frame.pack(); } /** * @param args */ public static void main(string[] args) { myframe mygui = new myframe(); mygui.buildmaingui(); } }
all 3 alternativer described in actionlistener, lets work easiest of them, seems coordinates same (i think required deepest textlayout ???)
import java.awt.borderlayout; import java.awt.font; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtextarea; import javax.swing.swingutilities; public class resizejtextarea { private jframe frame = new jframe(); private jscrollpane scrollpane = new jscrollpane(); private jtextarea textarea = new jtextarea(10, 15); private jbutton button = new jbutton("change"); private font newfont = new font("courier", font.plain, 10); public resizejtextarea() { textarea.settext("this line of text"); textarea.setfont(newfont); button.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { textarea.setfont(textarea.getfont().derivefont(20f)); //2. choice //textarea.setcolumns(20); //textarea.setrows(20); //3rd. coice //override preferredscrollableviewportsize frame.pack(); } }); scrollpane.setviewportview(textarea); frame.add(scrollpane); frame.add(button, borderlayout.south); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocation(100, 100); frame.pack(); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { resizejtextarea fs = new resizejtextarea(); } }); } }
Comments
Post a Comment