简体   繁体   English

列名称未显示在JTable中

[英]column name are not showing in JTable

i am trying to design JTable but column name are not showing i don't know why the array are are declared can some one take a look at this piece of code table = new JTable(data, columnNames); 我正在尝试设计JTable,但没有显示列名。我不知道为什么要声明数组。可以有人看一下这段代码吗: table = new JTable(data,columnNames); i tried to look in java tutorial but i did not find it tell now 我试图看一下Java教程,但现在没有发现

package AnimeAid;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableColumn;


    /**
     *
     * @author isslam
     */
    public class GuiInterface extends JFrame {
        JTable table;

         public static void main(String[] args) {
         GuiInterface is = new GuiInterface("t");
         is.setVisible(true);
         }

        public GuiInterface(String title){
        setSize(900, 700);
        setTitle(title);
        setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
        String[] columnNames = {"#","Start","End","Translation column"};
        Object[][] data = {
        {"1", "00:00:01,600","00:00:04,080", "Mr Magnussen, please state your\n" +
        "full name for the record."},
        {"2", "00:00:04,080 ","00:00:07,040","Charles Augustus Magnussen."}};

        table = new JTable(data, columnNames);
        table.setFillsViewportHeight(true);
        table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
            TableColumn columnA = table.getColumn("#");
            columnA.setMinWidth(10);
            columnA.setMaxWidth(20);
            TableColumn columnB= table.getColumn("Start");
            columnB.setMinWidth(80);
            columnB.setMaxWidth(90);

            TableColumn columnC= table.getColumn("End");
            columnC.setMinWidth(80);
            columnC.setMaxWidth(90);


        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);


        //contaner part
        Container cp = getContentPane();
        cp.add(table);



        }}

The problem is, you've added the table to the content pane. 问题是,您已将该表添加到内容窗格中。 This effectively removes it from the scroll pane, which is responsible for displaying the tables column headers. 这有效地将其从滚动窗格中删除,该窗格负责显示表的列标题。

Remove the following lines 删除以下几行

    Container cp = getContentPane();
    cp.add(table);
package com.tom.mom;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableColumn;


    /**
     *
     * @author isslam
     */
    public class GuiInterface extends JFrame {
        JTable table;

         public static void main(String[] args) {
         GuiInterface is = new GuiInterface("t");
         is.setVisible(true);
         }

        public GuiInterface(String title){
        setSize(900, 700);
        setTitle(title);
        setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
        String[] columnNames = {"#","Start","End","Translation column"};
        Object[][] data = {
        {"1", "00:00:01,600","00:00:04,080", "Mr Magnussen, please state your\n" +
        "full name for the record."},
        {"2", "00:00:04,080 ","00:00:07,040","Charles Augustus Magnussen."}};

        table = new JTable(data, columnNames);
        table.setFillsViewportHeight(true);
        table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
            TableColumn columnA = table.getColumn("#");
            columnA.setMinWidth(10);
            columnA.setMaxWidth(20);
            TableColumn columnB= table.getColumn("Start");
            columnB.setMinWidth(80);
            columnB.setMaxWidth(90);

            TableColumn columnC= table.getColumn("End");
            columnC.setMinWidth(80);
            columnC.setMaxWidth(90);


        JScrollPane scrollPane = new JScrollPane(table);

       add(scrollPane);



}
    }

since you already extended and the JFrame and you are working in the constructor of class which is nothing but a JFrame so you are in that position where there is no need to get the area and put your components.When we do this type of coding in constructor then we do not take getContentPane and place our components.It is required when you are working out of the constructor and trying to put some components in frame 因为您已经扩展了JFrame,并且您正在使用类的构造函数,而该构造函数只不过是JFrame,所以您处于不需要获取区域和放置组件的位置。构造函数,那么我们就不用getContentPane来放置我们的组件了。当您在构造函数中工作并尝试将某些组件放入框架中时,这是必需的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM