数据未显示在JTable中

时间:2012-11-22 08:12:14

标签: java mysql swing jtable tablemodel

我正在使用DAO Factory从数据库中获取数据。当我运行我的程序时,数据显示在printline中,因此他将数据从数据库中删除。但是我希望它能在我的JTable中显示,但是这个是空的,我不知道如何填充它。

代码JFrame:

package View;

import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.table.TableModel;

import Controller.VerwijderController;
import Model.OefeningenListModel;
import Model.OefeningenTableModel;

public class VerwijderenHome extends JFrame {

    private JList LijstOefening;
    private JScrollPane jScrollPane1;
    private Container window = getContentPane();
    private JButton delete;
    private VerwijderController Controller;
    private JTable tabel;

    public VerwijderenHome()
    {
        initGUI();
    }

    public void addDeleteListener(ActionListener a){
        delete.addActionListener(a);
    }

    private void initGUI() {
        setPreferredSize(new Dimension(800, 600));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(null);
        setVisible(true);

        JTable table = new JTable(new OefeningenTableModel());              
        JScrollPane scrollPane = new JScrollPane(table);  
        scrollPane.setBounds(50, 50, 300, 60);
        window.add(scrollPane);

        delete = new JButton("Delete");
        delete.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        delete.setBounds(50, 265, 100, 30);
        window.add(delete);

        pack();


        Controller = new VerwijderController();
        addDeleteListener(Controller);

    }

    public JButton getDelete(){
        return delete;
    }

    public JList getLijst()
    {
        return LijstOefening;


}
    }

Code DefaultTableModel ?? (不知道我是否必须使用这个)

package Model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;

import datapackage.DAOFactory;

public class OefeningenTableModel extends DefaultTableModel {

      ArrayList<Oefening> oefeningen;

      public OefeningenTableModel(){

          oefeningen = DAOFactory.getFactory(0).getIDAOOefening().load();

      }




    }

1 个答案:

答案 0 :(得分:3)

DefaultTableModelVector支持,代表表模型的列和行。

您已经提供了自己的ArrayList个对象,但是您没有覆盖将这些数据提供回表所需的方法。 DefaultTableModel不知道您的ArrayList

尝试覆盖以下部分内容;

public class OefeningenTableModel extends AbstractTableModel {

    ArrayList<Oefening> oefeningen;

    public OefeningenTableModel(){
        oefeningen = DAOFactory.getFactory(0).getIDAOOefening().load();
    }    

    @Override
    public int getRowCount() {
        return oefeningen.getSize();
    }

    @Override
    public int getColumnCount() {
        // You'll need to fill this out to meet your requirements
    }

    @Override
    public String getColumnName(ing column) {
        // You'll need to fill this out to meet your requirements
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        // You'll need to fill this out to meet your requirements
    }

}

花点时间阅读How to use Tables