0

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

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 无关。)提前致谢!

4

3 回答 3

1

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

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

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

另外,省略的“一些代码”可能对表格的行为有很大的影响,所以要小心。

于 2012-10-16T23:39:56.127 回答
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);
            }});
    }
}

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

于 2012-10-16T23:48:09.030 回答
1
  • 没有向数据/列模型添加任何内容
  • 数据和列模型未初始化
  • JFrame从 a 中创建 aJPanel只是为了将自己添加到框架中是不好的做法

你不是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
        }
    }
};
于 2012-10-16T23:53:46.807 回答