Adding JButton into JTable cells

Hi there!!

I want to add a JButton into JTable cells.In fact I have got two classes.Class2 has been extended from the AbstractTableModel class and Class1 which is using Class2's model,,,here's the code,,

Class1

--

import javax.swing.JScrollPane;

import javax.swing.JTable;

publicclass Class1extends javax.swing.JFrame{

/*

//////GUI specifications

*/

publicstaticvoid main(String args[]){

java.awt.EventQueue.invokeLater(new Runnable(){

publicvoid run(){

new TestTableButton().setVisible(true);

}

});

Class2 model=new Class2();

jTable1=new JTable(model);

jScrollPane1.setViewportView(jTable1);

}

// Variables declaration - do not modify

private javax.swing.JScrollPane jScrollPane1;

// End of variables declaration

private javax.swing.JTable jTable1;

}

Class2

--

import javax.swing.table.*;

publicclass Class2extends AbstractTableModel{

private String[] columnNames ={"A","B","C"};

private Object[][] data ={

{"","",""},

{"","",""},

{"","",""},

{"","",""},

{"","",""},

{"","",""},

{"","",""},

{"","",""},

{"","",""}

};

publicint getColumnCount(){

return columnNames.length;

}

publicint getRowCount(){

return data.length;

}

public String getColumnName(int col){

return columnNames[col];

}

public Object getValueAt(int row,int col){

return data[row][col];

}

public Class getColumnClass(int c){

return getValueAt(0, c).getClass();

}

/*

* Don't need to implement this method unless your table's

* editable.

*/

publicboolean isCellEditable(int row,int col){

//Note that the data/cell address is constant,

//no matter where the cell appears onscreen.

returnfalse;

}

/*

* Don't need to implement this method unless your table's

* data can change.

*/

publicvoid setValueAt(Object value,int row,int col){

data[row][col] = value;

fireTableCellUpdated(row, col);

}

}

what modifications shud I be making to the Class2 calss to add buttons to it?

Can anybody help plz,,,,,?

Thanks in advance..

[6158 byte] By [Kami_Pakistana] at [2007-11-27 6:06:56]
# 1
What searches have you done to find a solution? I did this on Google: http://www.google.com/search?q=JButton+in+JTableSeems several hits there could be useful, and on my screen, both the two first hits have 獼Button in JTable?in their title.
rebola at 2007-7-12 16:23:22 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi rebol!

I did search out a lot for this but I found my problem was a bit different,,in fact I want to use another class's model into a class being extended by JFrame,,so was a bit confused,,,hope you can give me some ideas about how to handle that scenario,,I know this topic has been discussed before here many a times and also have visited this link,,

http://forum.java.sun.com/thread.jspa?threadID=465286&messageID=2147913

but am not able to map it to my need,,,hope you can help me a bit...

Thanks .....

Kami_Pakistana at 2007-7-12 16:23:22 > top of Java-index,Desktop,Core GUI APIs...
# 3
I believe to need to create custom cell renderers and cell editors. I am not 100% sure on that. Have you checked the tutorial trail?
Trini_Richa at 2007-7-12 16:23:22 > top of Java-index,Desktop,Core GUI APIs...
# 4
> but am not able to map it to my needWell I don't know what your need is. Your requirement says:I want to add a JButton into JTable cells.and thats what the example does. So its up to you to understand the basic code and then make the change.
camickra at 2007-7-12 16:23:22 > top of Java-index,Desktop,Core GUI APIs...