displaying icon in JButton table cell
hi,
I have a JTable with JButtons in first column, the second with
check boxes. The table should work in such a way that when a row is selected,
the icon is displayed (same as you have in MS Access when you design table data).
Now, when I click on the button, it displays the desired behavior, but when I
select the next column, the icon is not displayed. Why is this behavior seen?
Can anyone give me suggestions to get the desired behavior?
Thanks in advance.
The sample code is as given below:
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import com.uisupport.GIFConstants;
/**
* This class creates a condition table
*
* @author
*
*/
publicclass SelCriteriaConditionTableextends JScrollPane{
private JTable tblSelection =null;
private DefaultTableModel tblSelectionModel =null;
private String[] values ={"And","Or"};
private String[] colNames =new String[]{"","And/Or","Conditions"};// null;
private Object lblData[][] ={{new JButton(),"And",""},
{new JButton(),"Or",""}};// null;
/**
* Constructor
*/
public SelCriteriaConditionTable(DefaultTableModel tblSelectionModel){
this.tblSelectionModel = tblSelectionModel;
init();
}
void init(){
tblSelectionModel =new DefaultTableModel();
tblSelectionModel.setDataVector(lblData, colNames);
tblSelection =new JTable(tblSelectionModel);
tblSelection.getColumnModel().getColumn(0).setCellRenderer(
new CWTableButtonRenderer());
tblSelection.getColumnModel().getColumn(0).setCellEditor(
new ButtonEditor(new JCheckBox()));
tblSelection.getColumnModel().getColumn(1).setCellRenderer(
new CWComboBoxRenderer(values));
tblSelection.getColumnModel().getColumn(1).setCellEditor(
new CWComboBoxEditor(values));
tblSelection.setShowGrid(false);
this.getViewport().add(tblSelection);
}
class ButtonEditorextends DefaultCellEditor{
protected JButton button;
ImageIcon leftButtonIcon =new ImageIcon(GIFConstants.getArrowIcon()
.getImage());
public ButtonEditor(JCheckBox checkBox){
super(checkBox);
button =new JButton("", leftButtonIcon);
button.setOpaque(true);
}
public Component getTableCellEditorComponent(JTable table,
Object value,boolean isSelected,int row,int column){
button.setIcon(leftButtonIcon);
return button;
}
}
/**
* TableCellRenderer for the JComboBox
*
* @author
*
*/
publicclass CWComboBoxRendererextends JComboBoximplements
TableCellRenderer{
/**
* Constructor
*
* @param items -
*The combo box items
*/
public CWComboBoxRenderer(String[] items){
super(items);
}
/**
* Returns the component used for drawing the cell
*/
public Component getTableCellRendererComponent(JTable table,
Object value,boolean isSelected,boolean hasFocus,int row,
int column){
if (isSelected){
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
}else{
setForeground(table.getForeground());
setBackground(table.getBackground());
}
// Select the current value
setSelectedItem(value);
returnthis;
}
}
/**
* Combo box editor for the table cell
*
* @author
*/
class CWComboBoxEditorextends DefaultCellEditor{
/**
* Constructor
*
* @param items -
*The items in the combo box
*/
public CWComboBoxEditor(String[] items){
super(new JComboBox(items));
}
}
/**
* Button renderer for the table cell
*
* @author
*
*/
publicclass CWTableButtonRendererextends JButtonimplements
TableCellRenderer{
/**
* Constructor
*
*/
public CWTableButtonRenderer(){
}
/**
* Returns the component used for drawing the cell
*/
public Component getTableCellRendererComponent(JTable table,
Object value,boolean isSelected,boolean hasFocus,int row,
int column){
returnthis;
}
}
staticvoid createAndShowGUI(){
// Make sure we have nice window decorations.
// JFrame.setDefaultLookAndFeelDecorated(true);
// Create and set up the window.
JFrame frame =new JFrame("display ");
System.out.println(frame.getSize());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultTableModel tblSelModel =new DefaultTableModel();
SelCriteriaConditionTable dt =new SelCriteriaConditionTable(
tblSelModel);
frame.getContentPane().add(dt);
// Display the window.
frame.pack();
frame.setSize(new Dimension(400, 200));
frame.setVisible(true);
System.out.println(frame.getSize());
}
/**
* @param args
*/
publicstaticvoid main(String[] args){
// TODO Auto-generated method stub
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
createAndShowGUI();
}
});
}
}

