-3

I have a JTable that just contains one item which is a JButtton that has an icon which is set to the JFreeChart generated chart.

If you click the button it launches a new window with the JFreeChart chart.

I have a cell renderer for the button which simply returns the JButton object that contains the chart item.

If I single click on the button it will launch the new window.

But if I double click on the button the new window launches, but the icon on the button, which used to be the JFreeChart, changes to just a text string with the class name path of the JButton with the class field values.

They other way I can get this behavior to happen is if I remove the cell renderer then the JButton just diplays the class name. So I'm not sure what is going on and have played around with it a ton and haven't made any progress. I was going to post some code but there is just too much of it. If anybody has any ideas, I can post certain sections of code to help explain. Here is code:

Class init {

    ...
    ItModel itModel = new ItModel ();
    JTable table = new JTable(itModel );
    TableRendrend rend = new TableRend();
    table.getColumn("it").setCellRenderer(rend);

}

class TableRend implements TableCellRenderer {
    JButton itButton;
    ...

    getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int columns){
         if(itButton ==null){
            itButton = new JButton (JButton)value.getIcon();
         }

        return itButton;

    }

}

Class itModel extends AbstractTableModel{
    JButton button;

...

public Object getValueAt(int rowIndex, intColumnIndex){
     if(button==null){
        button = new JButton( new ImageIcon(JFreeChartImage.getImage()));
        }

    return button
    }

}

This all works except when double clicking on the JButton and it displays TEXT ex.

javax.Swing.JButton(0,0,...)

all field values instead of the the actual chart image that should be displayed

Xeon
  • 5,949
  • 5
  • 31
  • 52
user565660
  • 1,171
  • 3
  • 20
  • 37

1 Answers1

2

There are several issues with the code you posted, and too long to include them all in a comment.

  1. You should not store Swing components in the model. The creation of the components is the task for the renderer
  2. I do not believe you can click the button which is returned by the renderer. That button is not contained in the JTable which is displayed. Only an image/screenshot/stamp of the button is drawn on-screen, but it does not behave like a button. You would need an editor for that. Consult the JTable tutorial about the editors and renderers for more information
Robin
  • 36,233
  • 5
  • 47
  • 99
  • If I don't store the JButton in model what do I return from getValueAt()?? Somehow I have to return something??? Or should I not need a model and just have the rendere do it all? – user565660 Dec 26 '12 at 18:14
  • @user565660: this related [example](http://stackoverflow.com/a/3591230/230513) may be of interest. – trashgod Dec 26 '12 at 18:58
  • Thank you very much for the resonses. I added a similar TableCellEditor and that seems to improve things. I don't really need to edit anything I just need the JButton to work for 2 clicks. I set the click count start to a very high value so it will never try to edit. Ex setClickCountToStart(Integer.Max). That seems to work for me. Thank you – user565660 Dec 26 '12 at 20:29