java - How to prevent enlarging JFormattedTextField for BoxLayout? -
i have jformattedtextfield
components in 1 column. don´t understand why code causes enlarge maximum size.
// create text fields , set them up. priemerkolesa = new jformattedtextfield(numberformat); priemerosi = new jformattedtextfield(numberformat); hrubkaplasta = new jformattedtextfield(numberformat); hrubkakolesa = new jformattedtextfield(numberformat); hrubkanavinu = new jformattedtextfield(numberformat); hrubkanavinu.setcolumns(6); // layout text fields in panel. jpanel fieldpane = new jpanel(); fieldpane.setlayout(new boxlayout(fieldpane, boxlayout.y_axis)); fieldpane.add(priemerkolesa); fieldpane.add(priemerosi); fieldpane.add(hrubkaplasta); fieldpane.add(hrubkakolesa); fieldpane.add(hrubkanavinu);
i want have in nice stack, not expanded this.
it seems boxlayout
stretches components fit available space. 1 way around first put each component jpanel
flowlayout
(which not stretch components within) add panel container using boxlayout
. e.g. in form of mcve mentioned above:
import java.awt.*; import java.text.*; import javax.swing.*; import javax.swing.border.emptyborder; public class boxlayoutquandary { private jcomponent ui = null; boxlayoutquandary() { initui(); } public void initui() { if (ui != null) { return; } ui = new jpanel(new borderlayout(4, 4)); ui.setborder(new emptyborder(4, 4, 4, 4)); numberformat numberformat = new decimalformat("##.#"); // create text fields , set them up. jformattedtextfield priemerkolesa = new jformattedtextfield(numberformat); priemerkolesa.setcolumns(8); jformattedtextfield hrubkanavinu = new jformattedtextfield(numberformat); hrubkanavinu.setcolumns(6); // layout text fields in panel. jpanel fieldpane = new jpanel(); fieldpane.setlayout(new boxlayout(fieldpane, boxlayout.y_axis)); ui.add(fieldpane); jpanel priemerkolesapanel = new jpanel(); priemerkolesapanel.add(priemerkolesa); fieldpane.add(priemerkolesapanel); jpanel hrubkanavinupanel = new jpanel(); hrubkanavinupanel.add(hrubkanavinu); fieldpane.add(hrubkanavinupanel); } public jcomponent getui() { return ui; } public static void main(string[] args) { runnable r = new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (exception usedefault) { } boxlayoutquandary o = new boxlayoutquandary(); jframe f = new jframe(o.getclass().getsimplename()); f.setdefaultcloseoperation(jframe.dispose_on_close); f.setlocationbyplatform(true); f.setcontentpane(o.getui()); f.pack(); f.setminimumsize(f.getsize()); f.setvisible(true); } }; swingutilities.invokelater(r); } }
Comments
Post a Comment