Propagating an event to the cell renderer

Hi All,
I have a JTable cell which renders the value object in two separate Text Fields. So the table cell appears to have two text fields. I want to popup a dialog when the user clicks on one of those text fields. Not sure how to implement this.
Thanks.

Apologies for being in the wrong forum. I will keep that in mind for future postings.
The mouse listener pops up the dialog on a mouse click on the table cell. But I want the dialog to popup when the mouse is clicked on a certain area of the cell, more precisely on one of the text fields.

Similar Messages

  • Best Practice for Changing the Cell-Renderer in a TreeTab

    I've implemented a TreeTable (sap.ui.table.TreeTable). Now, I would like to change the way how table cells are being rendered. Intuitively, I would create a subclass "myrenderer" which extends sap.ui.table.TableRenderer.renderTableCell and assign this renderer as cell renderer with something like (pseudocode):
    myTreeTable.setCellRenderer(myrenderer);
    Any help/hint would be appreciated. An example would be appreciated even more ;-).
    Bye
        Christian

    Hi Christian
    You can extend from sap.ui.table.TreeTable like this Example
    -D

  • How to Listen an event before the page Renders

    Hi , we fired an event from the header . I want to listen the payload and show that data from the payload in a portlet on the page.
    But am not getting the data into the portlet. From the logs i can see that after the pageflow action executed, backing file is executed.

    What part of the lifecycle are you using in the backing file?
    Consider using handlePostBackData in your backing file.
    See:
    http://download.oracle.com/docs/cd/E13155_01/wlp/docs103/portlets/ipc.html
    Also, for some older but good basic reading, see:
    http://download-llnw.oracle.com/docs/cd/E13218_01/wlp/docs81/sp3/pdf/netix.pdf
    http://download.oracle.com/docs/cd/E13218_01/wlp/docs81//sp3/pdf/ipcguide.pdf

  • JTable :Setting the cell renderer overrides default behaviour

    I have a class renderer as shown below.
    I would like to apply this renderer to my cells in my JTable (see below):
    When I use "setDefaultRenderer" I get the behaviour I expect. Fx. that number columns a right aligned, dates show as dates etc. However when I use "setCellRenderer" it eems that the functionality of the superclass is overridden ??. Any explanation - anyone ?
          TableColumnModel  tcModel;
          JTable table
          tcModel = table.getColumnModel();
          Enumeration       enum;
          enum = tcModel.getColumns();
          /* This works */     
          table.setDefaultRenderer(Object.class,new renderer()); //The renderer works for all objects.
          while ( enum.hasMoreElements() )
             tColumn = (TableColumn)enum.nextElement();
             /* But this does not !!? */
             /* tColumn.setCellRenderer(new renderer());   */
    class renderer extends DefaultTableCellRenderer
       public Component getTableCellRendererComponent(
          JTable _table,
          Object _value,
          boolean _isSelected,
          boolean _hasFocus,
          int _row, int _col)
       Component cell = super.getTableCellRendererComponent(_table,_value,_isSelected,_hasFocus,_row,_col);
       return cell ;
    }

    I have a class renderer as shown below.
    I would like to apply this renderer to my cells in my JTable (see below):
    When I use "setDefaultRenderer" I get the behaviour I expect. Fx. that number columns a right aligned, dates show as dates etc. However when I use "setCellRenderer" it eems that the functionality of the superclass is overridden ??. Any explanation - anyone ?
          TableColumnModel  tcModel;
          JTable table
          tcModel = table.getColumnModel();
          Enumeration       enum;
          enum = tcModel.getColumns();
          /* This works */     
          table.setDefaultRenderer(Object.class,new renderer()); //The renderer works for all objects.
          while ( enum.hasMoreElements() )
             tColumn = (TableColumn)enum.nextElement();
             /* But this does not !!? */
             /* tColumn.setCellRenderer(new renderer());   */
    class renderer extends DefaultTableCellRenderer
       public Component getTableCellRendererComponent(
          JTable _table,
          Object _value,
          boolean _isSelected,
          boolean _hasFocus,
          int _row, int _col)
       Component cell = super.getTableCellRendererComponent(_table,_value,_isSelected,_hasFocus,_row,_col);
       return cell ;
    }

  • Event Handling in JTable Custom Cell Renderer

    I have a JLabel as a custom cell Renderer for a column. I want to handle mouse click event for the JLabel rendered in the cell.
    I tried adding the listener for the label in the custom cell renderer but it is not working. Any ideas how to handle this problem??
    Thanks
    S.Anand

    If you want to handle the selection of a tree node
    1) write a class like:
    public class TreePaneListener implements TreeSelectionListener {
    // TREE SELECTION LISTENER
    public void valueChanged(TreeSelectionEvent e) {
    JTree tree = (JTree)e.getSource();
    DefaultMutableTreeNode node = null;
    int count = 0;
    boolean doSomething = false;
    if(tree.getSelectionCount() > 1) {
         TreePath[] selection = tree.getSelectionPaths();
         int[] temp = new int[selection.length];
         for(int i =0; i < selection.length; i++) {
    // Check each node for selection
         node = (DefaultMutableTreeNode)selection.getLastPathComponent();
         if(node.getLevel()==2) {
    // Change this code to take the action desired
         doSomething = true;
    2) After creating the tree register the listener
    TreePaneListener handler = new TreePaneListener();
    tree.addTreeSelectionListener(handler);

  • JButton inside the cell of JTable

    JButton buttonInCellOfTable = new JButton();
    buttonInCellOfTable.setBackground(Color.RED);
    buttonInCellOfTable.setText("MORE");
    buttonInCellOfTable.setPreferredSize(new Dimension(10, 22));
    buttonInCellOfTable.setMinimumSize(new Dimension(10, 22));
    buttonInCellOfTable.setMaximumSize(new Dimension(10, 22));
    myJTable.addRow("No. 1", buttonInCellOfTable);
    But Color and size isn't shown unlike those are set.
    What can I do?

    * @(#)ButtonTableCellRenderer.java     Tiger     2005 April 29
    package com.yahoo.ron.table;
    import java.awt.Component;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax,swing.JTable;
    import javax.swing.table.TableCellRenderer;
    * @author     Ronillo Ang - [email protected]
    final public class ButtonTableCellRenderer extends JButton implements TableCellRenderer{
         public ButtonTableCellEditor(ActionListener actionListener){
              super("Editor");
              if(actionListener == null)
                   throw new NullPointerException();
              addActionListener(actionListener);
         public Component getTableCellRenderComponent(JTable table, Object value, boolean selected, boolean hasFocus, int row, int col){
              if(value instanceof String){
                   String text = String.valueOf(value);
                   setText(text);
              else
                   setText("Unknown value");
              return this;
    * @(#)TestingButtonTableCellRenderer.java      Tiger     2005 April 29
    package com.yahoo.ron.test;
    import com.yahoo.ron.table.ButtonTableCellRenderer;
    import com.yahoo.ron.table.UneditableTableModel;
    import com.yahoo.ron.ThreadMonitor;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableColumnModel;
    * @author     Ronillo Ang - [email protected]
    * A class to test com.yahoo.ron.table.ButtonTableCellRenderer
    public class TestingButtonTableCellRenderer implements ActionListener{
         public TestingButtonTableCellRenderer(){
              JFrame frame = new JFrame("Testing ButtonTableCellRenderer");
              frame.getContentPane().setLayout(new GridLayout(1, 1));
              JTable table = new JTable(new UneditableTableModel(new ThreadMonitor()));
              TableColumnModel tableColumnModel = table.getColumnModel();
              // get the last column.
              TableColumn tableColumn = tableColumnModel.getColumn(tableColumnModel.getColumnCount() - 1);
              // apply the cell renderer.
              tableColumn.setCellRenderer(new ButtonTableCellRenderer(this));
              frame.getContentPane().add(new JScrollPane(table));
              frame.pack();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
         public void actionPerformed(ActionEvent ae){
              // do nothing for now.
         static public void main(String[] args){
              new TestingButtonTableCellRenderer();
    // Okie!! I dont include my source of ThreadMonitor.java and UneditableTableModel.
    // If problem occur, make some modification. Okie!
    // Thanks! God bless you all!

  • JTable cell rendering lag

    I've got a JTable for which I wrote a custom CellRenderer that extends JLabel. For each cell, I set the icon for the JLabel that is going to be rendered in the cell. The icon is a gif with some transparent elements. So I set the JLabel to opaque and set the background color so it will show through in the transparent areas. Works like I intended it to, but there's a lag. The table I have has enough rows to scroll well beyond the JScrollPane that it's in. When I scroll down the table, all the cells show up briefly as only the background color, then change quickly to the icon that is in the JLabel. So when I scroll, the entire table seems to be the background color without any icons. When I stop scrolling, the icons fill in pretty quick. But it's disconcerting when scrolling. Once a certain region (set of rows) has been scrolled to once, the problem doesn't happen if you scroll away and then back to that same region.
    Thanks.
    ab.

    I had considered that and eliminated that route through some testing.
    I did sort of figure out what the problem is, but don't yet have a solution. The table I'm rendering has variable height rows. Even rows are one height, odd rows another height. In my custom renderer, I modify the row heights as:
              if (getTable() != null)
                   if (row % 2 == 0)
                        if(CommonStyle.SUMMARY_ROW_HEIGHT != getTable().getRowHeight(row))
                             getTable().setRowHeight(row, CommonStyle.SUMMARY_ROW_HEIGHT);
                   else
                        if(CommonStyle.ARROW_ROW_HEIGHT != getTable().getRowHeight(row))
                             getTable().setRowHeight(row, CommonStyle.ARROW_ROW_HEIGHT);
              }Turns out changing the row heights during the rendering process is what's causing the lag, perhaps there is some table structure changing event that I need to catch and suppress. I've got some optimization code in the cell renderer to no-op the repaint and property change events. I'll let you know what I find.
    Thanks.
    ab.

  • How to display multiple JComponents in a tree cell renderer

    I have an object in a tree cell renderer and want to display its members(three members) status in a JTree as checkboxes such that each node displays three checkboxex with member-names and a node name. i tried using a JPanel and adding three labels into this panel to be returned for the cell renderer but the GUI fails to paint the node componnents. However on clicking the node the component which isn't visible displays correctly. please Help me out

    Since you didn't provide any sample code, it's all about wild guesses on what your problem is. The following code shows the type of program you could have posted :import javax.swing.*;
    import javax.swing.tree.*;
    import java.awt.*;
    public class TestTree extends JPanel {
         private static class MyCell {
              String theCellName;
              boolean theFirstField;
              boolean theSecondField;
              boolean theThirdField;
              public MyCell(String aName, boolean firstField, boolean secondField, boolean thirdField) {
                   theCellName = aName;
                   theFirstField = firstField;
                   theSecondField = secondField;
                   theThirdField = thirdField;
         private static class MyTreeCellRenderer extends JPanel implements TreeCellRenderer {
              private JLabel theCellNameLabel;
              private JCheckBox theFirstCheckBox;
              private JCheckBox theSecondCheckBox;
              private JCheckBox theThirdCheckBox;
              private DefaultTreeCellRenderer theDelegate;
              public MyTreeCellRenderer() {
                   super(new GridLayout(4, 1));
                   theCellNameLabel = new JLabel();
                   add(theCellNameLabel);
                   theFirstCheckBox = new JCheckBox("firstField");
                   add(theFirstCheckBox);
                   theSecondCheckBox = new JCheckBox("secondField");
                   add(theSecondCheckBox);
                   theThirdCheckBox = new JCheckBox("thirdField");
                   add(theThirdCheckBox);
                   theDelegate = new DefaultTreeCellRenderer();
                   setOpaque(true);
              public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected,
                                                                       boolean expanded, boolean leaf, int row, boolean hasFocus) {
                   if (!(value instanceof DefaultMutableTreeNode)) {
                        return theDelegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
                   Object userObject = ((DefaultMutableTreeNode)value).getUserObject();
                   if (!(userObject instanceof MyCell)) {
                        return theDelegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
                   setBackground(tree.getBackground());
                   if (selected) {
                        setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
                   } else {
                        setBorder(BorderFactory.createLineBorder(getBackground(), 2));
                   MyCell cell = (MyCell)userObject;
                   theCellNameLabel.setText(cell.theCellName);
                   theFirstCheckBox.setSelected(cell.theFirstField);
                   theSecondCheckBox.setSelected(cell.theSecondField);
                   theThirdCheckBox.setSelected(cell.theThirdField);
                   return this;
              public Component add(Component comp) {
                   if (comp instanceof JComponent) {
                        ((JComponent)comp).setOpaque(false);
                   return super.add(comp);
         public TestTree() {
              super(new BorderLayout());
              JTree tree = new JTree(createModel());
              tree.setShowsRootHandles(true);
              tree.setCellRenderer(new MyTreeCellRenderer());
              add(new JScrollPane(tree), BorderLayout.CENTER);
         private static final TreeModel createModel() {
              DefaultMutableTreeNode root = new DefaultMutableTreeNode(new MyCell("root", true, true, true));
              DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(new MyCell("child1", false, true, false));
              DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(new MyCell("child2", false, false, true));
              root.add(child1);
              root.add(child2);
              return new DefaultTreeModel(root);
         public static void main(String[] args) {
              final JFrame frame = new JFrame("Test");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setContentPane(new TestTree());
              SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        frame.setSize(600, 400);
                        frame.show();
    }

  • CheckBox in customized cell renderer - Urgent

    i need to customize the cell renderer for a table for one of the columns. One of the column is Boolean type and i want it the way it comes in a default cell renderer.
    if i mention the renderer for the column as "checkbox" - it gives me the checkbox "left aligned" i want it in the center.
    what shall i do?

    that works.
    thanks ivan.
    could i get the same result with defaultTableCellRenderer? as it extends JLabel i get the label-"true/false" not the checkbox.

  • JTable with JPanel form as cell renderer/editor

    I have a JTable that uses a custom cell editor/renderer. The cell editor/renderer is a JPanel form with labels and text panes. As the user edits the information, I adjust the table row height (setRowHeight) and the JPanel layout manager updates the layout on the cell editor. This all seems to work fine. However, when the user exists the cell and the cell renderer is shown, it has the correct new row height but has not been correctly layed-out for the new row height. How do I force my cell renderer to be re-layout after the editor closes?

    That does not seem to work. I have done the following:
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int r, int c)
    this.table = (EditorTable) table;
    row = r;
    column = c;
    if (isSelected)
    setBackground (Preferences.selectedRowBgColor);
    else
    setBackground (formBg);
    // constructs the panel components
    setCellValue (value);
    revalidate();
    return (this);

  • Custom table model, table sorter, and cell renderer to use hidden columns

    Hello,
    I'm having a hard time figuring out the best way to go about this. I currently have a JTable with an custom table model to make the cells immutable. Furthermore, I have a "hidden" column in the table model so that I can access the items selected from a database by their recid, and then another hidden column that keeps track of the appropriate color for a custom cell renderer.
    Subject -- Sender -- Date hidden rec id color
    Hello Pete Jan 15, 2003 2900 blue
    Basically, when a row is selected, it grabs the record id from the hidden column. This essentially allows me to have a data[][] object independent of the one that is used to display the JTable. Instinctively, this does not seem right, but I don't know how else to do it. I know that the DefaultTableModel uses a Vector even when it's constructed with an array and I've read elsewhere that it's not a good idea to do what I'm trying to do.
    The other complication is that I have a table sorter as well. So, when it sorts the objects in the table, I have it recreate the data array and then set the data array of the ImmutableTableModel when it has rearranged all of the items in the array.
    On top of this, I have a custom cell renderer as well. This checks yet another hidden field and displays the row accordingly. So, not only does the table sort need to inform the table model of a change in the data structure, but also the cell renderer.
    Is there a better way to keep the data in sync between all of these?

    To the OP, having hidden columns is just fine, I do that all the time.. Nothing says you have to display ALL the info you have..
    Now, the column appears to be sorting properly
    whenever a new row is added. However, when I attempt
    to remove the selected row, it now removes a seemingly
    random row and I am left with an unselectable blank
    line in my JTable.I have a class that uses an int[] to index the data.. The table model displays rows in order of the index, not the actual order of the data (in my case a Vector of Object[]'s).. Saves a lotta work when sorting..
    If you're using a similar indexing scheme: If you're deleting a row, you have to delete the data in the vector at the INDEX table.getSelectedRow(), not the actual data contained at
    vector.elementAt(table.getSelectedRow()). This would account for a seemingly 'random' row getting deleted, instead of the row you intend.
    Because the row is unselectable, it sounds like you have a null in your model where you should have a row of data.. When you do
    vector.removeElementAt(int), the Vector class packs itself. An array does not. If you have an array, when you delete the row you must make sure you dont have that gap.. Make a new array of
    (old array length-1), populate it, and give it back to your model.. Using Vectors makes this automatic.
    Also, you must make sure your model knows the data changed:
    model.fireTableDataChanged(); otherwise it has no idea anything happened..
    IDK if that's how you're doing it, but it sounds remarkably similar to what I went thru when I put all this together..

  • JTree as cell renderer for JList

    I have an application that requires to display a list of tree-structured data.
    So I've used JTree a the cell renderer for the JList, and I can see a list of trees with that data in.
    However, the Jtree doesn't respond to Mouse messages, even if I dispatch the to it manually. So the tree is essentially dead.
    Does anybody know how to fix this?

    I'm not sure if they have the same thing for lists though.Yes, it is so - a cellrenderer or celleditor is a component, that is only there during it is used - a cellrenderer is there as long as it needs to paint the contents, a celleditor is there, if an edit-process is invoked and it will get messages as long as the editing process continues - after finishing editing, the component is no longer there - normally the renderer is called after that, to render the new contents into the rectangle of that cell, because the contents in its non-editing state may look other than that from the editor during the editing-state.
    greetings Marsian

  • JList or JTree Cell Renderer

    hi. im doing a scheduler application
    wherein a list is displayed and when user post
    a scheduled activity, a textbox will be painted
    on top of the list, so that you could do away with
    overlapping schedules(2 or more schedule with
    the same time interval/s). Much like as that of
    the MS Outlook calendar.
    Can anyone post a snippet of the code of the
    cell renderer? im having a hard time studying
    cell rendering.
    thank you very much.

    Pls i badly nedd your help. somebody knowledgable on this?

  • Changing JList cell renderer on selection

    Hi,
    In our application we need to change the renderer of the selected cell of JList to JTextArea while maintaining default cell renderer for unselected cells. I tried by providing custom cell renderer (code is given below) but it does not work..:-(. Though the component used by JList for rendering the cell is JTextArea, the height of the cell remains same as that of unselected cells. Our requirement is to change the cell height of the selected row so as to give a feel that selected row expands and shows some more information about the selected item to the user.
    Here is the code snippet of the cell renderer that I wrote:
    class CellRenderer1 extends DefaultListCellRenderer{
    private JTextArea selTxtArea;
    CellRenderer1() {
    selTxtArea = new JTextArea(3,20);
    this.setOpaque(true);
    public Component getListCellRendererComponent(JList list,
    Object value, int index,
    boolean isSelected, boolean cellHasFocus) {
    String name = (String) value;
    if ( isSelected ) {
    selTxtArea.setBackground(list.getSelectionBackground());
    selTxtArea.setForeground(list.getSelectionForeground());
    selTxtArea.setText(name + "\n" + name);
    return selTxtArea;
    else {
    this.setBackground(list.getBackground());
    this.setForeground(list.getForeground());
    this.setText(name);
    return this;
    //return this;
    Any pointers or help will be highly appreciated.
    Thanks
    Atul

    JList calculates fixedCellHeight and then uses the same for every cell. This was causing the problem. By overriding the getRowHeight method of BasicListUI class I was able to achieve different cell heights for selected and unselected rows. Following is the code snippet which shows how this was achieved:
    protected int getRowHeight(int row) {
    if ( (cellHeights == null) || (cellHeights.length < row )) {
    cellHeights = new int[row];
    ListModel model = list.getModel();
    Object value = model.getElementAt(row);
    ListSelectionModel selModel = list.getSelectionModel();
    boolean isSelected = selModel.isSelectedIndex(row);
    Component comp = list.getCellRenderer().
    getListCellRendererComponent( list, value, row,
    isSelected, false);
    Dimension dim = comp.getPreferredSize();
    int height = dim.height;
    cellHeights[row] = height;
    return cellHeights[row];
    }

  • Using JScrollpane via Custom Cell Renderer.Please Help

    Chaps,
    I have a question regarding scrolling of a JList.
    I have written a class (FTRDList) that has an Custom Cell Renderer.
    I want the list to be scrollable.
    This class is being called by many other classes.
    Now,as the FTRDList class is being called by many classes,i need
    to have all these classes to have the flwg statement
    add(new JScrollPane(FTRDList)),BorderLayout.CENTER);
    This can be a lot of work to chaneg all classes
    Rather,is it possible to have the JScrollPane built in the
    FTRDList class once and for all and let all classes call
    it by
    add(FTRDList,BorderLayout.CENTER);
    Please can anyone tell me how to make the FTRDList class Scrollable?
    Or is this not possible at all?
    Attached is the class:
    public class FTRDList extends JList {
        /* Inner class for Custom Cell Renderer */
        class MyCellRenderer extends JLabel implements ListCellRenderer {
        public Component getListCellRendererComponent(JList list,
                                                      Object value,            // value to display
                                                      int index,               // cell index
                                                      boolean isSelected,      // is the cell selected
                                                      boolean cellHasFocus) {  // the list and the cell have the focus
                setText(value.toString());
                if (isSelected) {
                setBackground(isSelected ? Color.red : Color.yellow);
             setForeground(isSelected ? Color.white : Color.black);
                setEnabled(list.isEnabled());
                setOpaque(isSelected);
                return this;
        public FTRDList() {
            super();
            setSelectedIndex(0);
            /** Invoke the cell Renderer */
            setCellRenderer(new MyCellRenderer());
            setOpaque(false);    

    I HAVE ALSO POSTED THIS IN THE JAVA PROGRAMMING FORUM PLEASE DONT GET OFFENDED AS I AM EXPECTING AN URGENT RESPONSEWell, someone has probably already answered this question in the Java forum, so I won't waste my time and answer it again here.

Maybe you are looking for