(Java)Problem z dodawaniem elementów do DefaultTableModel


(Wojmak) #1

Witam,

Mam problem z dodaniem elementów za pomocą addRow(Object[] objs) do DefaultTableModel. Pokaże kod źródłowy od razu a potem wyjątek jaki mi się rzuca

Mój nieco zmodyfikowany DefaultTableModel

class NotEditableTableModel extends DefaultTableModel

	{		

		public boolean isCellEditable(int row, int column)

		{

		      return false;

		}

	}

Konstruktor klasy gdzie JTable ma inicjowane nagłówki.

JTable jest inicjowane tak JTable table = new JTable(NotEditableTableModel)

for(int i=0;i
		{

			TableColumn col = new TableColumn();

			col.setHeaderValue(columnNames[i]);

			table.addColumn(col);

		}

Metoda gdzie dodawane są nowe wartości - tu jest błąd.

messages = new MailMessage[args.length];

((DefaultTableModel)table.getModel()).setRowCount(args.length);

for (int i =0 ; i
{

      MailMessage message = new MailMessage();  

      message.constructFromText(args[i]);

	messages[i] = message;

	String[] row = new String[5];

	row[0] = message.getFrom();

	row[1] = message.getSubject();

	row[2] = message.getDate();

	row[3] = "";

	row[4] = Integer.toString(message.getId());

	((DefaultTableModel)table.getModel()).addRow(row);

}

No i wyjątek

Exception occurred during event dispatching:

java.lang.ArrayIndexOutOfBoundsException: 0 >= 0

	at java.util.Vector.elementAt(Unknown Source)

	at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)

	at javax.swing.JTable.getValueAt(Unknown Source)

	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.paintWithOffscreenBuffer(Unknown Source)

	at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)

	at javax.swing.JComponent._paintImmediately(Unknown Source)

	at javax.swing.JComponent.paintImmediately(Unknown Source)

	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)

	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)

	at java.awt.event.InvocationEvent.dispatch(Unknown Source)

	at java.awt.EventQueue.dispatchEvent(Unknown Source)

	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

	at java.awt.Dialog$1.run(Unknown Source)

	at java.awt.Dialog$2.run(Unknown Source)

	at java.security.AccessController.doPrivileged(Native Method)

	at java.awt.Dialog.show(Unknown Source)

	at javax.swing.JOptionPane.showOptionDialog(Unknown Source)

	at javax.swing.JOptionPane.showMessageDialog(Unknown Source)

	at javax.swing.JOptionPane.showMessageDialog(Unknown Source)

	at MessagesListPanel.actionPerformed(MessagesListPanel.java:176)

	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

	at java.awt.Component.processMouseEvent(Unknown Source)

	at javax.swing.JComponent.processMouseEvent(Unknown Source)

	at java.awt.Component.processEvent(Unknown Source)

	at java.awt.Container.processEvent(Unknown Source)

	at java.awt.Component.dispatchEventImpl(Unknown Source)

	at java.awt.Container.dispatchEventImpl(Unknown Source)

	at java.awt.Component.dispatchEvent(Unknown Source)

	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

	at java.awt.Container.dispatchEventImpl(Unknown Source)

	at java.awt.Component.dispatchEvent(Unknown Source)

	at java.awt.EventQueue.dispatchEvent(Unknown Source)

	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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-1" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0

	at java.util.Vector.elementAt(Unknown Source)

	at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)

	at javax.swing.JTable.getValueAt(Unknown Source)

	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.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.paintWithOffscreenBuffer(Unknown Source)

	at javax.swing.JComponent.paintDoubleBuffered(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 sun.awt.RepaintArea.paintComponent(Unknown Source)

	at sun.awt.RepaintArea.paint(Unknown Source)

	at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)

	at java.awt.Component.dispatchEventImpl(Unknown Source)

	at java.awt.Container.dispatchEventImpl(Unknown Source)

	at java.awt.Component.dispatchEvent(Unknown Source)

	at java.awt.EventQueue.dispatchEvent(Unknown Source)

	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)

Tak to wygląda. Próbowałem też robić za pomocą metody insertRow, bo setValueAt zdaje się modyfikuje jedynie istniejące już elementy.


(Wojmak) #2

Proszę już nie odpowiadać na post, rozwiązałem problem poprzez użycie zamiast:

JTable table = new JTable(new NotEditableTableModel());

Zrobiłem tak:

NotEditableTableModel netm= new NotEditableTableModel();

JTable table = new JTable(netm);

Według mnie nie ma różnicy, ale według Javy różnica jest, i teraz działa dobrze. :smiley: