title: Adding a JPanel to another JPanel having TableLayout
body: <p>I am trying to develop a map editor in Java using <a href="http://java.sun.com/products/jfc/tsc/articles/tablelayout/" rel="nofollow noreferrer"><code>TableLayout</code></a>. My map window receives as a constructor a Map object. From that map object i am able to retrieve the Grid and every item in the grid along with other getters and setters.
  The problem is that even though the Mapping extends JComponent, when I place it in a panel it is not painted. I have overridden the paint method to satisfy my needs. Here is the code, maybe you could help me.</p>
  
  <pre><code>public class MapTest extends JFrame implements ActionListener {
  
      private JPanel mainPanel;
      private JPanel mapPanel;
      private JPanel minimapPanel;
      private JPanel relationPanel;
      private TableLayout tableLayout;
      private JPanel tile;
  
      MapTest(Map map) {
          mainPanel = (JPanel) getContentPane();
          mapPanel = new JPanel();
          populateMapPanel(map);
          mainPanel.add(mapPanel);
          this.setPreferredSize(new Dimension(800, 600));
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.setVisible(true);
      }
  
      private double[][] generateTableLayoutSize(int x, int y, int size) {
          double panelSize[][] = new double[x][y];
          for (int i = 0; i &lt; x; i++) {
              for (int j = 0; j &lt; y; j++) {
                  panelSize[i][j] = size;
              }
          }
          return panelSize;
      }
  
      private void populateMapPanel(Map map) {
          double[][] layoutSize = generateTableLayoutSize(map.getMapGrid().getRows(), map.getMapGrid().getColumns(), 50);
          tableLayout = new TableLayout(layoutSize);
  
          for(int i = 0; i &lt; map.getMapGrid().getRows(); i++)
          {
              for(int j = 0; j &lt; map.getMapGrid().getColumns(); j++)
              {
                  tile = new JPanel();
                  tile.setName(String.valueOf(((Mapping)map.getMapGrid().getItem(i, j)).getCharacter()));
                  tile.add(map.getMapItem(i, j));
                  String constraint = i + "," + j;
                  mapPanel.add(tile, constraint);
              }
          }
          mapPanel.validate();
          mapPanel.repaint();
      }
  
      public void actionPerformed(ActionEvent e) {
          throw new UnsupportedOperationException("Not supported yet.");
      }
  } 
  </code></pre>
  
  <p>My Mapping Class</p>
  
  <pre><code>public class Mapping extends JComponent implements Serializable{
  
      private BufferedImage image;
      private Character character;
  
      //default
      public Mapping() {
          super();
          this.image = null;
          this.character = '\u0000';
      }
  
      //Mapping from image and char
      public Mapping(BufferedImage image, char character) {
          super();
          this.image = image;
          this.character = character;
      }
  
      //Mapping from file and char
      public Mapping(File file, char character) {
          try {
              this.image = ImageIO.read(file);
              this.character = character;
          } catch (IOException ex) {
              System.out.println(ex);
          }
      }
  
      public char getCharacter() {
          return character;
      }
  
      public void setCharacter(char character) {
          this.character = character;
      }
  
      public BufferedImage getImage() {
          return image;
      }
  
      public void setImage(BufferedImage image) {
          this.image = image;
          repaint();
      }
  
      @Override
      /*Two mappings are consider the same if
      -they have the same image OR
      -they have the same character OR
      -both of the above*/
      public boolean equals(Object mapping) {
          if (this == mapping) {
              return true;
          }
          if (mapping instanceof Mapping) {
              return true;
          }
          //WARNING! equals might not work for images
          return (this.getImage()).equals(((Mapping) mapping).getImage())
                  || (this.getCharacter()) == (((Mapping) mapping).getCharacter());
      }
  
      @Override
      public void paintComponent(Graphics g) {
          super.paintComponent(g);
          //g.drawImage(image, 0, 0, null);
          g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
      }
  
  //    @Override
  //    public Dimension getPreferredSize() {
  //        if (image == null) {
  //            return new Dimension(10, 10); //instead of 100,100 set any prefered dimentions
  //        } else {
  //            return new Dimension(100, 100);//(image.getWidth(null), image.getHeight(null));
  //        }
  //    }
      private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
          character = (Character) in.readObject();
          image = ImageIO.read(ImageIO.createImageInputStream(in));
      }
  
      private void writeObject(java.io.ObjectOutputStream out) throws IOException {
          out.writeObject(character);
          ImageWriter writer = (ImageWriter) ImageIO.getImageWritersBySuffix("jpg").next();
          writer.setOutput(ImageIO.createImageOutputStream(out));
          ImageWriteParam param = writer.getDefaultWriteParam();
          param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
          param.setCompressionQuality(0.85f);
          writer.write(null, new IIOImage(image, null, null), param);
      }
  }
  </code></pre>