将jtable列设置为图像按钮,但动态更改特定行/列上的图像,而不是列中的所有行

时间:2012-05-18 19:00:25

标签: java swing jtable

我正在尝试执行以下操作:

将jtable列中的所有单元格设置为图像button1。当另一行/列中的单元格更改为特定值时,图像按钮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();

}

}

1 个答案:

答案 0 :(得分:0)

我可能会误解你的问题。但似乎更容易依赖表格模型渲染器的既定概念,而不是手动跟踪更改和调用更新。

尝试管理模型中的状态。设计渲染器以检查对象的状态并采取相应的行为。然后,作为模型更改的结果,表格被通知并重新绘制。触发渲染器以显示更新的数据。