java 如何在 jtable 的单元格内添加按钮并给它操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16799624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add buttons inside a cell of jtable and give it action
提问by Ogre3dUser
i know this has been answered before but considering im still a newbie i cant figure out a way to give actions and block the cell editing, i have tried it in several ways,i can successfully render the buttons however when i click on it, it edits the cell instead of pressing the button, i know that in order to avoid the cell editing i should create an abstract table and overide this method:
我知道之前已经回答过这个问题,但考虑到我仍然是一个新手,我无法找到一种方法来执行操作并阻止单元格编辑,我已经尝试了多种方法,我可以成功地呈现按钮但是当我点击它时,它编辑单元格而不是按下按钮,我知道为了避免单元格编辑,我应该创建一个抽象表并覆盖此方法:
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 3) {
return false;
} else {
return true;
}
}
however im using a default JTable,so this is the code i am using for the Panel and cell renderer:
但是我使用的是默认的 JTable,所以这是我用于面板和单元格渲染器的代码:
class PlusMinusCellRenderer extends JPanel implements TableCellRenderer {
public Component getTableCellRendererComponent(
final JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
if(column < 3)
{
JLabel campo = new JLabel(value.toString());
this.add(campo);
}
if(column > 2)
{
//this is a button
this.add(botaoteste);
materialtable.revalidate();
materialtable.repaint();
}
return this;
}
and this is the code i am using to retrieve data from sql to the Jtable(I customised the DefaultJTable code)
这是我用来从 sql 检索数据到 Jtable 的代码(我自定义了 DefaultJTable 代码)
String[] columnNames={"teste","abc","def"};
Object[][] data = new Object[1][4];
if(createConnection())
{
try {
Statement statemt = conLogin.createStatement();
ResultSet rs = statemt.executeQuery("SELECT * FROM Materiais");
//ResultSet rs = statemt.executeQuery("SELECT * FROM Materiais");
rsmtdata = rs.getMetaData();
//int columns = rsmtdata.getColumnCount();
columnNames = new String[]{rsmtdata.getColumnName(1),rsmtdata.getColumnName(2),rsmtdata.getColumnName(3),"Deletar"};
if(rs.next())
{
data[0][0] = rs.getString(1);
data[0][1] = rs.getString(2);
data[0][2] = rs.getString(3);
data[0][3] = new Boolean(false);
}
while (rs.next())
{
Object[][] temp = new Object[data.length+1][4];
for(int i=0;i < data.length;i++)
{
for(int j = 0;j < 4;j++)
{
temp[i][j] = data[i][j];
}
}
temp[data.length][0] = rs.getString(1);
temp[data.length][1] = rs.getString(2);
temp[data.length][2] = rs.getString(3);
temp[data.length][3] = new Boolean(false);
data = temp;
}
materialtable = new javax.swing.JTable(data, columnNames);
materialtable = new javax.swing.JTable(data, columnNames){
public TableCellRenderer getCellRenderer( int row, int column ) {
return new PlusMinusCellRenderer();
}
};
materialtable.setRowHeight( 32 );
} catch (SQLException ex) {
Logger.getLogger(ProfessorForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
//Create the scroll pane and add the table to it.
materialtable.setBackground(new java.awt.Color(153, 255, 51));
materialtable.setSelectionBackground(new java.awt.Color(255, 255, 51));
materialtable.setSelectionForeground(new java.awt.Color(255, 102, 0));
jScrollPane3.setViewportView(materialtable);
so to render the button inside the table i based on this thread: Adding Buttons inside cell of JTable along with data?
所以要根据这个线程在表格内呈现按钮: 在 JTable 的单元格内添加按钮和数据?
my question is very straight forward, how can i disable Row editing(just like the isCellEditable() method usage) and give action to the buttons? any help here is greatly appreciated and please take it to consideration im still novice so please detailed information or samples is needed ! Kind regards, Romulo Romero
我的问题很简单,如何禁用行编辑(就像 isCellEditable() 方法的用法一样)并对按钮进行操作?非常感谢这里的任何帮助,请考虑我仍然是新手,所以请提供详细信息或样本!亲切的问候,罗慕洛·罗梅罗
采纳答案by trashgod
You need botha renderer andand editor, as shown in this example. See How to Use Tables: Editors and Renderersfor details. Tangentially, you should override the method isCellEditable()
in your TableModel
rather than extending JTable
.
您需要既渲染器和和编辑,如本例。请参阅如何使用表:编辑器和渲染器的详细信息。四角切圆,你应该覆盖的方法isCellEditable()
在你的TableModel
而不是扩展JTable
。