Win a copy of Head First Android this week in the Android forum!

Avirup Das

Greenhorn
+ Follow
since Jul 16, 2010
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Avirup Das

Hi Rob,

I do agree with you that the method getValueAt(0, c) is giving NullPointerException for some column.
Actually the problem was with the String array declaration and the respective values being assigned.

public String[] targetName = {"None","T0","T1","T2","T3","T4","T5"};

.

I have fixed that problem and eventually integrated with my main project and the good news is its working
fine now.

However extending DefaultCellEditor didn't solved my purpose.I don't know why but it didn't. I think I had
missed out something. So I have created a subclass of JTable and override the getCellEditor() method
and it seems that the problem is resolved.

Thanks for your response
Avirup
11 years ago
Hi Rob,

Let me post my code. It is just a small version of the actual code.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;


public class TestFrame extends JFrame implements ActionListener
{
public String[] sourceName = {"D0","D1","D2"};
public String[] sourceType = {"SMI","EFI","SMI"};
public String[] targetName = {"None","T0","T1","T2","T3","T4","T5"};
public String[] targetType = {"-","SMI","EFI","SMI","EFI","SMI","EFI"};
public HashMap sourceMap = new HashMap();
public HashMap targetMap = new HashMap();
public HashMap editors = new HashMap();

public TestFrame()
{
sourceMap.put(sourceName[0],sourceType[0]);
sourceMap.put(sourceName[1],sourceType[1]);
sourceMap.put(sourceName[2],sourceType[2]);

targetMap.put("None","-");
targetMap.put(targetName[0],targetType[0]);
targetMap.put(targetName[1],targetType[1]);
targetMap.put(targetName[2],targetType[2]);
targetMap.put(targetName[3],targetType[3]);
targetMap.put(targetName[4],targetType[4]);
targetMap.put(targetName[5],targetType[5]);

initComponents();
populateTable();
}
private void initComponents()
{
scrollPane = new JScrollPane();
table = new JTable(new MyTableModel());
setDefaultCellRendering();
// Rest of the GUI related code
}
private void populateTable()
{
for(int i=0;i<sourceName.length;i++)
{
table.setValueAt("-",i,1);
table.setValueAt("-",i,2);
table.setValueAt("-",i,3);
table.setValueAt("-",i,4);
table.setValueAt(new Boolean(false),i,0);
}
for(int i=0;i<sourceName.length;i++)
{
table.setValueAt(sourceName[i],i,1);
table.setValueAt(sourceType[i],i,2);
for(int j=0;j<targetName.length;j++)
{
if(!isTargetMapped(j)&&isLabel(sourceName[i],targetName[j]))
{
table.setValueAt(new Boolean(true),i,0);
table.setValueAt(targetName[j],i,3);
table.setValueAt(targetType[j],i,4);
break;
}
}
}
}
public boolean isTargetMapped(int count)
{
boolean flag = false;
for(int i=0;i<sourceName.length;i++)
{
String target = (String)this.table.getValueAt(i,3);
if(target.equals(targetName[count]))
flag = true;
}
return flag;
}
public boolean isLabel(String source,String target)
{
String srcType = (String)sourceMap.get(source);
String tgtType = (String)targetMap.get(target);
return srcType.equalsIgnoreCase(tgtType);
}
private void setDefaultCellRendering()
{
DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
cr.setHorizontalAlignment(JLabel.CENTER);

TableColumn column = null;

column = null;
column = table.getColumnModel().getColumn(0);
column.setPreferredWidth(30);

column = null;
column = table.getColumnModel().getColumn(1);
column.setCellRenderer(cr);

column = null;
column = table.getColumnModel().getColumn(2);
column.setCellRenderer(cr);

column = null;
column = table.getColumnModel().getColumn(3);
column.setCellEditor(new MyTableCellEditor());
// column.setCellRenderer(cr);

column = null;
column = table.getColumnModel().getColumn(4);
column.setCellRenderer(cr);
}
public Vector getComboItems(int row,int col)
{
Vector vect = new Vector();
vect.add("None");
for(int j=0;j<targetName.length;j++)
if(isLabel(sourceName[col],targetName[j]))
vect.add(targetName[j]);
return vect;
}
class MyTableModel extends AbstractTableModel
{
String[] columnNames = {"","Source Disk","Label","Target Disk","Label"};
Object[][] data = new Object[sourceName.length][5];
public int getColumnCount()
{
return this.columnNames.length;
}
public int getRowCount()
{
return this.data.length;
}
// Get Table Column Name
public String getColumnName(int col)
{
return columnNames[col];
}
// Get Table row-column(cell) value
public Object getValueAt(int row,int col)
{
return data[row][col];
}
// JTable uses this method to determine the default
// renderer/editor for each cell.If we didn't implement
// this method then the First Column will contain the
// text "true/false" inspite of a CheckBox.
public Class getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row,int col)
{
boolean flag = false;
if(col==3)
{
boolean bool = ((Boolean)getValueAt(row,0)).booleanValue();
if(bool)
flag = true;
}
return flag;
}
// Don't implement this method unless the Table Data
// can change.
public void setValueAt(Object value,int row,int col)
{
if(col==0)
repaint();
data[row][col] = value;
fireTableCellUpdated(row,col);
}
}
class MyTableCellRenderer extends JLabel implements TableCellRenderer
{
public Component getTableCellRendererComponent( JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int rowIndex,
int colIndex
)
{

setText((String)value);
return this;
}
}
class MyTableCellEditor extends DefaultCellEditor
{
public MyTableCellEditor()
{
super(new JComboBox());
}
public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int col)
{
JComboBox combo = (JComboBox)super.getTableCellEditorComponent(table,value,isSelected,row,col);
combo.removeAllItems();
Vector vect = getComboItems(row,col);
for(int i=0;i<vect.size();i++)
combo.addItem((String)vect.get(i));

return combo;
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() instanceof JComboBox)
comboClicked(table.getSelectedRow());
}
public void comboClicked(int row)
{
try
{
if(row!=-1)
{
String target= (String)table.getValueAt(row,2);
String type = (String)this.targetMap.get(target);
table.setValueAt(type,row,3);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String args[])
{
new TestFrame();
}
private JScrollPane scrollPane;
private JTable table;
private JComboBox comboBox;
}


Thanks
Avirup
11 years ago
Hi,

I do have a similar requirement, My JTable's first column comprises of check boxes and third column comprises
of JComboBox (a unique one for each row). How ever even after implementing the "MyTableCellEditor" class as
Rob suggested, I am getting the following exception.

java.lang.NullPointerException
at TestFrame$MyTableModel.getColumnClass(TestFrame.java:297)
at javax.swing.JTable.getColumnClass(JTable.java:1752)
at javax.swing.JTable.getCellRenderer(JTable.java:3700)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:1148)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1051)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:974)

Can you please suggest whats going wrong. I can post my code if needed.

Thanks
Avirup
11 years ago