Trouble with Java for-loop logic -
i having trouble understanding why loop isn't behaving it. purpose loop add multiple textfields onto gui, 70 exact. 7 across, 10 down. adds fields fine, stops short 1 row , 1 column want. seems enough information identify problem, can't, came here.
for(int = 0; < 6; i++){ for(int j = 0; j < 9; j++){ ot2field[i][j] = new jtextfield(); ot1field[i][j] = new jtextfield(); stfield[i][j] = new jtextfield(); } } int xpointer = 3; int ypointer = 7; for(int = 0; < 6; i++){ for(int j = 0; j < 9; j++){ addtimefieldborder0010(option3, ot2field[i][j], gridbag, gbc, xpointer, ypointer, 1, 1, 0); ypointer = ypointer + 3; } xpointer++; ypointer = 7; } } private void addtimefieldborder0010(jcomponent container, jcomponent component, gridbaglayout gridbag, gridbagconstraints gbc, int x, int y, int height, int width, double weightx) { gbc.gridx = x; gbc.gridy = y; gbc.gridheight = height; gbc.gridwidth = width; gbc.weightx = weightx; ((jtextfield) component).sethorizontalalignment(jlabel.center); component.setborder(borderfactory.creatematteborder(0, 0, 1, 0, color.red)); component.setfont(component.getfont().derivefont(18.0f)); component.setforeground(color.red); component.setbackground(color.yellow); gridbag.setconstraints(component, gbc); container.add(component, gbc); }
according java language specification §15.20.1,
- the value produced
<
operatortrue
if value of left-hand operand less value of right-hand operand, , otherwisefalse
.
so starting @ i = 0
, looping while i
less 6. need loop while less 7, or less or equal 6. same applies next loop.
change 2 loops to:
for(int = 0; < 7; i++){ for(int j = 0; j < 10; j++){ //stuff } }
Comments
Post a Comment