• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JButton in JTable

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Does any of you know how I can put a clickable JButton in a JTable ? I succeed in adding the JButton as a CellRenderer in my JTable, but it doesn't seem to react to ActionEvents...
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your question is answered here
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sven ,

Your button will respond to mouse clicks when you use cellEditor class.
I am sending an example code. Try to use it.


Step 1. Add this below line to your main class.

table.getColumnModel().getColumn(0).setCellEditor(new ButtonEditor(new JCheckBox()));


Step 2. Use this Cell Editor. When Button is clicked getCellEditorValue() method is called. Implement required functionality in that method.


class ButtonEditor extends DefaultCellEditor {

protected JButton button;
private String label;
private boolean isPushed;

public ButtonEditor(JCheckBox checkBox) {
super(checkBox);

button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
}

public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
if (isSelected)
{
button.setForeground(table.getSelectionForeground());
button.setBackground(table.getSelectionBackground());
}
else
{
button.setForeground(table.getForeground());
button.setBackground(table.getBackground());
}

label = (value ==null) ? "" : value.toString();
button.setText( label );
isPushed = true;
return button;
}

public Object getCellEditorValue() {
if (isPushed) {
JOptionPane.showMessageDialog(button ,label + ": Ouch!");
}

isPushed = false;
return new String( label ) ;
}

public boolean stopCellEditing()
{
isPushed = false;
return super.stopCellEditing();
}

protected void fireEditingStopped() {
super.fireEditingStopped();
}
}

Hope this helps you.

All The Best.
 
Sven Mari�n
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gee Sasi, this thing works! By adding my cell renderer aswell it looks solid.

Thanks and happy endings,

Sven
 
Sven Mari�n
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Sasi, just to let you know there's a simpler solution :



Cya
 
sasi kala
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya.

This code is good and understandable.

Thanx Sven.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic