2016-09-26 95 views
1

JTableJScrollPane不会显示列标题。带JScrollPane的表格将不会显示列标题

package jTable; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 

public class Main { 

    JFrame frame ; 
    Main() 
    {  
    frame = new JFrame() ; 
    String [][] names = { 
      {"1000","yamen","develeoper"}, 
      {"2000","aymen","data entry"}, 
      {"3000","mohammed","teacher"} 
      }; 

     String[] header = {"ID","NAME","JOB"};  
     JTable jtb = new JTable(names,header) ;   
     JScrollPane s = new JScrollPane(jtb); 

     jtb.setBounds(40,50,300,400); 
     frame.add(jtb); 

     frame.setSize(400, 500); 
     frame.setVisible(true); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    } 

    public static void main(String[] args) { 
     new Main(); 
    } 
} 
+0

[* JTable可能重复将不显示列标题*](http://stackoverflow.com/q/2320812/230513)。 – trashgod

回答

0

尝试建立与名称和标题

tableModel = new DefaultTableModel(names, headers); 

一个TableModel然后分配JTable这个TableModel

jtb.setModel(tableModel); 

,然后添加一个新的JScrollPane在框架

frame.add(new JScrollPane(jtb)); 

至少,这是我在程序中做到的。