我的JTable中没有显示数据

时间:2012-10-16 23:31:28

标签: java swing jtable

滚动条和按钮将被绘制,但不会绘制表格中的单元格,并且控制台已正确打印数据,因此我想知道我在哪里做错了。

public class MyGUIManager extends JPanel {

    protected JFrame frame;
    protected JScrollPane scrollPane=null;
    protected JTable table=null;
    protected TableModel model=null;
    protected JButton btnInsert=null;

    protected String dbname,tblname;
    protected Vector<String> colNames;
    protected Vector<Vector<String>> data,backup;
    protected int col;

    protected MySQLGUIManager() throws Exception{

        frame =new JFrame("MySQL Manager");
        frame.setContentPane(this);
        frame.setMinimumSize(new Dimension(800,600));
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        init();
    }

    protected void init() throws Exception{
        //some inrrelevant operations about database
        loadTbl();
    }

    protected void loadTbl() throws SQLException{
        table=new JTable(data,colNames){
            private static final long serialVersionUID = 1L;
            public void tableChanged(TableModelEvent e){
                int column=e.getColumn();
                assert e.getFirstRow()==e.getLastRow():"more than 1 row have been changed!";
                int row=e.getFirstRow();
                if(scrollPane!=null){
                    //some codes
                }
            }
        };

        model=table.getModel();
        scrollPane=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(scrollPane,BorderLayout.CENTER);
        btnInsert=new JButton("Insert a row");
        add(btnInsert,BorderLayout.SOUTH);
        repaint();
        revalidate();
//      scrollPane.repaint();
//      scrollPane.revalidate();
        System.out.println(model.getColumnCount()+" "+model.getRowCount());
        for(int i=0;i<data.size();i++){
            for(int j=0;j<data.get(0).size();j++){
                System.out.print(model.getValueAt(i, j)+" ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws Exception {
        new MySQLGUIManager();
    }

}

(省略了一些代码,但它们与GUI无关。) 提前谢谢!

3 个答案:

答案 0 :(得分:1)

我没有在代码中看到您将表实际添加到任何UI现有容器的任何位置。

此外,在调用pack()之前,创建表并将其添加到其预期的父组件可能是个好主意。

您可能想要考虑将表的创建与加载数据分开。

此外,省略的“某些代码”可能会对表的行为产生非常大的影响,因此请小心。

答案 1 :(得分:1)

好的......这是一个如何使用DefaultTableModel模型填充表格的示例,因为您的伪SSCCE无用:

package com.model.impl;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;


public class FillTable extends JFrame{

    JPanel panel = new JPanel();
    JLabel lblName = new JLabel("Name: ");
    JLabel lblIp = new JLabel("Ip: ");
    JTextField txtName = new JTextField(10);
    JTextField txtIp = new JTextField(10);
    JButton btnAdd = new JButton("Add");
    JTable table = new JTable();
    DefaultTableModel model = new DefaultTableModel(new Object[][]{}, new String[]{"Name","Ip"});

    public FillTable(){
        super("Test table");
        setLayout(new BorderLayout());
        add(panel,BorderLayout.NORTH);
        panel.add(lblName);
        panel.add(txtName);
        panel.add(lblIp);
        panel.add(txtIp);
        panel.add(btnAdd);
        table.setModel(model);
        add(new JScrollPane(table),BorderLayout.CENTER);

        btnAdd.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                String name = txtName.getText();
                String ip = txtIp.getText();

                model.addRow(new Object[]{name,ip});
            }});
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                FillTable ft = new FillTable();
                ft.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                ft.setLocationByPlatform(true);
                ft.pack();
                ft.setVisible(true);
            }});
    }
}

我希望它会有所帮助。祝你好运!

答案 2 :(得分:1)

  • 没有任何内容添加到数据/列模型
  • 数据和列模型未初始化
  • 仅在JFrame内创建JPanel只是为了将自己添加到框架中是不好的做法

您未从super.tableChanged方法

中拨打tableChanged
table = new JTable(data, colNames) {
    private static final long serialVersionUID = 1L;

    public void tableChanged(TableModelEvent e) {
        super.tableChanged(e);
        int column = e.getColumn();
        assert e.getFirstRow() == e.getLastRow() : "more than 1 row have been changed!";
        int row = e.getFirstRow();
        if (scrollPane != null) {
            //some codes
        }
    }
};