I am unable to add a Combo box in JTable 2nd column of each row with different value. Please help me.. I already try lots of time with many example but I man unable to change the ComboBox value in every row. Is it possible to rander a particular column with different component in every row ?
TableCellRenderer.getTableCellEditorComponent takes both the row and column numbers, so you can use that to your advantage. For example:
Of course you can use a Collection or List instead of an Object[], but the principle is the same.
Thanks Rob Prime. It is working fine.
yours solution helps me a lot.
I have one more doubt that , can not we Render the same cell with different component. Like the same column in first row with JCombo Box and the same column in next row with JTextField.
I have a Map, which I have to display in n-rows and 3-column table ... where for rew columns the value need to select from combobox and few need to enter. The number of rows we cant define... because it is dynamic.
I face one more problem that, when i click on Cell for edit,then the scope is remains their till i will not give enter , in the case of JTextField.
Similarly in JComboBox the Scope is remain there till if I will not select any value from JComboBox.
Is there any way that it will lose its scope when i will click on any other cell.
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1644
posted
0
Instead of putting all the code in a custom editor, override the getCellEditor(...) method of JTable to return the appropriate editor for the row and column.
binayakumar patel
Greenhorn
Joined: Jun 26, 2009
Posts: 27
posted
0
Hi Rob,
I can not understand / follow your suggestion can you please define it more clearly.
Rob Camick wrote:Instead of putting all the code in a custom editor, override the getCellEditor(...) method of JTable to return the appropriate editor for the row and column.
Uhm yeah, that would be better
Rob means this (which I used myself for a similar situation as well... why did I forget?):
Avirup Das
Greenhorn
Joined: Jul 16, 2010
Posts: 3
posted
0
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.
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]);
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
)
{
Also, don't post such large pieces of code when only a small piece is needed. And if you post code, UseCodeTags.
Avirup Das
Greenhorn
Joined: Jul 16, 2010
Posts: 3
posted
0
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.