Search code examples
javaswingjtablejscrollpanejtableheader

how to put name to columns to one jtable?


public void fillTable()
{
  conexiones.daofutbolista search = new conexiones.daofutbolista();
  ResultSet datostraidos =search.buscarfutbolistas(); 

  try
  {
    model.addColumn("numero"); 
    model.addColumn("equipo"); 
    model.addColumn("posicion");
    model.addColumn("Numero"); 
    model.addColumn("apodo"); 
    model.addColumn("Liguilla");
    model.addColumn("Edad");  
    while(datostraidos.next() )
    {
      Object [] fila = new Object[7];
      for(int i=0;i<7;i++) 
      {
        fila[i]=datostraidos.getObject(i+1);
        System.out.println("for");
      }
      System.out.println("while");
      model.addRow(fila);
    }
    model.addRow(columnasname);
    gridview.setModel(model);
  }
  catch(Exception es)
  {
    System.out.println(es);
  }
}

i have that method for fill my jtable with a query but my jtable' columns don't have a name, how do i put it? i dont want to delete the query gotten.


Solution

  • What does your table model looks like? Are you using a default table model?

    Also, do you use a JSCrollPane? If not, adding it to a JSCrollPane works most of the times.

    See this:

    http://download.oracle.com/javase/tutorial/uiswing/components/table.html#show

    In this code I have two tables, one with JScrollPane, one without.

    Look how the second (placed at the bottom) doesn't have headers.

    By the way, this is a nice trick to provide a "results" column ;)

    using scrollpane