Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

Swing [Archive]


How to hide a JCheckBox in a JTable - Help


Hey, I have a JTable that has one of its columns as JCheckboxes.  But some of the rows of my table do not require a JCheckBox so I would like the cells of the JCheckBox column to be blank on those rows.  Can anyone give me any tips about doing that?  I have a a cell renderer similar to the one down below.  I don't understand why I couldn't just say something along the lines of:  if(nRow == 5){      setVisible(false);; }   I would think that it would make the JCheckBox cell in row five be invisible, but it doesn't.  So another approach I attempted was to override the paintComponent() in the renderer like this:  public void paintComponent(Graphics g){      if(row != 5){          super.paintComponent(g);      } }   That approach would not draw anything in the JCheckBox cell for row 5 like I want, but when I would select that row in the table, the cell wouldn't have the same background as the other cells (it would stay white).   So basically, what is the correct way of making a cell in my JCheckBox column invisible?  Thanks for the help.    Kevin  //Example of the renderer I'm using   public class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {   public CheckBoxRenderer() {   }   public Component getTableCellRendererComponent(JTable table,Object value, 					       boolean isSelected, boolean hasFocus, 					       int row, int column) {   	if (value.toString().equals("Y")){  		this.setSelected(true);  	} 	else{  		this.setSelected(false);  	}   	this.setHorizontalAlignment(JCheckBox.CENTER);   	if (isSelected) { 		setForeground(table.getSelectionForeground()); 		setBackground(table.getSelectionBackground()); 	}  	else{ 		setForeground(table.getForeground()); 		setBackground(table.getBackground()); 	}   	return this; } } 

Seems like you could just put a JLabel in your class and if you didn't want to show the checkbox, just return that label.

import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.tree.*; import javax.swing.table.*;  public class Test implements TableCellRenderer { 	private JCheckBox checkBox=new JCheckBox(); 	private JLabel label=new JLabel(); 	private DefaultTableCellRenderer blankRenderer=new DefaultTableCellRenderer();  	public Test() { 		checkBox.setEnabled(false); 	}  	public Component getTableCellRendererComponent(JTable table, 	                  Object value, 	                  boolean isSelected, 	                  boolean hasFocus, 	                  int row, 	                  int column) { 		if (row==5) { 			updateAppearance(value, label, isSelected, hasFocus); 			return label; 		} 		else { 			checkBox.setSelected(Boolean.TRUE.equals(value)); 			updateAppearance(value, checkBox, isSelected, hasFocus); 			return checkBox; 		} 	}  	private void updateAppearance(Object value, Component render, boolean isSelected, boolean hasFocus) { 		// this is the step you have to do to adjust the background, foreground, 		// see the source for DefaultTableCellRenderer for example 	}  	public static final void main(String[] args) { 		Object[][] data=new Object[10][10]; 		Object[] colNames=new Object[10]; 		for (int i=0; i<10; i++) { 			colNames=(char)('A'+i)+""; 			for (int j=0; j<10; j++) { 				data[j]=Boolean.TRUE; 			} 		}  		data[0][5]=""; 		JTable table=new JTable(data, colNames); 		table.getColumnModel().getColumn(0).setCellRenderer(new Test()); 		JFrame f = new JFrame(); 		f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); 		f.getContentPane().add(new JScrollPane(table)); 		f.pack(); //		f.setLocationRelativeTo(null); 		f.setVisible(true); 	} }

thanks.  its amazing how simple it is when you see it.  Kevin


Related Links

icons in JTable cells
Resize Applet and it crashes!
Setting Icon Image in a JWindow
JTable cell editing help
how to displays charts...does it exist ?
Please help.Urgent Jtable
JComboBox not dropping
ButtonModel and JCheckBox
event handling JRadioButton
splash screen image loads too slowly
JSplitpane Question
JTable Urgent !
use of emotions
How can I remove or replace the " Java coffee cup " icon ???
toolbar
Limit in JTable