Java setModel builder中出现错误

Java setModel builder中出现错误,java,swing,interface,awt,tablemodel,Java,Swing,Interface,Awt,Tablemodel,我正在从TableModel接口实现我自己的模型。我的问题在于我的模型的构建者。我作为参数传递一个Object[][]像从数据库读取的数据,以及Object[]像从数据库读取的头 在tableModel接口中,我有一个ArrayList,我在其中保存从DB读取的数据 如何将二维对象[]复制到ArrayList中 在下面的几行中,我展示了我的代码,您可以在其中看到我在做什么 public class MiModelo implements TableModel{ private ArrayLis

我正在从TableModel接口实现我自己的模型。我的问题在于我的模型的构建者。我作为参数传递一个Object[][]像从数据库读取的数据,以及Object[]像从数据库读取的头

在tableModel接口中,我有一个ArrayList,我在其中保存从DB读取的数据

如何将二维对象[]复制到ArrayList中

在下面的几行中,我展示了我的代码,您可以在其中看到我在做什么

public class MiModelo implements TableModel{

private ArrayList<TableModelListener> listeners=new ArrayList<TableModelListener>();

private ArrayList<Object[]> datos=new ArrayList<Object[]>();

private ArrayList<String> titulos=new ArrayList<String>();

public MiModelo(Object[][] obj, Object[] head){
    Object[] registro;
    for (int i=0;i<obj.length;i++){
        registro=new Object[obj[i].length];
        for (int k=0;k<obj[i].length;k++){
            registro[k]=obj[i][k];
        }
        System.out.println(registro[0]+", "+registro[1]+", "+registro[2]+", "+registro[3]);
        datos.add(registro);
    }


    for (int i1=0;i1<head.length;i1++)
        titulos.add((String) head[i1]);
}
正如您所看到的,在代码中,我编写了一个System.out.println,以查看它是否正在从数据库中读取数据,并且很可能是正常的


提前感谢。

您对
TableModel
的实现并没有按照
TableModel
界面的要求为
JTable
提供将其自身添加为其模型侦听器的方法。而是扩展,它提供了必要的实现。这是一个基本的例子


如果这不是问题所在,请编辑您的问题,以包括显示您描述的问题的and堆栈跟踪。

您对
TableModel
的实现无法使
JTable
按照
TableModel
接口的要求将其自身添加为其模型的侦听器。而是扩展,它提供了必要的实现。这是一个基本的例子


如果这不是问题所在,请编辑您的问题,以包含一个和堆栈跟踪,该跟踪显示您描述的问题。

您可以编写异常堆栈跟踪吗?您可以编写异常堆栈跟踪吗?好的,我已经修复了从AbstractTableModel扩展的问题,并且它可以工作,但是我想使用TableModel接口来看看它是如何工作的。无论如何谢谢你,很高兴它起了作用;以
AbstractTableModel
的源代码为例进行研究。过了一段时间,您可以通过单击左侧的来接受这个答案。好的,我已经解决了从AbstractTableModel扩展的问题,并且它可以工作,但是我想使用TableModel接口来看看它是如何工作的。无论如何谢谢你,很高兴它起了作用;以
AbstractTableModel
的源代码为例进行研究。一段时间后,您可以通过单击左侧的来接受此答案。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)