Hi,

I have a Jtable created in swing of which one column contains check boxes.

What i want is a few check boxes on specific rows.And now in the entire column
Any help! :)

Also i donot know the row number at which i want the check boxes.That needs to be done dynamically based on the value of the next column..

Thanks
Adity

Can you post the code you're using at the moment...? Otherwise I have no idea what to suggest.

Umm..the code is really huge.let me see what part of it will make sense :

for (int k =0;k < m_data.getColumnCount();k++ )  //Create cell renderer and editors for each column based on datatype
		  { 
			  System.out.println("COUNT == " + m_data.getColumnCount());
			   TableCellRenderer renderer = null;
			  TableCellEditor editor = null;
			  System.out.println("VALUE OF k = " + k);
		  switch(k)
		 {
		  case 0 ://check box
			  System.out.println("Entered here "); 
			renderer =new CheckCellRenderer();  // calls the renderer.
			JCheckBox chBox = new JCheckBox();
			chBox.setHorizontalAlignment(JCheckBox.CENTER);
			chBox.setBackground(m_table.getBackground());
			editor = new DefaultCellEditor(chBox);
			break;
			//TableCellRenderer render1 = new myCellRenderer();
		  case 2://Action Combo-box
			  renderer = new DefaultTableCellRenderer();
			
			  break;
		  case 1://Agent
			  renderer = new DefaultTableCellRenderer();
			
			  break;
 class CheckCellRenderer extends JCheckBox implements TableCellRenderer //Cell renderer that uses check boxes
	    {
	    	/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
			protected  Border m_nonFocusBorder = new EmptyBorder(1,1,1,1);
	    	protected  Border m_FocusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
	    	public CheckCellRenderer()
	    	{
	    		super();
	    		setOpaque(true);
	    		setBorderPainted(true);
	    		setHorizontalAlignment(JCheckBox.CENTER);	
	    	}
			public Component getTableCellRendererComponent(JTable table,
					Object value, boolean isSelected, boolean hasFocus, 
					int row,int column) 
			{
				Boolean b;
				if(value instanceof Boolean)
				{
					 b = (Boolean) value;
					 
					// setDisabledSelectedIcon(getDisabledSelectedIcon());
					setSelected(b.booleanValue());
						
				}
				setBackground(isSelected && !hasFocus ? table.getSelectionBackground() : table.getBackground());
				setForeground(isSelected && !hasFocus ? table.getSelectionForeground() : table.getForeground());
				setFont(table.getFont());
				//setBorder(hasFocus ? m_FocusBorder : m_nonFocusBorder);
				return this;
			}
	    }

This is the code for the check box renderer.This gives me checkboxes in the first column.Now i want to make it invisible for most of the rows and visible at just a few of them.

Thanks

I can't figure out where you're trying to do this already (there is no if statement checking the next column's value?)

I'd assume it could be done just by defining a new constructor for CheckCellRenderer that accepts as a parameter whether or not it should be visible - then set that with setVisible().

Alternatively, when k=0 do another check to see what's in the next column of the table. If the value in that column says that you should use the checkbox, then use CheckCellRenderer. If not, then use DefaultTableCellRenderer.

For checking the value of the next column, it looks like you might want to try using getValueAt(int, int).

I've never actually used a JTable, so I'm answering this based mostly off the documentation. Let me know if I completely misunderstand what you're trying to do, or if that doesn't work :)

Hi ,
Thanks for the help.I am trying to do what you suggested.Calling the check box cell renderer only once i see the value of the next column else i set it to default cell renderer.

But what i notice is that we set the renderer component for an entire column by statement :

TableColumn column = new TableColumn(k,m_data.m_columns[k].m_width,renderer,editor);

so this statement is after the switch case gets over. once the renderer for each column has been set I associate each column with the specific renderer.How do i set specific cell renderer's within one column?

The first link does not open :(

Says :
Unable to Initialize the Application

Please check the application logs for more information.

The link still works for me.

Someone named 'dreammachine' posted this:

Ok, so I've looked into their source and I think I know where and what the trouble is. The JTable.java has a method called:

3658> public TableCellRenderer getCellRenderer(int row, int column) {
3659> TableColumn tableColumn = getColumnModel().getColumn(column);
3660> TableCellRenderer renderer = tableColumn.getCellRenderer();
3661> if (renderer == null) {
3662> renderer = getDefaultRenderer(getColumnClass(column));
3663> }
3664> return renderer;
3665> }

starting at line 3658 of their source code. It retrieves the TableCellRenderer on line 3660 by calling the tableColumn's getCellRenderer method. This is found in the TableColumn.java class:

421> public TableCellRenderer getCellRenderer() {
422> return cellRenderer;
423> }

See the problem? Only ONE cell Renderer. It's referring to a variable found at line 140 which is of type TableCellRenderer ... well actually it's created as a DefaultTableCellRenderer at some point since TableCellRenderer is an interface.

Basically the fix is this:

In the TableColumn.java file, a collection (Vector, LinkedList, whatever) needs to keep track of each cell's renderer. This will solve the solution. Of course this will be something that you or I can make.

What's funny is the contradiction in documentation between JTable's and TableColumn's getCellRenderer() method. First, if we look at TableColumn's documentation it states:

"Returns the TableCellRenderer used by the JTable to draw values for this column."

Based on that first statement, the getCellRenderer() method in TableColumn is doing its job exactly. No lies, no contradictions in what it does.

However, that method is called up inside of the JTable's getCellRenderer() method which says a completely different thing which is:

"Returns an appropriate renderer for the cell specified by this row and column."

Now we have a problem. For the cell specified. It appears that the rush to push this out blinded some developer who either:

1) mis-interpreted what the JTable getCellRenderer() method was supposed to do and inadvertently added a feature or;
2) was in a 2 a.m. blitz, wired on Pepsi and adrenalin and wrote the bug in.

Either way, I'm really hoping that they'll fix this because it will take care of at least 2 bugs. Btw, anyone interested in posting a subclass to solve this problem (subclass of TableColumn) is more than welcome. I've spent much too much time on this and my project is already behind so I can't really afford any more time on this.

later,

a.

But I recommend you read the first post and the replies to this one as well. Seems strange that I can access the website but you cannot?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.