JTable不显示列名称 [英] JTable doesn't show columns names

查看:113
本文介绍了JTable不显示列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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);

    }

但是当我运行我的代码时,表格不会显示列名。当我将1设置为此行时,它只显示一个空行: DefaultTableModel model = new DefaultTableModel(columns,1);

But when I run my code, the table does not display column names. It just shows one empty row as I set 1 to this line: DefaultTableModel model = new DefaultTableModel(columns,1);

请告诉我我的代码错在哪里?
谢谢!

Please show me where my code is wrong?? Thanks!

编辑:有人问同样的问题(在JTable中没有标题),但答案是将Jtable添加到JScrollPane,这对@@没有帮助
编辑:您好 Dan ,我添加了完整的构造函数,这里是 readDownloadList()方法的代码:

Edited: As someone asked same question (No titles in JTable) but the answer is add Jtable to a JScrollPane and this does not help @@ Edited: Hi Dan, I've added my full Constructor, and here is the code of the readDownloadList() method:

void readDownloadList(){
            File file = new File("downloadlist.dat");
            ObjectInputStream ois = null;
            if(!file.exists())
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            try {
                ois = new ObjectInputStream(new FileInputStream(file));
                downloadList = (ArrayList<DownloadItem>) ois.readObject();
                ois.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                downloadList = new ArrayList<DownloadItem>();
                e.printStackTrace();
            }catch(ClassNotFoundException e){
                downloadList = new ArrayList<DownloadItem>();
                e.printStackTrace();
            }
        }

谢谢!

推荐答案

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

You used the JScrollPane in a wrong way. To make it work fine, just do the following.

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

private JScrollPane scrollPane = new JScrollPane(contentTable);

注释掉用于添加 JTable的行 JScrollPane

// scrollPane.add(contentTable);

当你将组件放在 JScrollPane ,你提到哪个是要应用滚动的视图。

When you put the component inside the constructor of a JScrollPane, you mention which is the view to which the scroll to be applied for.

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

On the other side, using the add method, you just add a component to a container, like adding it to a JPanel. This way, you don't specify the component to add the scroll bars to.

这篇关于JTable不显示列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆