gpt4 book ai didi

java - 单击按钮时 JTable 单元格发生变化?

转载 作者:行者123 更新时间:2023-12-02 06:53:16 24 4
gpt4 key购买 nike

我有一个表,其中包含一些名称的表模型和一个 CellRenderer,它扩展了我制作的包含 JButton 的面板。一切都很好,除了当我单击按钮时,只要我按住 JButton 单击,单元格中的值就会消失!?在面板和渲染器下面。有什么想法吗?

        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);


}
}

}

感谢您的任何想法。

最佳答案

  • 不要设置/修改任何JComponent,其值在XxxRenderer内,不要更改值的状态(来自XxxTableModel )

  • XxxRenderer 只是装饰器而不是值生成器,

  • 单元格值(在XxxRenderer中)是存储在XxxTableModel中的可靠值,要在XxxTableModel中设置此值,

  • 不要将JComponents存储在XxxTableModel中,仅存储其渲染值

关于java - 单击按钮时 JTable 单元格发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17745500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com