帮我看一下这个代码,有什么问题。谢谢(顶者有分)

yiqiangyang 2004-11-22 12:53:14
这是一个ColorEditor,和 可是在我的表里面用来渲染单元格时发现内存上涨很大,并且渲染另一个单元格时上一次的实例没有被回收,请大虾们帮忙看一下。谢谢!
public class ColorEditor extends AbstractCellEditor
implements TableCellEditor,
ActionListener {
protected Color currentColor;
protected JButton button;
protected JColorChooser colorChooser = null;
protected JDialog dialog;
protected static final String EDIT = "edit";
public ColorEditor() {
button = new JButton();
button.setActionCommand(EDIT);
button.addActionListener(this);
button.setBorderPainted(false);
//Set up the dialog that the button brings up.
colorChooser = new JColorChooser();
dialog = JColorChooser.createDialog(button,
"Pick a Color",
true, //modal
colorChooser,
this, //OK button handler
null); //no CANCEL button handler

}
public void actionPerformed(ActionEvent e) {
if (EDIT.equals(e.getActionCommand())) {
//The user has clicked the cell, so
//bring up the dialog.
button.setBackground(currentColor);
colorChooser.setColor(currentColor);
dialog.setVisible(true);

//Make the renderer reappear.
fireEditingStopped();
dialog.dispose();
}
else {
//User pressed dialog's "OK" button.
currentColor = colorChooser.getColor();
}
}

public Object getCellEditorValue() {
return currentColor;
}
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
currentColor = (Color)value;
return button;
}
}
/////////////////////////////////////////////////////////////////////////////
public class ColorRenderer extends JLabel implements TableCellRenderer {
Border unselectedBorder = null;
Border selectedBorder = null;
boolean isBordered = true;
public ColorRenderer(boolean isBordered) {
this.isBordered = isBordered;
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) {
Color newColor = (Color)color;
setBackground(newColor);
if (isBordered) {
if (isSelected) {
if (selectedBorder == null) {
selectedBorder = BorderFactory.createMatteBorder(2,5,2,5, table.getSelectionBackground());
}
setBorder(selectedBorder);
} else {
if (unselectedBorder == null) {
unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
table.getBackground());
}
setBorder(unselectedBorder);
}
}

setToolTipText("RGB value: " + newColor.getRed() + ", "
+ newColor.getGreen() + ", "
+ newColor.getBlue());
return this;
}
}
...全文
275 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
yiqiangyang 2004-11-25
  • 打赏
  • 举报
回复
最后一顶
yiqiangyang 2004-11-25
  • 打赏
  • 举报
回复
呵呵绝望了!
yiqiangyang 2004-11-24
  • 打赏
  • 举报
回复
我等!!!
seekcess 2004-11-23
  • 打赏
  • 举报
回复
我不懂啊!!!
kofiory 2004-11-23
  • 打赏
  • 举报
回复
顶!!!!!!!!!!!!!!!!
yiqiangyang 2004-11-23
  • 打赏
  • 举报
回复
呵呵,不好意思。
ColorEditor:
import javax.swing.AbstractCellEditor;
import javax.swing.table.TableCellEditor;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JTable;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
ColorRenderer:
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;
import java.awt.Color;
import java.awt.Component;
谢谢大家了
御南 2004-11-23
  • 打赏
  • 举报
回复
不得行啊
还待高手出现
yiqiangyang 2004-11-23
  • 打赏
  • 举报
回复
upupupupupupupup
redex 2004-11-23
  • 打赏
  • 举报
回复
对java的图形开发所知甚少,帮你UP!!!
beakham 2004-11-23
  • 打赏
  • 举报
回复
帮顶
御南 2004-11-22
  • 打赏
  • 举报
回复
就是啊,什么包都没导入,运行不起,来个完整的程序
cyicecream 2004-11-22
  • 打赏
  • 举报
回复
给一个能运行,用的上这个东西的例子,我没有那么多时间看啊。
我得从例子上面直接找你的问题所在,好调试
羊城咩咩咩 2004-11-22
  • 打赏
  • 举报
回复
顶!!!!!!!!!!!!!!!!!!!!!
yiqiangyang 2004-11-22
  • 打赏
  • 举报
回复
自己再顶一下!
csforgood 2004-11-22
  • 打赏
  • 举报
回复
虽然看的一点没头绪但还是帮你顶~
JavaAmateur 2004-11-22
  • 打赏
  • 举报
回复
up *_*
yiqiangyang 2004-11-22
  • 打赏
  • 举报
回复
public void setColorEditor(JTable table,TableColumn columnClass){
TableCellEditor cellEditor = new ColorEditor();
columnClass.setCellEditor(cellEditor);
ColorRenderer renderer = new ColorRenderer(true);
columnClass.setCellRenderer(renderer);
}
这个是我调用的函数,供大家参考。
cyicecream 2004-11-22
  • 打赏
  • 举报
回复
给出你的用例
wandou999 2004-11-22
  • 打赏
  • 举报
回复
uuuuuuuup*_*
jFresH_MaN 2004-11-22
  • 打赏
  • 举报
回复
哇,渲染?不太会啊
帮顶吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧