我有一个带有tablemodel的表,其中包含一些名称和一个CellRenderer,它扩展了我制作的包含JButton的Panel。 Everythign效果很好,除非我点击按钮,只要我按住JButton点击,Cell中的值就会消失!?小组和渲染器。有什么想法吗?
package gui.table;
import event.TestController;
import gui.DaimlerColor;
import gui.MainWindow;
import java.awt.Component;
import java.util.LinkedList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import org.jdom2.Document;
import org.jdom2.Element;
@SuppressWarnings("serial")
public class TestcaseCellRenderer extends TestcasePanel implements TableCellRenderer {
List<String> data;
MainWindow gui = null;
TestController tgc = null;
public TestcaseCellRenderer() {
super();
setName("Table.cellRenderer");
}
public TestcaseCellRenderer(List<String> names, MainWindow gui2, TestController tgc) {
super();
this.data = names;
this.gui = gui2;
this.tgc = tgc;
setName("Table.cellRenderer");
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
//this.setBackground(isSelected ? table.getSelectionBackground() : table
// .getBackground());
this.setOpaque(true);
this.setToolTipText("Click play to rerun this Testcase");
this.setName(data.get(row));
//set Button Image if passed
int passed = 0;
setPlaybuttonIcon(gui.getTestcasetableModelIcon(row));
setClickable(tgc.clickable);
if (row <= gui.currentTestcase) {
Document doc = tgc.getDoc();
List<Element> cases = doc.getRootElement().getChildren();
List<Element> teststeps = cases.get(row).getChildren();
List<String> attributes = new LinkedList();
for (Element teststep : teststeps) {
if (teststep.getAttribute("status") != null) {
attributes.add(teststep.getAttributeValue("status"));
}
else{
attributes.add("empty");
}
}
if(attributes.contains("empty")){
setButton("");
}
else if (attributes.contains("failed")){
setButton("failed");
}
else{
setButton("passed");
}
} else {
setButton("");
}
this.setBorder(BorderFactory.createEmptyBorder());
this.setBackground(isSelected ? DaimlerColor.LIGHT_BLUE : (row % 2 == 1 ? DaimlerColor.DARK_WHITE : DaimlerColor.WHITE));
return this;
}
}
package gui.table;
import gui.MainWindow;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class TestcasePanel extends JPanel {
public JButton playButton = new JButton();// new JButton("Play");
//
String name = "-";
JLabel nameLabel = new JLabel(name);
JLabel statusIconLabel = new JLabel();
public MainWindow gui;
public void setName(String name) {
this.name = name;
nameLabel.setText(name);
}
public void setClickable(boolean b){
playButton.setEnabled(b);
}
public void setPlaybuttonIcon(String s){
ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "\\ressources\\playbutton.png");
Image img = icon.getImage();
Image scaledImg = img.getScaledInstance(16, 16, java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(scaledImg);
ImageIcon icon2 = new ImageIcon(System.getProperty("user.dir") + "\\ressources\\pausebutton.png");
Image img2 = icon2.getImage();
Image scaledImg2 = img2.getScaledInstance(16, 16, java.awt.Image.SCALE_SMOOTH);
icon2 = new ImageIcon(scaledImg2);
if(s.equals("play")){
playButton.setIcon(icon);
}else{
playButton.setIcon(icon2);
}
}
public void setButton(String s) {
if (s.equals("passed")) {
ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "\\ressources\\check.png");
Image img = icon.getImage();
icon = new ImageIcon(img);
statusIconLabel.setIcon(icon);
statusIconLabel.setOpaque(false);
} else if (s.equals("failed")) {
ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "\\ressources\\fail.png");
Image img = icon.getImage();
icon = new ImageIcon(img);
statusIconLabel.setIcon(icon);
statusIconLabel.setOpaque(false);
} else {
ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "\\ressources\\help.png");
Image img = icon.getImage();
icon = new ImageIcon(img);
statusIconLabel.setIcon(icon);
statusIconLabel.setOpaque(false);
}
}
public TestcasePanel() {
super();
setOpaque(true);
playButton.setFocusable(false);
playButton.setRolloverEnabled(false);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
this.setLayout(gbl);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0;
gbc.insets = new Insets(3, 3, 3, 3);
gbc.anchor = GridBagConstraints.WEST;
gbl.setConstraints(statusIconLabel, gbc);
this.add(statusIconLabel);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 0.8;
gbl.setConstraints(nameLabel, gbc);
this.add(nameLabel);
ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "\\ressources\\playbutton.png");
Image img = icon.getImage();
Image scaledImg = img.getScaledInstance(Integer.valueOf((int) this.getPreferredSize().getHeight()).intValue(),
Integer.valueOf((int) this.getPreferredSize().getHeight()).intValue(), java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(scaledImg);
playButton.setName("play");
playButton.setIcon(icon);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 0;
gbl.setConstraints(playButton, gbc);
this.add(playButton);
}
public void setButtonIcon(String name){
if(name.equals("play")){
ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "\\ressources\\playbutton.png");
Image img = icon.getImage();
Image scaledImg = img.getScaledInstance(16,16, java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(scaledImg);
playButton.setIcon(icon);
}else if (name.equals("pause")) {
ImageIcon icon = new ImageIcon(System.getProperty("user.dir") + "\\ressources\\pausebutton.png");
Image img = icon.getImage();
Image scaledImg = img.getScaledInstance(16,16, java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(scaledImg);
playButton.setIcon(icon);
}
}
}
感谢任何想法。
答案 0 :(得分:2)
不要设置/修改任何JComponent
,其值在XxxRenderer
内,不要更改值的状态(来自XxxTableModel
)
XxxRenderer
只是装饰者而不是值生成器,
单元格值(在XxxRenderer
中)是存储在XxxTableModel
中的可保留值,以便在XxxTableModel
中设置此值,
不要将JComponents
存储在XxxTableModel
中,只存储其渲染值
答案 1 :(得分:0)
您的渲染器是一个包含按钮和所有内容的整个组件。我相信这不是实现渲染器的正确方法。需要模拟此行为,例如
MouseListener
中查找用户点击的位置以及需要更改的行你使用gui
包中的一些导入来防止代码编译和运行所以我最好的猜测是按钮在按下时会混淆渲染 - 或者在尝试绘制其按下的状态时?