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