java - Expanding a node of a JTree minimizes GridBagLayout -
i have created simple swing application consists of jtoolbar, jtree , rsyntaxtextarea, inside gridbaglayout.
the jtree has 1 top node , has 1 child node. complete ui jtoolbar, jtree , rsyntaxtextarea
when expanding top node of jtree, whole gridbaglayout kind of "minimizes":
i've googled phenominum, since there's no error message or else in console, i'm kind of helpless right now.
i'm using following code create ui:
rsyntaxtextarea textarea = new rsyntaxtextarea(50, 150); textarea.setsyntaxeditingstyle(syntaxconstants.syntax_style_java); textarea.setcodefoldingenabled(true); rtextscrollpane sp = new rtextscrollpane(textarea); cp.setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 0; cp.add(createtoolbar(), c); c.gridx = 0; c.gridy = 1; c.ipadx = 90; c.fill = gridbagconstraints.both; cp.add(createtree(), c); c.gridx = 1; c.gridy = 1; c.fill = gridbagconstraints.horizontal; cp.add(sp, c); ... private jtoolbar createtoolbar() { jtoolbar tb = new jtoolbar("toolbar", jtoolbar.horizontal); jbutton ob = new jbutton(new imageicon("...")); tb.add(ob); tb.setfloatable(false); tb.setrollover(true); return tb; } ... private jtree createtree() { defaultmutabletreenode top = new defaultmutabletreenode("projects"); jtree tree = new jtree(top); defaultmutabletreenode test = new defaultmutabletreenode("i'm test!"); top.add(test); return tree; }
update: minimal code example compile on system testing purposes:
import java.awt.borderlayout; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtoolbar; import javax.swing.jtree; import javax.swing.swingutilities; import javax.swing.tree.defaultmutabletreenode; import javax.swing.jscrollpane; import javax.swing.jtextarea; public class tester extends jframe { public tester () { initializecomponent(); } private void initializecomponent() { jpanel cp = new jpanel(new borderlayout()); jtextarea textarea = new jtextarea(50, 150); jscrollpane sp = new jscrollpane(textarea); cp.setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 0; cp.add(createtoolbar(), c); c.gridx = 0; c.gridy = 1; c.ipadx = 90; c.fill = gridbagconstraints.both; cp.add(createtree(), c); c.gridx = 1; c.gridy = 1; c.fill = gridbagconstraints.horizontal; cp.add(sp, c); setcontentpane(cp); setdefaultcloseoperation(exit_on_close); pack(); setlocationrelativeto(null); } private jtree createtree() { defaultmutabletreenode top = new defaultmutabletreenode("projects"); jtree tree = new jtree(top); defaultmutabletreenode test = new defaultmutabletreenode("i'm test!"); top.add(test); return tree; } private jtoolbar createtoolbar() { jtoolbar tb = new jtoolbar("toolbar", jtoolbar.horizontal); jbutton ob = new jbutton("button"); tb.add(ob); tb.setfloatable(false); tb.setrollover(true); return tb; } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { new tester().setvisible(true); } }); } }
when expanding top node of jtree, whole gridbaglayout kind of "minimizes":
the gridbaglayout shrink minimum size of component when there not enough space display entire component.
swing application consists of jtoolbar, jtree , rsyntaxtextarea, inside gridbaglayout.
i use default borderlayout
of frame:
add(toolbar, borderlayout.page_start); add(treescrollpane, borderlayout.center); add(textareascrollpane, borderlayout.page_end);
note how added jtree jscrollpane. scrollbars appear tree when needed.
if want use gridbaglayout
read section swing tutorial on how use gridbaglayout explanation of how use various constraints. may want start "weightx/y" constraints control components space frame size changed. also, @ "fill" constraint.
Comments
Post a Comment