swing - Java- TitledBorder in gridbag layout expanding as the window expands -
i making form using gridbag
layout has titled border. first titledborder
panel
customer details works fine except know how add spacing between first title , textfield
(eg first name) , following title , textfield
(eg last name) below it.
the problem second panel
room details expands enlarge/expand window , happens, components inside shift. remain fixed components in first panel
.
this form.java class:
public class form extends jframe{ jpanel pnl= new jpanel(); jpanel pnl1= new jpanel(); jlabel fname= new jlabel("first name: "); jlabel lname= new jlabel("last name: "); jlabel contact= new jlabel("contact number: "); jlabel email= new jlabel("email address: "); jlabel address= new jlabel("address: "); jlabel numpsns= new jlabel("number of persons: "); jtextfield fnamefield= new jtextfield(25); jtextfield lnamefield= new jtextfield(25); jtextfield contactfield= new jtextfield(25); jtextfield emailfield= new jtextfield(25); jtextarea txtadd= new jtextarea(5, 25); spinnermodel sm= new spinnernumbermodel(1,0,30,1); jspinner spinner= new jspinner(sm); public form(){ this.settitle("reservation form"); pnl.setborder(new titledborder(null,"customer details", titledborder.center, titledborder.top, null, null)); getcontentpane().add(pnl, borderlayout.north); pnl.setlayout(new gridbaglayout()); gridbagconstraints gc= new gridbagconstraints(); //first column of grid// gc.anchor= gridbagconstraints.east; gc.weightx=0.5; gc.weighty=0.5; gc.gridx=0; gc.gridy=0; pnl.add(fname, gc); gc.gridx=0; gc.gridy=1; pnl.add(lname,gc); gc.gridx=0; gc.gridy=2; pnl.add(contact, gc); gc.gridx=0; gc.gridy=3; pnl.add(email, gc); gc.gridx=0; gc.gridy=4; pnl.add(address, gc); //second column// gc.anchor= gridbagconstraints.west; gc.gridx=1; gc.gridy= 0; pnl.add(fnamefield,gc); gc.gridx=1; gc.gridy=1; pnl.add(lnamefield, gc); gc.gridx=1; gc.gridy=2; pnl.add(contactfield, gc); gc.gridx=1; gc.gridy=3; pnl.add(emailfield, gc); gc.gridx=1; gc.gridy=4; pnl.add(txtadd, gc); //second titled border// pnl1.setborder(borderfactory.createtitledborder(null, "booking details", titledborder.center, titledborder.center)); add(pnl1, borderlayout.center); pnl1.setlayout(new gridbaglayout()); gridbagconstraints gc1= new gridbagconstraints(); //first column// gc1.weightx= 0.5; gc1.weighty=0.5; gc1.gridx=0; gc1.gridy=0; pnl1.add(numpsns, gc1); gc1.anchor= gridbagconstraints.west; gc1.gridx=1; gc1.gridy= 0; pnl1.add(spinner,gc1); } }
form_main.java class
public class form_main { public static void main(string[] args) { form form_display= new form(); form_display.setdefaultcloseoperation(jframe.dispose_on_close); form_display.pack(); form_display.setsize(500,280); form_display.setvisible(true); } }
you've added second panel centre
position of borderlayout
, expected behaviour type of layout. perhaps should use gridbaglayout
layout 2 panels
to add internal spacing panel use compoundlayout
, wrapping titledborder
, emptyborder
or set gridbagconstraints#insets
property
simple example...
import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import javax.swing.borderfactory; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jspinner; import javax.swing.jtextarea; import javax.swing.jtextfield; import javax.swing.spinnermodel; import javax.swing.spinnernumbermodel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.border.border; import javax.swing.border.compoundborder; import javax.swing.border.emptyborder; import javax.swing.border.titledborder; public class form extends jframe { jpanel pnl = new jpanel(); jpanel pnl1 = new jpanel(); jlabel fname = new jlabel("first name: "); jlabel lname = new jlabel("last name: "); jlabel contact = new jlabel("contact number: "); jlabel email = new jlabel("email address: "); jlabel address = new jlabel("address: "); jlabel numpsns = new jlabel("number of persons: "); jtextfield fnamefield = new jtextfield(25); jtextfield lnamefield = new jtextfield(25); jtextfield contactfield = new jtextfield(25); jtextfield emailfield = new jtextfield(25); jtextarea txtadd = new jtextarea(5, 25); spinnermodel sm = new spinnernumbermodel(1, 0, 30, 1); jspinner spinner = new jspinner(sm); public form() { this.settitle("reservation form"); setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = gridbagconstraints.remainder; gbc.fill = gbc.horizontal; border border = new compoundborder( new titledborder(null, "customer details", titledborder.center, titledborder.top, null, null), new emptyborder(10, 10, 10, 10)); pnl.setborder(border); getcontentpane().add(pnl, gbc); pnl.setlayout(new gridbaglayout()); gridbagconstraints gc = new gridbagconstraints(); //first column of grid// gc.anchor = gridbagconstraints.east; gc.weightx = 0.5; gc.weighty = 0.5; gc.gridx = 0; gc.gridy = 0; pnl.add(fname, gc); gc.gridx = 0; gc.gridy = 1; pnl.add(lname, gc); gc.gridx = 0; gc.gridy = 2; pnl.add(contact, gc); gc.gridx = 0; gc.gridy = 3; pnl.add(email, gc); gc.gridx = 0; gc.gridy = 4; pnl.add(address, gc); //second column// gc.anchor = gridbagconstraints.west; gc.gridx = 1; gc.gridy = 0; pnl.add(fnamefield, gc); gc.gridx = 1; gc.gridy = 1; pnl.add(lnamefield, gc); gc.gridx = 1; gc.gridy = 2; pnl.add(contactfield, gc); gc.gridx = 1; gc.gridy = 3; pnl.add(emailfield, gc); gc.gridx = 1; gc.gridy = 4; pnl.add(txtadd, gc); //second titled border// pnl1.setborder(borderfactory.createtitledborder(null, "booking details", titledborder.center, titledborder.center)); add(pnl1, gbc); pnl1.setlayout(new gridbaglayout()); gridbagconstraints gc1 = new gridbagconstraints(); //first column// gc1.weightx = 0.5; gc1.weighty = 0.5; gc1.gridx = 0; gc1.gridy = 0; pnl1.add(numpsns, gc1); gc1.anchor = gridbagconstraints.west; gc1.gridx = 1; gc1.gridy = 0; pnl1.add(spinner, gc1); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } form frame = new form(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } }
thanks lot. can show me how can change font , size of titles(like customer details, booking details) of borders? font.bold doesn't seem work in createtitledborder
...
new titledborder(null, "customer details", titledborder.center, titledborder.top, uimanager.getfont("label.font").derivefont(font.bold), null);
...
that didn't work
works fine me...
import java.awt.dimension; import java.awt.eventqueue; import java.awt.font; import java.awt.graphics; import java.awt.graphics2d; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.border.titledborder; public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { public testpane() { setlayout(new gridbaglayout()); jpanel p1 = new jpanel(); p1.setborder(new titledborder(null, "customer details", titledborder.center, titledborder.top, null, null)); jpanel p2 = new jpanel(); p2.setborder(new titledborder(null, "customer details", titledborder.center, titledborder.top, uimanager.getfont("label.font").derivefont(font.bold), null)); jpanel p3 = new jpanel(); p3.setborder(new titledborder(null, "customer details", titledborder.center, titledborder.top, uimanager.getfont("label.font").derivefont(font.italic), null)); jpanel p4 = new jpanel(); p4.setborder(new titledborder(null, "customer details", titledborder.center, titledborder.top, uimanager.getfont("label.font").derivefont(font.bold | font.italic), null)); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = gridbagconstraints.remainder; gbc.weightx = 1; gbc.weighty = 1; gbc.fill = gridbagconstraints.both; add(p1, gbc); add(p2, gbc); add(p3, gbc); add(p4, gbc); } @override public dimension getpreferredsize() { return new dimension(200, 200); } } }
Comments
Post a Comment