title: Creating tables in java
body: <p>I am doing my best to just create read-only tables (blank, but ready to get column content and row names) next to a list in a java application but I still cannot figure out what's the problem with these tables</p>
  
  <hr>
  
  <p>I get the exception</p>
  
  <blockquote>
    <p>Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
        at examples.TableData.getColumnCount(TableData.java:25)
        at javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:1285)
        at javax.swing.JTable.tableChanged(JTable.java:4386)
        at javax.swing.JTable.setModel(JTable.java:3688)
        at javax.swing.JTable.(JTable.java:632)
        at javax.swing.JTable.(JTable.java:573)
        at examples.Main$2.run(Main.java:77)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    BUILD SUCCESSFUL (total time: 5 seconds)</p>
    
    <hr>
  </blockquote>
  
  <p>Here is the main file:</p>
  
  <pre><code>/* Програма с графичен интерфейс (swing/awt), която
   * извежда продуктовите категории от базата данни на "northwind"
   * и изкарва информация за продуктите в нея
  */
  
  package examples;
  
  /* Компоненти на swing */
  import javax.swing.SwingUtilities;
  import javax.swing.JList;
  import javax.swing.JFrame;
  import javax.swing.JScrollPane;
  import javax.swing.event.ListSelectionEvent;
  import javax.swing.event.ListSelectionListener;
  import javax.swing.JTable;
  import javax.swing.table.TableColumn;
  import javax.swing.table.TableColumnModel;
  /* Компоненти на awt */
  import java.awt.Dimension;
  import java.awt.GridLayout;
  import java.awt.Container;
  import java.awt.BorderLayout;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseListener;
  import java.awt.event.MouseEvent;
  /* Компоненти на SQL ?? */
  
  public class Main
  {
      public static void main(String[] args)
      {
          /* ЗАГЛАВИЕ на прозорец, добавяне на    КОНТЕЙНЪР в прозореца */
          JFrame frame = new JFrame("Product Exmplorer");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Container contentPane = frame.getContentPane();
  
          /* Използване на JList                  ЛИСТ */
          String labels[] = { "Test1", "Test2" };
          JList jlist = new JList(labels);
  
          /* Слагане на                           ПЛЪЗГАЧИ в контейнъра */
          JScrollPane scrollPane1 = new JScrollPane(jlist);
          contentPane.add(scrollPane1, BorderLayout.EAST);
  
          /**********************************************************************/
          /* Дефиниране на                        ЛИСЪНЪР за клик */
          MouseListener mouseListener = new MouseAdapter()
          {
              public void mouseClicked(MouseEvent mouseEvent)
              {
                  JList theList = (JList) mouseEvent.getSource();
                  if (mouseEvent.getClickCount() == 2)
                  {
                      int index = theList.locationToIndex(mouseEvent.getPoint());
                      if (index &gt;= 0)
                      {
                          Object o = theList.getModel().getElementAt(index);
                          System.out.println("Double-clicked on: " + o.toString());
                      }
                  }
              }
          };
          jlist.addMouseListener(mouseListener);
  
          /**********************************************************************/
  
          Runnable runner = new Runnable() // анонимен клас
          {
              @Override
              public void run()
              {
                  JFrame frame = new JFrame("Button model");
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
                  // създаване на таблицата
                  TableData td = new TableData();
                  JTable table = new JTable(td);
                  table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
  
                  JScrollPane scrollPane = new JScrollPane(table);
                  frame.add(scrollPane , BorderLayout.CENTER);
  
                  frame.pack();
                  frame.setVisible(true);
              }
          };
  
          SwingUtilities.invokeLater(runner);
      }
  }
  </code></pre>
  
  <hr>
  
  <p>and here is the Source Package needed:</p>
  
  <pre><code>package examples;
  
  import java.util.Calendar;
  import java.util.GregorianCalendar;
  import javax.swing.table.AbstractTableModel;
  
  public class TableData extends AbstractTableModel
  {
      //Row &amp; Column Data
      private Object[][] values =
      {
      };
      private final String[] columnHeaders = {
        "Име", "Фамилия", "Роден(а) на", "Сума", "Пол"  
      };
  
      @Override
      public int getRowCount()
      {
          return values.length;
      }
      @Override
      public int getColumnCount()
      {
          return values[0].length;
      }
      @Override
      public Object getValueAt(int rowIndex, int columnIndex)
      {
          return values[rowIndex][columnIndex];
      }
      //--------------------------------------------------------------------------
  }
  </code></pre>
  
  <hr>
  
  <p>I am almost to having my worst nightmares </p>