java - Centering Spanned Columns with GridBagLayout -


i have jdialog in have series of columns on want center jlabel. cannot seem find on how that. can center within cell not within spanned set of columns. labels in image "target" , "change" need centered. time line settings

here sscce produces jdialog.

package stokermonitor;  import java.awt.dimension; import java.awt.font; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import java.awt.dialog.modalitytype;  import javax.swing.jdialog; import javax.swing.jlabel; import javax.swing.jseparator; import javax.swing.swingconstants;   public class test {  static jdialog timelinedialog; static int row=0;  public static void main(string[] args) {     timelinedialog = new jdialog();     timelinedialog.setlayout(new gridbaglayout());     timelinedialog.setmodalitytype(modalitytype.modeless);     timelinedialog.settitle("time line settings");     timelinedialog.setdefaultcloseoperation(jdialog.do_nothing_on_close);     jlabel timelabel = new jlabel("time");     jlabel actionlabel = new jlabel("action");     jlabel probetemplabel=null;     jlabel pittemplabel=null;     jlabel targetheader=new jlabel("target");     font boldfont=targetheader.getfont().derivefont(font.bold, (float) 14);     targetheader.setfont(boldfont);     jlabel changeheader=new jlabel("change");     changeheader.setfont(boldfont);     if (configuration.getinstance().celsius) {         probetemplabel = new jlabel("temp (\u00b0 c)");         pittemplabel = new jlabel("temp (\u00b0 c)");     }     else {         probetemplabel = new jlabel("temp (\u00b0 f)");         pittemplabel = new jlabel("temp (\u00b0 f)");     }     jlabel meatlabel=new jlabel("meat");     jlabel cooktimelabel=new jlabel("est. time");     jlabel weightlabel=new jlabel("weight");     jlabel probelabel=new jlabel("probe");     jlabel pitlabel=new jlabel("pit");     setnewseparator(1,row);     gridbagconstraints gbc=makegbc(2, row);     gbc.gridwidth=7;     gbc.fill=gridbagconstraints.horizontal;     gbc.anchor=gridbagconstraints.center;     timelinedialog.add(targetheader,gbc);     setnewseparator(9,row);     timelinedialog.add(changeheader,makegbc(10, row));     setnewseparator(12,row++);     setnewseparator(1,row);     timelinedialog.add(timelabel, makegbc(2, row));     timelinedialog.add(probetemplabel,makegbc(3, row));     timelinedialog.add(meatlabel,makegbc(4, row));     timelinedialog.add(weightlabel,makegbc(5, row));     timelinedialog.add(cooktimelabel,makegbc(6, row));     timelinedialog.add(probelabel,makegbc(7, row));     timelinedialog.add(actionlabel, makegbc(8, row));     setnewseparator(9,row);     timelinedialog.add(pitlabel,makegbc(10, row));     timelinedialog.add(pittemplabel, makegbc(11, row++));     setnewseparator(12,row);     timelinedialog.pack();     timelinedialog.setlocationrelativeto(gui.getinstance().getframe());     timelinedialog.setvisible(true); }  static void setnewseparator(int column_,int row_) {     jseparator sep=new jseparator(swingconstants.vertical);     sep.setpreferredsize(new dimension(1,1));     gridbagconstraints gbc=makegbc(column_, row_);     gbc.fill=gridbagconstraints.vertical;     gbc.weighty=1.;     timelinedialog.add(sep,gbc); }  static gridbagconstraints makegbc(int x, int y) {       gridbagconstraints gbc = new gridbagconstraints();       insets west_insets=new insets(5,0,5,5);       insets east_insets=new insets(5,5,5,0);       gbc.gridx = x;       gbc.gridy = y;       gbc.gridwidth = 1;       gbc.gridheight = 1;        gbc.anchor = (x == 0) ? gridbagconstraints.west : gridbagconstraints.east;       gbc.fill = (x == 0) ? gridbagconstraints.both             : gridbagconstraints.horizontal;        gbc.insets = (x == 0) ? west_insets : east_insets;       gbc.weightx = (x == 0) ? 0.1 : 1.0;       gbc.weighty = 1.0;       return gbc;    } } 

can explain need center headers? tia.

tip: when debugging layouts, find useful set each component have different background color. can see area assigned / filled particular component. components default transparent background, means bounds not visible. (eg targetheader.setopaque(true); targetheader.setbackground(color.red);)

the situation have layout jlabel fills 7 columns specified. however, label draws text contains in leftmost part of space fills.

there 2 alternative solutions:

  1. tell label want center text within label: targetheader.sethorizontalalignment(jlabel.center);

  2. alternatively, can tell layout not fill space. gbc.fill=gridbagconstraints.none;


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -