package table;
/*
* TableButtons_1.java
* Author: Zafir Anjum / André Uhres
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableButtons_1 extends JFrame {
private JTable table;
private String[] columnNames = {"title 1", "title 2", "title 3", "title 4", "title 5", "title 6", "title 7"};
private DefaultTableModel model;
private int id;
public TableButtons_1() {
super("Table Buttons");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
model = new DefaultTableModel(createDataArray(), columnNames);
table = new JTable(model) {
@Override
public TableCellRenderer getCellRenderer(final int row, final int column) {
TableColumn tableColumn = getColumnModel().getColumn(column);
TableCellRenderer renderer = tableColumn.getCellRenderer();
if (renderer == null) {
Class c = getColumnClass(column);
if (c.equals(Object.class)) {
Object o = getValueAt(row, column);
if (o != null) {
c = getValueAt(row, column).getClass();
}
}
renderer = getDefaultRenderer(c);
}
return renderer;
}
@Override
public TableCellEditor getCellEditor(final int row, final int column) {
TableColumn tableColumn = getColumnModel().getColumn(column);
TableCellEditor editor = tableColumn.getCellEditor();
if (editor == null) {
Class c = getColumnClass(column);
if (c.equals(Object.class)) {
Object o = getValueAt(row, column);
if (o != null) {
c = getValueAt(row, column).getClass();
}
}
editor = getDefaultEditor(c);
}
return editor;
}
};
table.setDefaultRenderer(JComponent.class, new JComponentCellRenderer_1());
table.setDefaultEditor(JComponent.class, new JComponentCellEditor_1());
table.setRowHeight(25);
add(new JScrollPane(table));
}
private Object[][] createDataArray() {
Object[][] data = new Object[10][];
for (int row = 0; row < data.length; row++) {
Object[] rowData = new Object[7];
// Buttons
JButton btn1 = getButton();
JComboBox btn2 = getCombo(row);
rowData[0] = "" + id++;
rowData[1] = "";
rowData[2] = "";
rowData[3] = "";
rowData[4] = btn2;
rowData[5] = "";
rowData[6] = btn1;
data[row] = rowData;
}
return data;
}
private void neueZeile(final int row) {
Vector<Object> rowData = new Vector<Object>();
// Buttons
JButton btn1 = getButton();
JComboBox btn2 = getCombo(row);
rowData.add("" + id++);
rowData.add("");
rowData.add("");
rowData.add("");
rowData.add(btn2);
rowData.add("");
rowData.add(btn1);
model.insertRow(row, rowData);
}
private JButton getButton() {
JButton btn1 = new JButton("Neue Zeile");
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
table.getCellEditor().stopCellEditing();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
neueZeile(table.getSelectedRow() + 1);
}
});
}
});
return btn1;
}
private JComboBox getCombo(int row) {
String[] items = {"Item "+row+"_1", "Item "+row+"_2", "Item "+row+"_3"};
final JComboBox btn2 = new JComboBox(items);
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
table.getCellEditor().stopCellEditing();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
System.out.println(btn2.getSelectedItem() + " row: " + table.getSelectedRow());
}
});
}
});
return btn2;
}
public static void main(final String[] args) {
new TableButtons_1().setVisible(true);
}
}
class JComponentCellRenderer_1 implements TableCellRenderer {
private JLabel label = new JLabel();
@Override
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
// if(value instanceof JComboBox){
// label.setText(((JComboBox)value).getSelectedItem().toString());
// return label;
// }
return (JComponent) value;
}
}
class JComponentCellEditor_1 implements TableCellEditor, Serializable {
protected EventListenerList listenerList = new EventListenerList();
protected transient ChangeEvent changeEvent = null;
protected JComponent editorComponent = null;
public Component getComponent() {
return editorComponent;
}
@Override
public Object getCellEditorValue() {
return editorComponent;
}
@Override
public boolean isCellEditable(final EventObject anEvent) {
return true;
}
@Override
public boolean shouldSelectCell(final EventObject anEvent) {
if (editorComponent != null && anEvent instanceof MouseEvent && ((MouseEvent) anEvent).getID() == MouseEvent.MOUSE_PRESSED) {
Component dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent, 3, 3);
MouseEvent e = (MouseEvent) anEvent;
MouseEvent e2 = new MouseEvent(dispatchComponent, MouseEvent.MOUSE_RELEASED, e.getWhen() + 100000, e.getModifiers(), 3, 3, e.getClickCount(), e.isPopupTrigger());
dispatchComponent.dispatchEvent(e2);
e2 = new MouseEvent(dispatchComponent, MouseEvent.MOUSE_CLICKED, e.getWhen() + 100001, e.getModifiers(), 3, 3, 1, e.isPopupTrigger());
dispatchComponent.dispatchEvent(e2);
}
return true;
}
@Override
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
@Override
public void cancelCellEditing() {
fireEditingCanceled();
}
@Override
public void addCellEditorListener(final CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
@Override
public void removeCellEditorListener(final CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}
protected void fireEditingStopped() {
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener) listeners[i+1]).editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener) listeners[i+1]).editingCanceled(changeEvent);
}
}
}
// implements TableCellEditor
@Override
public Component getTableCellEditorComponent(final JTable table, final Object value, final boolean isSelected, final int row, final int column) {
editorComponent = (JComponent) value;
return editorComponent;
}
}