JTable problem..
Really need help.. urgent..
I'm creating a JTable which obtains its data from a database. This part i manage to get it up and working. My problem now is inserting a JButton (e.g. Edit, View, Delete) for user functionality.
I tried a few ways and also read thru forums with no progress. Then i found out that JTable treats every cell as a JLable. I'm stuck.
Thanks for your help in advance.
[417 byte] By [
-TiKuS-a] at [2007-10-3 8:54:27]

I just recently figured this out myself here is what I do to add buttons to a JTable.
This is the code I add to my display
//makes the buttons in the strategy report column
JTable table = new JTable();
table .getColumn("column name").setCellRenderer(new ButtonRenderer());
table .getColumn("column name").setCellEditor(new ButtonEditor(new JCheckBox()));
This is the class I add to my prgram to handle the buttons.
class ButtonRenderer extends JButton implements TableCellRenderer
{
public ButtonRenderer()
{
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column)
{
if(value==null)
setText("");
else
setText(value.toString());
return this;
}
}
class ButtonEditor extends DefaultCellEditor
{
protected JButton button;
private String label;
private boolean buttonIsPushed;
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(value==null)
label="";
else
label = value.toString();
button.setText(label);
buttonIsPushed = true;
return button;
}
public Object getCellEditorValue()
{
if (buttonIsPushed)
{
int selectedRow = marketTable.getSelectedRow();
if(selectedRow<numValidRows)
{
/********************************
put your code of what you want to happen when button is pushed.
*************************************/
}
buttonIsPushed = false;
return new String(label) ;
}
public boolean stopCellEditing()
{
buttonIsPushed = false;
return super.stopCellEditing();
}
protected void fireEditingStopped()
{
super.fireEditingStopped();
}
}
I hope this helps.
Cheers,
Ricky>
Saintsairforce's implementation has some specific dependency or coupling.
I think camickr's implementation is much more generic, standard, simple, reusable and scalable.
Study it if you have time and guts.
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=680674
hiwaa at 2007-7-15 4:04:26 >
