Code
class ButtonsPanel extends JPanel {
public final List buttons =
Arrays.asList(new JButton("view"), new JButton("edit"));
public ButtonsPanel() {
super();
setOpaque(true);
for(JButton b: buttons) {
b.setFocusable(false);
b.setRolloverEnabled(false);
add(b);
}
}
}
class ButtonsRenderer extends ButtonsPanel
implements TableCellRenderer {
public ButtonsRenderer() {
super();
setName("Table.cellRenderer");
}
@Override public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setBackground(isSelected?table.getSelectionBackground():table.getBackground());
return this;
}
}
class ButtonsEditor extends ButtonsPanel
implements TableCellEditor {
public ButtonsEditor(final JTable table) {
super();
//---->
//DEBUG: view button click -> control key down + edit button(same cell) press
// -> remain selection color
MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
ButtonModel m = ((JButton)e.getSource()).getModel();
if(m.isPressed() && table.isRowSelected(table.getEditingRow())
&& e.isControlDown()) {
setBackground(table.getBackground());
}
}
};
buttons.get(0).addMouseListener(ml);
buttons.get(1).addMouseListener(ml);
//<----
buttons.get(0).addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
fireEditingStopped();
JOptionPane.showMessageDialog(table, "Viewing");
}
});
buttons.get(1).addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
int row = table.convertRowIndexToModel(table.getEditingRow());
Object o = table.getModel().getValueAt(row, 0);
fireEditingStopped();
JOptionPane.showMessageDialog(table, "Editing: "+o);
}
});
addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
fireEditingStopped();
}
});
}
@Override public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
this.setBackground(table.getSelectionBackground());
return this;
}
@Override public Object getCellEditorValue() {
return "";
}
//Copid from AbstractCellEditor
//protected EventListenerList listenerList = new EventListenerList();
transient protected ChangeEvent changeEvent = null;
@Override public boolean isCellEditable(java.util.EventObject e) {
return true;
}
//......
References
Hey, really cool Swing blog. I put a link to you in my website www.blue-walrus.com . Would appreciate if you did the same... trying to get a link 'ring' of Swing blogs.
ReplyDeleteOlly
Hi, redmonkey, sorry for this reply to be late.
ReplyDeleteI add "Link ring of Swing blogs" on side bar.
Thank you.
Me encanta tu blog, realmente me ha servido bastante
ReplyDeleteMe alegro de que le ayude :)
DeleteThis comment has been removed by the author.
ReplyDeleteHi, Adhisheshu
DeleteYou can download all the source code from the link in the screenshot above.
Actually I am doing Desktop application not web application I want insert three buttons in single column of table when click button1 it will do some actions like that
DeleteYes, you can download the source code(src.zip) of the Desktop(JFrame) application.
DeleteI want insert multiple icons in the column cell in Jtable with one by one
ReplyDeleteDo you mean something like this?
DeleteShow JToolTip for Icons placed in the cell of the JTable