设置JTable中列图像按钮,但动态地改变特定的行/列的图像,而不是在
的所有行我试图做到以下几点:设置JTable中列图像按钮,但动态地改变特定的行/列的图像,而不是在
将所有细胞JTable中列图像按钮1。当另一行/列中的单元格更改为特定值时,图像按钮1只需在该行中更改为image2。如果删除行,则需要相应更新列映像,因为显示的行是从实际的defaulttablemodel中过滤的。
我做了大量的研究并尝试了不同的东西。我是一个java新手,不擅长表格单元渲染器/编辑器,这就是为什么我有这么多麻烦。
目前发生的事情是,改变按钮的功能被调用,但只有当我点击按钮时才会隐藏,而其他图像甚至不会显示出来。
我的程序执行此操作: btnRenderer = new ButtonRenderer(); btnRenderer.setButtonImagesURL(codebase.toString());
btnEditor = new ButtonEditor(table);
btnEditor.setButtonImagesURL(codebase.toString());
table.getColumnModel().getColumn(5).setCellRenderer(btnRenderer);
table.getColumnModel().getColumn(5).setCellEditor(btnEditor);
而且该特定的行/列值变化时,它调用: this.btnEditor.changeButtonToComplete(); 因为我无法弄清楚如何让表听取更改并更改按钮。
以下是我的渲染器/编辑器类。
class ButtonRenderer extends ButtonsPanel implements TableCellRenderer {
private boolean changeIconComplete = false;
ImageIcon imageComplete;
public ButtonRenderer() {
super();
}
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setBackground(table.getBackground());
this.setAlignmentX(Component.CENTER_ALIGNMENT);
this.setAlignmentY(Component.CENTER_ALIGNMENT);
setToolTipText("Abort upload");
return this;
}
public void setButtonImagesURL(String codebase)
{
setButtonImages(codebase);
URL imgURL = null;
try {
imgURL = new URL(codebase + "/img/uploadComplete.png");
} catch (MalformedURLException e) {
e.printStackTrace();
}
imageComplete = new ImageIcon(imgURL);
}
}
public class ButtonEditor extends ButtonsPanel implements TableCellEditor {
Service cloudService;
FileUploader fileUP;
DefaultTableModel model;
int currRow;
int currentlyUploadingRow;
public ButtonEditor(final JTable table) {
super();
MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
}
};
buttons.get(0).addMouseListener(ml);
buttons.get(0).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currRow == currentlyUploadingRow) {
// Cancel current uploading
fileUP.setCancelled();
cloudService.setCancelled();
} else {
// Set status to abort
if (model.getRowCount() == 0 || currRow == -1)
return;
if (model.getValueAt(currRow, 5).toString().contentEquals("Queued") &&
(Boolean)model.getValueAt(currRow, 7) == false) {
model.setValueAt("Aborted", currRow, 5);
}
}
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
}
});
}
public void setButtonImagesURL(String codebase)
{
setButtonImages(codebase);
createCompletedButton(codebase);
}
public void changeButtonToComplete()
{
changeAbortToComplete();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
//Get table model equivalent of row
String filename = table.getValueAt(row, 1).toString() + table.getValueAt(row, 0).toString();
this.currRow = getRowIndexByFilename(filename);
this.setBackground(table.getBackground());
return this;
}
private int getRowIndexByFilename(String filename)
{
String name;
for (int row = 0; row < model.getRowCount(); row++) {
name = model.getValueAt(row, 1).toString() + model.getValueAt(row, 0).toString();
if (filename.contentEquals(name) &&
!model.getValueAt(row, 5).toString().contentEquals("Complete") &&
!model.getValueAt(row, 5).toString().contains("Invalid") &&
!model.getValueAt(row, 5).toString().contains("Error") &&
!model.getValueAt(row, 5).toString().contains("Abort"))
return row;
}
return -1;
}
private int getRowIndexByFilenameComplete(String filename)
{
String name;
for (int row = 0; row < model.getRowCount(); row++) {
name = model.getValueAt(row, 1).toString() + model.getValueAt(row, 0).toString();
if (filename.contentEquals(name) &&
model.getValueAt(row, 5).toString().contentEquals("Complete") ||
model.getValueAt(row, 5).toString().contains("Invalid") ||
model.getValueAt(row, 5).toString().contains("Error") ||
model.getValueAt(row, 5).toString().contains("Abort"))
return row;
}
return -1;
}
// @Override
public Object getCellEditorValue() {
return "";
}
@Override
public boolean isCellEditable(java.util.EventObject e) {
return true;
}
}
public class ButtonsPanel extends JPanel {
public final java.util.List<JButton> buttons = Arrays.asList(new JButton(), new JButton());
public final JButton btnComplete = new JButton();
private ImageIcon[] images = new ImageIcon[2];
public ButtonsPanel() {
super();
setOpaque(false);
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.setBorder(new javax.swing.border.EmptyBorder(-5, -5, -5, -5));
}
public void setButtonImages(String codebase)
{
java.util.List<String> imgPaths = Arrays.asList("abortUpload.png", "/img/uploadComplete.png");
URL imgURL = null;
int ii = 0;
for (String path: imgPaths) {
try {
imgURL = new URL(codebase + "/img/" + path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
images[ii] = new ImageIcon(imgURL);
images[ii].setDescription(path);
ii++;
}
ii = 0;
for (JButton b: buttons) {
b.setFocusable(false);
b.setRolloverEnabled(false);
b.setIcon(images[ii]);
b.setBorderPainted(false);
b.setAlignmentX(Component.CENTER_ALIGNMENT);
b.setMargin(new Insets(0,0,0,0));
add(b);
if (ii == 1)
this.getComponent(1).hide();
ii++;
}
}
public void changeAbortToComplete()
{
this.getComponent(0).hide();
this.getComponent(1).show();
}
}
我也许误解了你的问题。但似乎更容易依赖已建立的table-model-renderer概念,而不是手动跟踪更改和调用更新。
尝试管理模型中的状态。设计一个渲染器来检查对象的状态并采取相应的行动。然后,由于模型发生变化,表格会被通知并重新绘制。渲染器被触发以呈现更新的数据。
@Andrew Thomson - 没有发布sscce(因为我的代码可能非常不正确),我将如何执行以下操作:创建一个带有buttonX的列的jtable。然后,当特定行中的另一列获得特定值时,按钮x将变为按钮y,但其他行将保持原有状态。在以下示例中,所有以ButtonX开头的行,但当Progress从上传更改为完成时,ButtonX变为ButtonY。行名称进度中止/完成 0文件1完成按钮Y 1文件2完成按钮Y 2文件3上载ButtonX –
为了更好地提供帮助,请发布[SSCCE](http://sscce.org/)。 –