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.
CodePudding user response:
The following code works for me. Can you expand your code to include a reproducible problem? And please describe exactly what the problem is so we can be sure we're all looking at the same thing.
public class AwtPaint {
public static void main( String[] args ) {
SwingUtilities.invokeLater( AwtPaint::init );
}
private static void init() {
JFrame frame = new JFrame();
Table myTable = new Table( 3, 3 );
frame.add( myTable );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
class Table extends Container {
public Table( int rows, int cols ) {
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( "Col: " i ", row: " y );
add(label);
}
}
}
}
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());
}
}
CodePudding user response:
Thank you for all the feedback. I still don't know what was actually wrong, but when I followed Joop's proposal and entered "@Override", Eclipse showed me an error, saying that my signature is unknown. I couldn't see a difference to the documentation, but deleted the code and created the method again, this time using the Overwrite functionality of Eclipse. Now it works. Maybe a non printable character somewhere in the line?!