Discussion:
Why doesn't my TableCellRenderer react to MouseEvents
(too old to reply)
siva
2006-03-09 11:07:43 UTC
Permalink
Hi All

I have developed report king of screen in Swing .

A JTable contains only one column,for that i am applying Panel as a
render , over

the Panel ,i have Labels , clicking on that labels , i need
populate the one more screen

in renderer mouse click functionality not working , please help this
issue.
decalod85
2006-03-09 16:44:12 UTC
Permalink
Post by siva
Hi All
I have developed report king of screen in Swing .
A JTable contains only one column,for that i am applying Panel as a
render , over
the Panel ,i have Labels , clicking on that labels , i need
populate the one more screen
in renderer mouse click functionality not working , please help this
issue.
Putting a mouse click listener on a renderer will do nothing for you
since the renderer component is not actually on the screen. JTable
draws a picture of the renderer then pastes it into the correct cell.

You need to put the mouse click listener on the JTable instead.
JR
2006-03-09 19:20:09 UTC
Permalink
you probably want a button--> you need to use a button as the renderer,
and also use a button as an editor; then it will look pressed and
respond to clicks.

so, make a JTable extension and for that column return an Editor whose
getTableCellEditorComponent() method returns a button.

then, put an actionlistener on that button, and inside the
actionlistener call the getEditingrow() to find out which row of the
table the button is on, and then do the right thing:

btw- TestConnectionButton is a class that Extends JButton;which also
happens to implement the TableCellRenderer interface as well.

search this group for putting JButtons into tables; and you may want to
have each "label" be a button in it's own column; you can make the
table not draw its gridlines if you want.

hope this helps

-Jill

/***********************************************************************
*The ButtonEditor; this is what makes the Button be pressable
*and it has an actionlistener, so that when the button is pressed
*it executes the testConnection method on the correct row

***********************************************************************/
public class ButtonEditor extends AbstractCellEditor
implements TableCellEditor, ActionListener {
private TestConnectionButton m_editor;

public ButtonEditor()
{
m_editor = new TestConnectionButton();

m_editor.setContentAreaFilled(true);
m_editor.addActionListener(this);
}

public JButton editor(){
return m_editor;
}
public void actionPerformed(ActionEvent e){
Debug.Print("clicked the button: status: "+ m_editor.getText());
int row = getEditingRow();
Debug.Print("current row: "+row);
ConnectionTableModel ctm =
(ConnectionTableModel)getModel();
ConnectionDesc lineItem = ctm.getItemAt(row);
String details = lineItem.getDescription();
if(StringUtils.isBlank(details)){
//don't do the action; put in a msg to the user
MessageDialog.showMessage("Connection needs a value to be tested",
m_editor);
Debug.Print("\n\n********don't**********");
} else {
Debug.Print("got the ctm: "+ ctm);
if (!(lineItem.statusTestAble())){
MessageDialog.showMessage("Connection already Tested",m_editor);
} else {
boolean okay = ctm.testConnection(row);
m_editor.setLF(okay);
}
}
}
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean selected,
int row,
int column) {
ConnectionTableModel ctm = (ConnectionTableModel)table.getModel();
ConnectionDesc connection = ctm.getItemAt(row);

int status = connection.getStatus();
m_editor.setLF(status);
m_editor.setForeground(table.getForeground());
return m_editor;
}

public Object getCellEditorValue(){
return m_editor.getText();
}
} //end button Editor class

Loading...