Question

I use a MouseListner in a DefaultTableCellRenderer.

A Label shall get this one ==========================>

public class CatCustTabRenderer extends DefaultTableCellRenderer implements
    TableCellRenderer {

Icon iconGetDate = new ImageIcon(GUI_RentABook.class.getResource("/images/edit15.jpg"));    
JButton button = new JButton(iconGetDate);

// setOpaque(true);
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {

    JTableHeader header = table.getTableHeader();
    header.setBackground(OwnConstants.SALTEGREY);
    header.setForeground(Color.white);



    table.getColumnModel().getColumn(0).setPreferredWidth(25);// 0 Numbers
    table.getColumnModel().getColumn(1).setPreferredWidth(60);// 1 Cust-ID
    table.getColumnModel().getColumn(2).setPreferredWidth(160);// 2 Customer
    table.getColumnModel().getColumn(3).setPreferredWidth(50);// 3 ZIP
    table.getColumnModel().getColumn(4).setPreferredWidth(100);// 4 City
    table.getColumnModel().getColumn(5).setPreferredWidth(100);// 5 Street
    table.getColumnModel().getColumn(6).setPreferredWidth(40);// 6 Str-#
    table.getColumnModel().getColumn(7).setPreferredWidth(115);// 7 Phone
    table.getColumnModel().getColumn(8).setPreferredWidth(170);// 8 Email
    table.getColumnModel().getColumn(9).setPreferredWidth(110);// 9 Loan-ID
    table.getColumnModel().getColumn(10).setPreferredWidth(16);// 10 Edit-Icon


    JLabel label = (JLabel) super.getTableCellRendererComponent(table,
            value, isSelected, hasFocus, row, column);  



String returnTTipp = "";
   returnTTipp = "Edit customer values";

    if (value instanceof ImageIcon)              
    {   label.setText(null);
        label.setIcon((ImageIcon)value);        
        label.setToolTipText(returnTTipp); 
        System.out.println("Im RENDERER");
        label.addMouseListener(new MouseAdapter() {
              public void mouseClicked(MouseEvent me) {
                  System.out.println("CLICKED");
                }
              });
    }else {
        label.setIcon(null);
        label.setToolTipText(null); 
    } 

    if ((row % 2) == 1 && !isSelected) {
        label.setBackground(OwnConstants.NAVAJOWHITE);
    } else if ((row % 2) == 0 && !isSelected) {
        label.setBackground(Color.WHITE);
    }


    return label;
}

}

This Label is part of a table, which is implemented in a GUI-class. The table [tableCatCustom] use also a MouseListener. When I click on a row, I get the values of it. BUT, on row Position 10 the Label(icon) is located. The Icon should have the MouseListener, which I had implemented (see above).

Here the MouseListener of the table ================>

tableCatCustom.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent ev) {
    if (ev.getClickCount() == 1) {

        JTable temp = (JTable) ev.getSource();
        int rowPos = temp.getSelectedRow();
        Object[] rowTemp = new Object[temp.getColumnCount()];

        for (int i = 0; i < rowTemp.length; i++) {

            txtCustID.setText((String) temp.getValueAt(rowPos, 1));
            txtCustomer.setText((String) temp.getValueAt(rowPos, 2));


            // Set LoanID
            if ((String) temp.getValueAt(rowPos, 1) != null)
            {
                loanIDCust = (String) temp.getValueAt(rowPos, 1);
                setLoanID();

            }else{
                lblCatMess.setForeground(Color.RED);
                lblCatMess.setText("No CustomerID found!");
            }

        }
    } else {
        System.out.println("No row is selected correctly!");
    }
}

});

When I click on a row, this MouseListener works well. But in the row is should also be the MouseListener of the Label (rowpos 10 = icon). This MouseListener does not work!!!

Where is my mistake?

Best regards

Wolfgang

Was it helpful?

Solution

A renderer is NOT a real component. The MouseListener will not work on the renderer.

If you want to do processing on a mouse click then you need to add the MouseListener to the JTable. There are methods in the JTable API that will allow you to convert the mouse point to a specific cell in the table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top