0

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.

Alex
  • 1,387
  • 2
  • 9
  • 14
  • 1
    Here's a quick tutorial on AWT while I look at this, maybe something will help. Does the code above reproduce the problem? https://www.oracle.com/java/technologies/painting.html – markspace Nov 23 '21 at 17:35
  • 1
    *I wanted to have the seperation lines between the columns and the rows.* - read the `GridLayout` API. You can' specify the horizontal/vertical spacing between components. Therefore the background of the panel will appear as your lines. – camickr Nov 23 '21 at 18:14
  • 2
    Your painting code is incorrect: 1) no need to override update(). All painting is done in the paint() method. 2) painting is done relative to the component, not the panel. So the x/y values should be 0 and you need to subtract 1 from the width/height. – camickr Nov 23 '21 at 18:21

2 Answers2

0

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());
    }
}
markspace
  • 10,621
  • 3
  • 25
  • 39
0

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?!

By the way, while paint is now called, the blue border is still not rendered. I replaced "setPaint" with "setColor" without success. I also tried the proposal of setting a hgap and vgap, but since the labels have the same background color as the Container of the Table, the gaps are invisible. And when setting the background of the container the labels are changed but not the container. That is an issue with Container. After I changed that Table is no longer extending Container but Panel, changes to the background of the Table are no longer forwarded to the labels. Now I can set different colors to the table and the labels using setBackground since whatever I coded in the paint method had no effect. I guess there was a reason to come up with Swing ;-)

Alex
  • 1,387
  • 2
  • 9
  • 14