jTable with Cell Editor and Cell Render For JCheckBox

48 views
Skip to first unread message

shiv...@gmail.com

unread,
Sep 23, 2008, 7:48:03 AM9/23/08
to javaworld

import java.awt.Component;
import java.awt.Cursor;

/*
* jTableWithEditorAndRender.java
*
* Created on September 23, 2008, 4:35 PM
*/
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.EventObject;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;



/**
*
* @author admin
*/
public class jTableWithEditorAndRender extends javax.swing.JFrame {

/** Creates new form jTableWithEditorAndRender */
public jTableWithEditorAndRender() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel2 = new javax.swing.JLabel();
jCheckBox1 = new javax.swing.JCheckBox();
jTextField1 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jButton1 = new javax.swing.JButton();


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
getContentPane().setLayout(null);

jLabel2.setText("Name:");
getContentPane().add(jLabel2);
jLabel2.setBounds(220, 100, 60, 20);

jCheckBox1.setText("Status");
getContentPane().add(jCheckBox1);
jCheckBox1.setBounds(460, 100, 90, 23);

jTextField1.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jTextField1ActionPerformed(evt);
}
});
getContentPane().add(jTextField1);
jTextField1.setBounds(260, 100, 180, 20);


jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(0, 0, 0)));
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(220, 130, 420, 290);

jButton1.setText("Add");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
jButton1MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jButton1MouseExited(evt);
}
});
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(560, 100, 80, 20);

pack();
}// </editor-fold>

private void jTextField1ActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
}

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
jButton1.setCursor(new Cursor(Cursor.WAIT_CURSOR));
//do my program here
try{
Thread.sleep(1000);
} catch(Exception z){};
}

private void jButton1MouseEntered(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
jButton1.setCursor(new
java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
}

private void jButton1MouseExited(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
jButton1.setCursor(new
java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}


javax.swing.table.DefaultTableModel model;
private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
model.addRow(new Object[]{jTextField1.getText(),new
Boolean(jCheckBox1.isSelected())});
jButton1.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
javax.swing.JTable table;
private void formWindowOpened(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
model=new javax.swing.table.DefaultTableModel(new Object[]
{"Name","Status"},0);
table=new javax.swing.JTable(model);
TableColumn column1 = ((TableColumnModel)
table.getColumnModel()).getColumn(1);
column1.setPreferredWidth(25);

CheckBoxCellEditor editor1 = new CheckBoxCellEditor();
column1.setCellEditor(editor1);

CheckBoxCellRendrer renderer1 = new CheckBoxCellRendrer();
column1.setCellRenderer(renderer1);
jScrollPane1.setViewportView(table);
table.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tableMouseClicked(evt);
}
});
}


private void tableMouseClicked(java.awt.event.MouseEvent evt) {
int m=table.getSelectedRow();
System.out.println("Value0........."+model.getValueAt(m,
0).toString());
System.out.println("class
name0............."+model.getValueAt(m,0).getClass());

System.out.println("Value1........."+model.getValueAt(m,
1).toString());
System.out.println("class
name1............."+model.getValueAt(m,1).getClass());

}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new jTableWithEditorAndRender().setVisible(true);
}
});
}



public class CheckBoxCellEditor extends JCheckBox implements
TableCellEditor {

protected ChangeEvent changeEvent = new ChangeEvent(this);

protected EventListenerList listenerList = new EventListenerList();

boolean selected = false;

boolean cellEnabled = true;

/**
* Constructs a new <code>CheckBoxCellEditor</code> object.
*/
public CheckBoxCellEditor() {
super();
this.setSize(10, 10);
addItemListener(new ItemListener() {

/**
* Invoked when item event occurs.
*
* @param event <code> ItemEvent</code>
*/
public void itemStateChanged(ItemEvent event) {
fireEditingStopped();
}
});
}

/**
* Registers the <code>CellEditorListener</code> to listeners list
*
* @param l a <code>CellEditorListener</code>
*/
public void addCellEditorListener(CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}

/**
* To cancel the Cell Editing invoked all the actions with this cell
will be cancelled
*/
public void cancelCellEditing() {
fireEditingCanceled();
}

/**
* Returns the cell editor value as <code>Object</code> this will be
called when stops the
* cell editing
*
* @return Object
*/
public Object getCellEditorValue() {
return new Boolean(this.isSelected());
}

/**
* Returns the Editor Component to be painted on the table for the
given row and column with
* given value.
* <p>
* in the method we need to set the value and state of component then
return component itself so
* that it can be painted as it is on the table.
*
* @param table <code>JTable</code> is the component where this editor
need to be painted.
* @param value <code>Object</code> is the value for the
EditorComponent.
* @param isSelected boolean
* @param row int
* @param column int
* @return <code>Component</code> is the editor component to be painted
on the table.
*/
public Component getTableCellEditorComponent(JTable table, Object
value, boolean isSelected,
int row, int column) {
selected = isSelected;
this.requestFocus();
if (table.isCellEditable(row, column)) {
this.setEnabled(true);
}
else {
this.setEnabled(false);
}
if (value.equals(new Boolean(true))) {
this.setSelected(true);
}
else {
this.setSelected(false);
}
return this;
}

/**
* sets the state of the cell.
*
* @param enabled boolean
*/
public void setCellEnabled(boolean enabled) {
cellEnabled = enabled;
}

/**
* Determines whether this editor component is Editable or not if the
return value is true then
* it will behave as editable component other wise it can not allow to
edit
*
* @param anEvent <code>EventObject</code>
* @return boolean
*/
public boolean isCellEditable(EventObject anEvent) {
return cellEnabled;
}

/**
* Remove the <code>CellEditorListener</code> from the listeners list.
*
* @param l <code>CellEditorListener</code>
*/
public void removeCellEditorListener(CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}

/**
* Returns whether cell should be selectble or not when some event
occurs.
*
* @param anEvent <code>EventObject</code>
* @return boolean
*/
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}

/**
* Stops the cell Editing invoked when the focus lost from this
editorcomponent and puts the
* values into model
*
* @return boolean
*/
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}

/**
* Invoked when stops the cell editing by removing thefocus from the
editor component.
*/
public void fireEditingStopped() {
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == CellEditorListener.class) {
listener = (CellEditorListener) listeners[i + 1];
listener.editingStopped(changeEvent);
}
}
}

/**
* Invoked when stops the cell editing when call cancell cell edit
method on perticular cell it
* will removes all editing values
*/
public void fireEditingCanceled() {
CellEditorListener listener;
Object[] listeners = listenerList.getListenerList();
for (int i = 0; i < listeners.length; i++) {
if (listeners[i] == CellEditorListener.class) {
listener = (CellEditorListener) listeners[i + 1];
listener.editingCanceled(changeEvent);
}
}
}
}







public class CheckBoxCellRendrer extends JCheckBox implements
TableCellRenderer {

public boolean cellEnabled = true;

/**
* Constructs a new <code>CheckBoxCellRendrer</code> object.
*/
public CheckBoxCellRendrer() {
super();
this.setEnabled(true);
}

/**
* Prepares and Returns the Button Renderer component for the table
cell to be renderered this
* will set the value and state of the component then paints on table
cell
*
* @param table <code>JTable</code> component where the renderer to be
painted
* @param value <code>Object</code> value for the renderer
* @param isSelected boolean
* @param hasFocus boolean
* @param row int
* @param column int
* @return <code>Component</code>
*/
public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected,
boolean hasFocus, int row, int column) {
if (table.isCellEditable(row, column)) {
this.setEnabled(true);
}
else {
this.setEnabled(false);
}
if (value != null && value.equals(new Boolean(true))) {
this.setSelected(true);
}
else {
this.setSelected(false);
}
return this;
}

/**
* sets the state of the cell
*
* @param enabled boolean
*/
public void setCellEnabled(boolean enabled) {
cellEnabled = enabled;
}

/**
* determines whether this cell is editable or not
*
* @param anEvent <code>EventObject</code>
* @return boolean
*/
public boolean isCellEditable(EventObject anEvent) {
return cellEnabled;
}

/**
* determines whether this cell is editable or not
*
* @param anEvent <code>EventObject</code>
* @return boolean
*/
public boolean isCellEnabled(EventObject anEvent) {
return cellEnabled;
}
}





// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

}

Ranjith PN

unread,
Sep 23, 2008, 7:49:14 AM9/23/08
to javawor...@googlegroups.com
Thanks


2008/9/23 shiv...@gmail.com <shiv...@gmail.com>:

--
Thanks & Regards
Ranjith

shashidhar D.M

unread,
Sep 23, 2008, 12:11:06 PM9/23/08
to javawor...@googlegroups.com

Thank you Ranjith
 
Reply all
Reply to author
Forward
0 new messages