TableCellRenderer 出现问题,尝试在 JTable Cell 中添加 JLabel(image)
这是我的代码 -
class ButtonCellRenderer extends AbstractCellEditor
implements TableCellRenderer,TableCellEditor,MouseListener{
JTable table;
JLabel rendererLabel ;
JButton editButton ;
String text = "BAKRA";
public ButtonCellRenderer(JTable table, int column) {
this.table = table;
rendererLabel = new JLabel("value.png");
//rendererBut.setToolTipText("BUNTHAAAAAAAAAAAAAA");
rendererLabel .addMouseListener(this);
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(column).setCellRenderer( this );
columnModel.getColumn(column).setCellEditor( this );
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row
, int column) {
// TODO Auto-generated method stub
rendererLabel.setOpaque(true);
if(isSelected)
rendererLabel.setBackground( table.getSelectionBackground());
else
rendererLabel.setBackground(Color.WHITE);
return rendererLabel ;
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{
return rendererLabel ;
}
public Object getCellEditorValue() {
// TODO Auto-generated method stub
return text;
}
public void mousePerformed(ActionEvent ev) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "UOBS BUTTON PRESSED",
"BUTTON PRESSED"
,JOptionPane.ERROR_MESSAGE);
}
}
一切正常,但是 - 实际上,当 JTable 窗口打开时,如果我直接单击基于 JLabel(图像)的 Column ,则图像会消失一段时间,并且表将所选行返回为 -1。 还有一点是行选择也仅限于前一列。
准确地说,如果我的表格有 4 列并且图像(JLabel)位于 第 4 列,那么如果我直接单击图像或第 4 列 ,然后行选择发生到第三列并返回 行选择为-1。但如果我选择任何其他列, 一切都正常并且工作正常。
Here is my piece of code -
class ButtonCellRenderer extends AbstractCellEditor
implements TableCellRenderer,TableCellEditor,MouseListener{
JTable table;
JLabel rendererLabel ;
JButton editButton ;
String text = "BAKRA";
public ButtonCellRenderer(JTable table, int column) {
this.table = table;
rendererLabel = new JLabel("value.png");
//rendererBut.setToolTipText("BUNTHAAAAAAAAAAAAAA");
rendererLabel .addMouseListener(this);
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(column).setCellRenderer( this );
columnModel.getColumn(column).setCellEditor( this );
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row
, int column) {
// TODO Auto-generated method stub
rendererLabel.setOpaque(true);
if(isSelected)
rendererLabel.setBackground( table.getSelectionBackground());
else
rendererLabel.setBackground(Color.WHITE);
return rendererLabel ;
}
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{
return rendererLabel ;
}
public Object getCellEditorValue() {
// TODO Auto-generated method stub
return text;
}
public void mousePerformed(ActionEvent ev) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "UOBS BUTTON PRESSED",
"BUTTON PRESSED"
,JOptionPane.ERROR_MESSAGE);
}
}
In this everything is working fine but -
Actually when the JTable window opens and if I click directly on the JLabel (image) based Column , then the image is gone for a while and the table returns the selected row as -1.
One more point is that the Row selection as well restricted to the previous column.
Precisely , If my Table has 4 column and Image(JLabel) is on the
column number 4th, then if I directly click on the image or column 4th
, then the row selection happens till the 3rd column and it returns
the row selection as -1. But If I select any other column, the
everything is proper and works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无需创建自定义渲染器来显示图像。 JTable 已经支持图标的默认渲染器。只需将 ImageIcon 添加到模型即可。然后,您需要重写
getColumnClass()
方法以返回该列的 Icon.class,并且将使用正确的渲染器。如果您尝试创建某种可点击的按钮,那么您可以使用 表格按钮列也支持图标。
There is no need to create a custom renderer to display an image. JTable already supports a default renderer for Icons. Just add an ImageIcon to the model. Then you need to override the
getColumnClass()
method to return Icon.class for that column and the proper renderer will be used.If you are trying to create some kind of clickable button then you can use the Table Button Column which also supports icons.