Reputation: 926
I have a table with a tablemodel containg some names and a CellRenderer which extends a Panel I made which contains a JButton. Everythign works great except when I click on the button the values in the Cell disappear as long as I hold the JButton clicked!? bellow the Panel and the Renderer. Any ideas?
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);
}
}
}
Thanks for any ideas.
Upvotes: 0
Views: 1323
Reputation: 4816
Your renderer is a whole component that has a button and everything. I believe this is not the correct way to implement a renderer. This behaviour needs to be simulated e.g.
MouseListener
find where the user clicked and what row needs to be changedYou use some imports from the gui
package that prevents the code to compile and run so my best guess here would be that the button messes with the rendering while is being pressed -perhaps when trying to draw its pressed state?
Upvotes: 0
Reputation: 109813
don't to set/modify any JComponent
, its value inside XxxRenderer
, don't to change state of value (came from XxxTableModel
)
XxxRenderer
is only decorator not value generator,
for cell value (in XxxRenderer
) is resposible value stored in XxxTableModel
, to set this value in XxxTableModel
,
don't store JComponents
in XxxTableModel
, there is stored only its rendering value
Upvotes: 2