• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Swing JCombobox and JTable issue

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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 ?
 
Sheriff
Posts: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
binayakumar patel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Rob Spoor
Sheriff
Posts: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, just return a JTextField from that method.

The following class for instance uses a JComboBox, JTextField and a JCheckBox:
 
binayakumar patel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, It is working fine.

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.


 
Rancher
Posts: 3296
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

I can not understand / follow your suggestion can you please define it more clearly.

 
Rob Spoor
Sheriff
Posts: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?):
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Posts: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Avirup Das wrote:java.lang.NullPointerException
at TestFrame$MyTableModel.getColumnClass(TestFrame.java:297)


So, what's on line 297 of TestFrame.java, inside method getColumnClass of inner class MyTableModel?
 
Avirup Das
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:So, what's on line 297 of TestFrame.java, inside method getColumnClass of inner class MyTableModel?



Avirup Das wrote:Let me post my code. It is just a small version of the actual code.



 
Rob Spoor
Sheriff
Posts: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getValueAt(0, c) returns null for some column.

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic