The curriculum for my son's school still includes the students learning Java AWT. Even though I don't think it is the best approach, I can't change it, because it is set by the Ministry of Education. Anyway, I try to help my son with his exercises, as I programmed AWT myself many years ago. But I have a problem with rendering labels. What I am actually after is to create a simple table in AWT, since tables are missing. The approach is to take a GridLayout and add Labels to it. This works fine but I wanted to have the seperation lines between the columns and the rows. Therefore I sub-classed Label and overwrote paint like so:
In the Table class I do
public class Table extends Container {
...
this.setLayout(new GridLayout(rows+1, cols));
for(int i=0; i<cols; i++) {
for(int y=0; y< rows; y++) {
TableLabel label = new TableLabel(rowData[i][y].toString());
add(label);
}
}
...
}
which refers to
public class TableLabel extends Label {
public TableLabel(String labelText) {
super(labelText);
}
public void paint(Graphics g) {
super.paint(g);
System.out.println("paint");
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.BLUE);
g2.drawRect(getX(), getY(), getWidth(), getHeight());
}
public void update(Graphics g) {
super.update(g);
System.out.println("update");
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.BLUE);
g2.drawRect(getX(), getY(), getWidth(), getHeight());
}
}
The rendering didn't change. According to the debugger neither paint nor update are ever called. Also the println is not writing to the console. What am I missing? The application is compiled and runs under Java 11. I know, AWT and version 11 is kind of wired, but that is the given setup. Thank you in advance.