KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   HTML  JAVA   Swing   Webtip  Lyric  Free sheet music  Ringtones

Best books about JAVA programming:

JAVA JTable JComponent

The following code show to add any JComponent in a JTablecell. Sample shows Jbuttons, Jlabels, Jcombobox, Jscrolbar, JCheckbox and radio buttons . These components behave in the regular manner to mouse actions.

The JComponentCellRenderer implements the TableCellRenderer and its single method simply returns the component in the cell to be rendered.

  • Table.java
  • JComponentCellEditor.java




    import java.awt.Component;
    import java.awt.event.*;
    import java.awt.AWTEvent;
    import java.lang.Boolean;
    import javax.swing.table.*;
    import javax.swing.event.*;
    import java.util.EventObject;
    import javax.swing.tree.*;
    import java.io.Serializable;
    import javax.swing.*;

    public class JComponentCellEditor implements TableCellEditor, TreeCellEditor,
    Serializable {

    protected EventListenerList listenerList = new EventListenerList();
    transient protected ChangeEvent changeEvent = null;

    protected JComponent editorComponent = null;
    protected JComponent container = null; // Can be tree or table


    public Component getComponent() {
    return editorComponent;
    }


    public Object getCellEditorValue() {
    return editorComponent;
    }

    public boolean isCellEditable(EventObject anEvent) {
    return true;
    }

    public boolean shouldSelectCell(EventObject anEvent) {
    if( editorComponent != null && anEvent instanceof MouseEvent
    && ((MouseEvent)anEvent).getID() == MouseEvent.MOUSE_PRESSED )
    {
    Component dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent, 3, 3 );
    MouseEvent e = (MouseEvent)anEvent;
    MouseEvent e2 = new MouseEvent( dispatchComponent, MouseEvent.MOUSE_RELEASED,
    e.getWhen() + 100000, e.getModifiers(), 3, 3, e.getClickCount(),
    e.isPopupTrigger() );
    dispatchComponent.dispatchEvent(e2);
    e2 = new MouseEvent( dispatchComponent, MouseEvent.MOUSE_CLICKED,
    e.getWhen() + 100001, e.getModifiers(), 3, 3, 1,
    e.isPopupTrigger() );
    dispatchComponent.dispatchEvent(e2);
    }
    return false;
    }

    public boolean stopCellEditing() {
    fireEditingStopped();
    return true;
    }

    public void cancelCellEditing() {
    fireEditingCanceled();
    }

    public void addCellEditorListener(CellEditorListener l) {
    listenerList.add(CellEditorListener.class, l);
    }

    public void removeCellEditorListener(CellEditorListener l) {
    listenerList.remove(CellEditorListener.class, l);
    }

    protected void fireEditingStopped() {
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
    if (listeners[i]==CellEditorListener.class) {
    // Lazily create the event:
    if (changeEvent == null)
    changeEvent = new ChangeEvent(this);
    ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent);
    }
    }
    }

    protected void fireEditingCanceled() {
    // Guaranteed to return a non-null array
    Object[] listeners = listenerList.getListenerList();
    // Process the listeners last to first, notifying
    // those that are interested in this event
    for (int i = listeners.length-2; i>=0; i-=2) {
    if (listeners[i]==CellEditorListener.class) {
    // Lazily create the event:
    if (changeEvent == null)
    changeEvent = new ChangeEvent(this);
    ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent);
    }
    }
    }

    // implements javax.swing.tree.TreeCellEditor
    public Component getTreeCellEditorComponent(JTree tree, Object value,
    boolean isSelected, boolean expanded, boolean leaf, int row) {
    String stringValue = tree.convertValueToText(value, isSelected,
    expanded, leaf, row, false);

    editorComponent = (JComponent)value;
    container = tree;
    return editorComponent;
    }

    // implements javax.swing.table.TableCellEditor
    public Component getTableCellEditorComponent(JTable table, Object value,
    boolean isSelected, int row, int column) {

    editorComponent = (JComponent)value;
    container = table;
    return editorComponent;
    }

    } // End of class JComponentCellEditor


Sponsored link
Java JTabbedPane
JTabbedPane Preview List
Java JComboBox
JComboBox Preview List
Java JTooltip
JTooltip Preview List
Java JList
JList Preview List
Java JTree
JTree Preview List 1
JTree Preview List 2
JAVA JTable
JTable Preview List 1
JTable Preview List 2
JTable Preview List 3
JTable Preview List 4
JTable Preview List 5
JTable Preview List 6
JTable Preview List 7
JAVA Jslide
JSlide Preview List

 

 

Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)

 

Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer