tags:

views:

29

answers:

1

I have a big button when clicked , adds an image to a table

    class BigButtonListener implements ActionListener{
 Image screenshot=null;
 Browser bigbrowser =null;

 BigButtonListener(Browser browser, DefaultTableModel dataModel, DefaultTableModel historyModel, JTable dataTable, JTable historyTable) {
  screenshot = browser.toImage(true);
  bigbrowser = browser;
  historyTable = historyTable;
  //table1.addRow
 }

  @Override
  public void actionPerformed(ActionEvent e) {
   // save current image
   historyModel.insertRow(0,new Object[]{new ImageIcon(screenshot)}); 
   //System.out.println(historyTable.getRowCount());

  }   
} 

however, all this adds is the text javax.swing.ImageIcon@9dfb04 and not the actual picture.

+1  A: 

DefaultTableCellRenderer extends JLabel and renders by simply setText(value.toString()).

Follow the definite Swing tutorial on custom cell renderer and editor.

Or use simple hack like this:

historyTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
    protected void setValue(Object value) {
        if( value instanceof ImageIcon ) {
            setIcon((ImageIcon)value);
            setText("");
        } else {
            setIcon(null);
            super.setValue(value);
        }
    }
});
Geoffrey Zheng