Reputation: 1240
I am using Java and GridBagLayout to recreate the form seen in the image. In order to create the grid lines under the days of the week I am inserting empty JLabels with borders into the empty cells. This worked perfect for me and then I decided to make each empty cell that is shaded on the form to also be shaded in java, and this is where I am struggling.
My thinking is that I can create a "shade pointer" like I did with my x and y coordinates, this shadePtr will begin with the value of 8 since that is the first row to be shadded, when the loop shades a row, then it increments the shadePtr by 3 since row 11 is the next to be shaded, then 14 and so on.
So far the only bit of success I get is if I comment out the last line of code you see (shadePtr = shadePtr + 3) but then only one row is shaded in. I can't seem to figure out what I am doing wrong here and would appreciate your time and effort.
int yPointer = 7;
int xPointer = 3;
int shadePtr = 8;
for (int j = 0; j <= 299; j++)
{
gbc.gridx = xPointer;
gbc.gridy = yPointer;
gbc.gridheight = 1;
gbc.gridwidth = 1;
if (yPointer == 36) calendarGridLines[j].setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.BLACK)); //if bottom row
else calendarGridLines[j].setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.BLACK));
if (yPointer == shadePtr){ //if row number = shadePtr then color the cell
calendarGridLines[j].setOpaque(true);
calendarGridLines[j].setBackground(Color.GRAY);
}
gridbag.setConstraints(calendarGridLines[j], gbc);
rp.add(calendarGridLines[j]);
xPointer++; //go to next cell in row
j++; //use the next jlabel
gbc.gridx = xPointer;
gbc.gridy = yPointer;
gbc.gridheight = 1;
gbc.gridwidth = 1;
if (yPointer == 36) calendarGridLines[j].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK)); //if bottom row
else calendarGridLines[j].setBorder(BorderFactory.createMatteBorder(1, 1, 0, 1, Color.BLACK));
if (yPointer == shadePtr){ //if row number = shadePtr then color the cell
calendarGridLines[j].setOpaque(true);
calendarGridLines[j].setBackground(Color.GRAY);
}
gridbag.setConstraints(calendarGridLines[j], gbc);
rp.add(calendarGridLines[j]);
xPointer++; //go to next cell in row
if(xPointer == 13) //if end of column then go to next row and reset column pointer to 3 and increment shade pointer by 3
{
yPointer++; //go down a row
xPointer = 3;
shadePtr = shadePtr + 3; //when this line is commented out, one row will be colored; when active none are colored
}
}
Upvotes: 1
Views: 57
Reputation: 17971
So far the only bit of success I get is if I comment out the last line of code you see
(shadePtr = shadePtr + 3)
but then only one row is shaded in. I can't seem to figure out what I am doing wrong here and would appreciate your time and effort.
If I understood your code right the thing is:
yPointer
is the "row" number in your grid.shadePtr
is the index to the next row to be shaded.The problem is you increase yPointer
in 1 unit in each iteration (which is fine) but shadePtr
is increased in 3 units also in each iteration. As yPointer
starts at 7 and shadePtr
starts at 8, then those variables never be equals because shadePtr
will always be greater than yPointer
.
If you comment that last line in the second iteration yPointer == shadePtr == 8
which is the first shaded row. But later yPointer
will be increased and shadePtr
still 8 so no row will be shaded anymore.
The number 3 is important to solve your problem but making this little change at the end:
int yPointer = 7;
int xPointer = 3;
int shadePtr = 8;
for (int j = 0; j <= 299; j++) {
...
if(xPointer == 13) { //if end of column then go to next row and reset column pointer to 3 and increment shade pointer by 3
yPointer++; //go down a row
xPointer = 3;
if((j % 3) == 0) {
shadePtr = yPointer;
}
}
}
This means, if j
is a multiple of 3 then the next row should be shaded so make this assignment: shadePtr = yPointer
after increase yPointer
. This will shade these row numbers: 8, 11, 14, 17, 20, 23, 26, 29, 32 and 35.
In this way your problem should be solved by making a little change but note shadePtr
is actually unnecessary. You can have a simple boolean to know if the row should be shaded:
int yPointer = 7;
int xPointer = 3;
boolean shade = false;
for (int j = 0; j <= 299; j++) {
...
if(xPointer == 13) { // end of column
yPointer++; //go down a row
xPointer = 3;
shade = (j % 3) == 0;
}
}
Upvotes: 2