Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JButton is not clickable in JTable

843806Apr 8 2008 — edited Apr 9 2008
Hi all,

I have a JTable which is created using a TableModel, now I want to have an erase button in the last column.
Therefore I use a ButtonRenderer but the problem is that I can't click on the buttons! They are visible and enabled but I just can't click them.

Code:
//Stuff..(Default Constructor,...)
private void initGui()
	{
		
		crudTabel = new JTable(ctm);
		crudTabel.setAutoCreateRowSorter(true);
		crudTabel.getColumn("Wissen").setCellRenderer(new ButtonRenderer());
		crudTabel.setVisible(true);
		JScrollPane jp = new JScrollPane(crudTabel);
		jp.setVisible(true);
		jp.setPreferredSize(new Dimension(500,500));
		this.getContentPane().add(jp);
		
	}
	class ButtonRenderer implements TableCellRenderer
	{
		Icon icon = new ImageIcon("gfx/delete.gif");
		JButton tabelButton;
		  public ButtonRenderer() 
		  {
		   tabelButton = new JButton();
		    tabelButton.setIcon(icon);
		    tabelButton.setVisible(true);
		    tabelButton.setEnabled(true);
		    
		    tabelButton.addActionListener(new ActionListener()
		    {

				public void actionPerformed(ActionEvent e) {
					tabelButtonActionPerformed();
					
				}
		    	
		    	
		    });
		    
		    
		  }
		  
		  public Component getTableCellRendererComponent(JTable table, Object value,
		                   boolean isSelected, boolean hasFocus, int row, int column) {
		    if (isSelected) {
		    	tabelButton.setForeground(table.getSelectionForeground());
		    	tabelButton.setBackground(table.getSelectionBackground());
		    }
		    else
		    {
		    	
		    	tabelButton.setForeground(table.getSelectionForeground());
		    	tabelButton.setBackground(table.getSelectionBackground());		      
		    }
		    
		    return tabelButton;
		  }

		

		
		}
		
		public void tabelButtonActionPerformed()
		{
			System.out.print("\nButton click!");
		}
How can I make them clickable so my actionPerformed can be executed?

Greetz Daan
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 7 2008
Added on Apr 8 2008
9 comments
1,354 views