Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTable不';不显示列名称_Java_Swing_Jtable_Jscrollpane_Jtableheader - Fatal编程技术网

Java JTable不';不显示列名称

Java JTable不';不显示列名称,java,swing,jtable,jscrollpane,jtableheader,Java,Swing,Jtable,Jscrollpane,Jtableheader,这是我的密码: public class DownloadMainView extends JFrame{ private ArrayList<DownloadItem> downloadList = new ArrayList<DownloadItem>(); private JMenuBar menubar = new JMenuBar(); private JMenu m_task = new JMenu("Tasks"); priv

这是我的密码:

public class DownloadMainView extends JFrame{
    private ArrayList<DownloadItem> downloadList = new ArrayList<DownloadItem>();
    private JMenuBar menubar = new JMenuBar();
    private JMenu m_task = new JMenu("Tasks");
    private JMenu m_tool = new JMenu("Tools");
    private JMenu m_help = new JMenu("Help");

    private JMenuItem mi_add = new JMenuItem("Add");
    private JMenuItem mi_exit = new JMenuItem("Exit");
    private JMenuItem mi_options = new JMenuItem("Options");
    private JMenuItem mi_help = new JMenuItem("Help");
    private JMenuItem mi_about = new JMenuItem("About");

    private JTree categoryTree = new JTree();
    private JTable contentTable = new JTable(new Object[][]{},new Object[]{"No.","Filename","URL","Status","Size","Added Date"});
    private JToolBar toolbar = new JToolBar();
    private JScrollPane scrollPane = new JScrollPane();

    private JButton btnAdd = new JButton("Add");
    private JButton btnCancel = new JButton("Cancel");
    private JButton btnDelete = new JButton("Delete");
    private JButton btnOption = new JButton("Option");

    public DownloadMainView() throws IOException{
        super("KPDownloader");
        setSize(800,400);
        setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //Build menubar
        menubar.add(m_task);
        menubar.add(m_tool);
        menubar.add(m_help);
        m_task.add(mi_add);
        mi_add.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK));
        m_task.add(new JSeparator());
        m_task.add(mi_exit);
        mi_exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.ALT_MASK));
        m_tool.add(mi_options);
        mi_options.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
        m_help.add(mi_help);
        mi_help.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0));
        m_help.add(mi_about);
        mi_about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,ActionEvent.CTRL_MASK));
        setJMenuBar(menubar);
        //about buttons
        toolbar.add(btnAdd);        
        toolbar.add(btnOption);
        toolbar.add(btnCancel);
        toolbar.add(btnDelete);
        toolbar.setLocation(0, 0);
        toolbar.setSize(800,42);
        this.add(toolbar);
        //add table to mainview
        String columns[] = {"No.","Filename","URL","Status","Size","Added Date"};
        DefaultTableModel model = new DefaultTableModel(columns,1);
        readDownloadList();
        if(downloadList != null){
            int length = downloadList.size();
            for(int i = 0; i < length; i++)
                model.insertRow(i, new Object[]{i,
                        downloadList.get(i).getFilename(),downloadList.get(i).getSize(),
                        downloadList.get(i).getStatus(),
                        downloadList.get(i).getURL(),downloadList.get(i).getAddedDate()});
        }

        contentTable.setModel(model);
        contentTable.setSize(800, 350);
        scrollPane.add(contentTable);
        scrollPane.setSize(800, 350);
        scrollPane.setLocation(0, 50);
        this.add(scrollPane);

    }

谢谢大家!

您以错误的方式使用了
JScrollPane
。要使其正常工作,只需执行以下操作

JTable
实例传递给构造函数中的
JScrollPane

private JScrollPane scrollPane = new JScrollPane(contentTable);
注释掉用于将
JTable
添加到
JScrollPane
的行:

// scrollPane.add(contentTable);
当您将组件放入
JScrollPane
的构造函数中时,您会提到要应用滚动的视图


另一方面,使用
add
方法,只需将组件添加到容器中,就像将其添加到
JPanel
中一样。这样,您就不会指定要添加滚动条的组件。

通常JScrollPane会有所帮助。请发布一些我们可以运行和尝试的代码,比如我们找出问题所在。嗨,丹,我添加了完整的构造函数,请再次查看!可能重复的谢谢你,它的工作!但这两种方式有什么区别吗?
// scrollPane.add(contentTable);