tia,
Ray
########################################################################################################
public class XTable extends JFrame {
JTable table;
public XTable() {
super("X Table");
setSize(800, 222);
table = new JTable( new EmitterTableModel(Data.getData()) );
//table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroll = new JScrollPane( table );
getContentPane().add(scroll, BorderLayout.CENTER);
}
public static void main(String[] args) {
XTable frame = new XTable();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
########################################################################################################
public class EmitterTableModel extends AbstractTableModel {
private final int PRIORITY = 1;
private final int AZIMUTH = 2;
private Emitter[] m_data; // Reference to the data.
private final Object[] column = new Object[] {
"Priority",
"Azimuth",
};
public EmitterTableModel(Emitter[] data) {
m_data = data; // Store a reference to the data.
}
public int getRowCount() { return m_data.length; }
public int getColumnCount() { return column.length; }
public String getColumnName(int col) { return (String) column[col]; }
public Object getValueAt(int row, int col) {
// Get the emitter specified by the row ...
Emitter emitter = m_data[row];
// Get the field specified by the column...
Object rv = null;
//try {
switch ( col ) {
case PRIORITY : rv = new Integer( emitter.getPriority() );
break;
case AZIMUTH : rv = new Double( emitter.getAzimuth() );
break;
default : rv = null; break;
}
//} catch (NullPointerException e) {
// System.out.println(e.printStackTrace());
//}
return rv;
}
// Define the classes returned for each column
public Class getColumnClass( int col ) {
Class rv = null;
switch ( col ) {
case PRIORITY : rv = Integer.class; break;
case AZIMUTH : rv = Double.class; break;
default: rv = null; break;
}
return rv;
}
public void setValueAt(Object obj, int row, int col) {
Emitter emitter = m_data[row];
switch ( col ) {
case PRIORITY : emitter.setPriority( ((Integer)
obj).intValue() ); break;
case AZIMUTH : emitter.setAzimuth( ((Double)
obj).doubleValue() ); break;
default : break;
}
}
}
########################################################################################################
public class Emitter {
private int priority;
private double azimuth;
public Emitter(int p) {
priority = p;
azimuth = 0.0;
}
public Emitter(int p, double az) {
priority = p;
azimuth = az;
}
public int getPriority() { return priority; }
public double getAzimuth() { return azimuth; }
public void setPriority(int value) { priority = value; }
public void setAzimuth(double value) { azimuth = value; }
}
########################################################################################################
Exception occurred during event dispatching:
java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(JTable.java:3537)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:995)
at
javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:917)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:858)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:39)
at javax.swing.JComponent.paintComponent(JComponent.java:398)
at javax.swing.JComponent.paint(JComponent.java:739)
at javax.swing.JComponent.paintChildren(JComponent.java:523)
at javax.swing.JComponent.paint(JComponent.java:748)
at javax.swing.JViewport.paint(JViewport.java:668)
at javax.swing.JComponent.paintWithBuffer(JComponent.java:4393)
at javax.swing.JComponent._paintImmediately(JComponent.java:4336)
at javax.swing.JComponent.paintImmediately(JComponent.java:4187)
at
javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:370)
at
javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:205)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:154)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:317)
at
java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:103)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:84)
Ray
They *do always begin* at 0 (like arrays, Lists, ListModels etc.) and that
is also your problem.
> public class EmitterTableModel extends AbstractTableModel {
> private final int PRIORITY = 1;
> private final int AZIMUTH = 2;
>
> private Emitter[] m_data; // Reference to the data.
>
> private final Object[] column = new Object[] {
> "Priority",
> "Azimuth",
> };
>
> public EmitterTableModel(Emitter[] data) {
> m_data = data; // Store a reference to the data.
> }
>
> public int getRowCount() { return m_data.length; }
> public int getColumnCount() { return column.length; }
Returns 2: there are the columns '0' and '1'.
> public String getColumnName(int col) { return (String) column[col]; }
Returns "Priority" for col == 0, "Azimuth" for col == 1.
> public Object getValueAt(int row, int col) {
col will be either 0 or 1.
> // Get the emitter specified by the row ...
> Emitter emitter = m_data[row];
>
> // Get the field specified by the column...
> Object rv = null;
> //try {
> switch ( col ) {
> case PRIORITY : rv = new Integer( emitter.getPriority() );
> break;
> case AZIMUTH : rv = new Double( emitter.getAzimuth() );
Never reached, since col is either 0 or 1.
> break;
> default : rv = null; break;
If col is 0, 'null' will be returned. This is still allowed, but not
what you want.
> public Class getColumnClass( int col ) {
> Class rv = null;
> switch ( col ) {
> case PRIORITY : rv = Integer.class; break;
> case AZIMUTH : rv = Double.class; break;
> default: rv = null; break;
But here, if col is 0, you return 'null' for getColumnClass, and that
is invalid.
-> crash.
So fix your constants to be 0-based instead of 1-based.
Christian
--
Your mornings will be brighter
Break the line
Tear up rules
Make the most of a million times no Bauhaus, Hope