Friday, 15 April 2011

java - Swing display JPanels in a row to the right corner -



java - Swing display JPanels in a row to the right corner -

i creating custom decoration first customized swing programme window, started layout managers, , looks doing wrong, first used borderlayout , borderlayout.east or west display on corner, allows 1 panel displayed on corner, won't display in row.

looks this:

with code:

this.panel.setlayout(new borderlayout()); this.panel.add(this.createtoolbutton("x"), borderlayout.east);

but if add together panel, newest panel on previous 1 (note used panels because jbutton hates me, it's default styles doesn't allow me create flat)

now used gridbaglayout

this.panel.setlayout(new gridbaglayout()); box panels = new box(boxlayout.x_axis); panels.add(this.createtoolbutton("x")); this.panel.add(panels, borderlayout.east);

but on run get

exception in thread "awt-eventqueue-0" java.lang.illegalargumentexception: cannot add together layout: constraints must gridbagconstraint @ java.awt.gridbaglayout.addlayoutcomponent(unknown source)

what doing wrong? how can have panels floated right 1 one?

edit:

this.panel.setlayout(new gridbaglayout()); gridbagconstraints gc = new gridbagconstraints(); gc.fill = gridbagconstraints.west; this.panel.add(this.createtoolbutton("x"), gc);

exception in thread "awt-eventqueue-0" java.lang.illegalargumentexception: cannot add together layout: constraints must gridbagconstraint @ java.awt.gridbaglayout.addlayoutcomponent(unknown source)

use gridbagconstraint gridbaglayout.

this.panel.setlayout(new gridbaglayout()); gridbagconstraint gc = new gridbagconstraint(); // set different properties of gridbagconstraint per need this.panel.add(panels, gc);

read more how utilize gridbaglayout read more properties of gridbagconstraint.

here the example larn more it.

edit

you can seek flowlayout right alignment:

jpanel titlepanel=new jpanel(new gridlayout(1,2)); titlepanel.setborder(new lineborder(color.black)); titlepanel.setbackground(color.light_gray); jpanel panel = new jpanel(new flowlayout(flowlayout.right, 0, 0)); panel.setbackground(color.light_gray); titlepanel.add(new jlabel("title",jlabel.left)); panel.add(new jbutton("x")); titlepanel.add(panel); frame.add(titlepanel, borderlayout.north); // add together panel in north section of undecorated jframe // default uses borderlayout

snapshot:

java swing layout jpanel gridbaglayout

No comments:

Post a Comment