代码之家  ›  专栏  ›  技术社区  ›  John R Doner

jtable不显示列标题

  •  75
  • John R Doner  · 技术社区  · 14 年前

    我有下面的代码来实例化一个jtable:这个表给出了正确的行数和列数,但是列上没有标题的符号。

    public Panel1()
    {
        int  nmbrRows;
    
        setLayout(null);
        setBackground(Color.magenta);
        Vector colHdrs;
    
        //create column headers
    
        colHdrs = new Vector(10);
        colHdrs.addElement(new String("Ticker"));
    
        // more statements like the above to establish all col. titles       
    
        nmbrRows = 25;
        DefaultTableModel tblModel = new DefaultTableModel(nmbrRows, colHdrs.size());
        tblModel.setColumnIdentifiers(colHdrs);
    
        scrTbl = new JTable(tblModel);
        scrTbl.setBounds(25, 50, 950, 600);
        scrTbl.setBackground(Color.gray);
        scrTbl.setRowHeight(23);    
        add(scrTbl);
    
    //rest of constructor
    ...
    
    }
    

    将此代码与其他表生成代码进行比较,我看不到任何缺少的步骤,但必须缺少一些内容。

    4 回复  |  直到 5 年前
        1
  •  164
  •   Erkan Haspulat    14 年前

    把你的 JTable 里面 JScrollPane . 试试这个:

    add(new JScrollPane(scrTbl));
    
        2
  •  21
  •   Community Neeleshkumar S    7 年前

    此答案与接受答案之间的主要区别在于使用了 setViewPortView(). instead of add().

    How to Put jtable in jscrollpane using eclipse ide:

    1. create jscrollpane container via design tab.
    2. Stretch jscrollpane to desired size(applies to absolute layout).
    3. 拖放 jtable component on top of jscrollpane (viewport area)。

    在结构>组件中, table 应是 scrollbane的子级

    生成的代码如下所示:

    jscrollpane scrollpane=new jscrollpane();
    ……
    
    jtable table=new jtable();
    scrollbane.setviewportview(表);
    < /代码> 
    fsetViewportView() instead of add().

    如何放置JTable在里面JScrollPane使用Eclipse IDE:

    1. 创造滚动面板通过设计选项卡的容器。
    2. 拉伸滚动面板到所需大小(适用于绝对布局)。
    3. 拖放JTHT组件位于滚动面板(观察区)。

    在结构组件中,table应该是的孩子scrollPane. enter image description here

    生成的代码如下所示:

    JScrollPane scrollPane = new JScrollPane();
    ...
    
    JTable table = new JTable();
    scrollPane.setViewportView(table);
    
        3
  •  7
  •   Tobias Sjöbeck    9 年前

    如前所述,“正常”方法是将其添加到JScrollPane,但有时您不希望它滚动(不要问我什么时候)。然后您可以自己添加TableHeader。这样地:

    JPanel tablePanel = new JPanel(new BorderLayout());
    JTable table = new JTable();
    tablePanel.add(table, BorderLayout.CENTER);
    tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
    
        4
  •  1
  •   chaithra_a    7 年前
        public table2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 485, 218);
        setTitle("jtable");
        getContentPane().setLayout(null);
    
        String data[][] = { { "Row1/1", "Row1/2", "Row1/3" },
                { "Row2/1", "Row2/2", "Row2/3" },
                { "Row3/1", "Row3/2", "Row3/3" },
                { "Row4/1", "Row4/2", "Row4/3" }, };
    
        String header[] = { "Column 1", "Column 2", "Column 3" };
    
        // Table
        JTable table = new JTable(data,header);
    
    
        // ScrollPane
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(36, 37, 407, 79);
        getContentPane().add(scrollPane);
    
       }
    

    }

    试试这个!!