package table;
/*
* TableLayoutTesting3.java
*/
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 TableLayoutTesting3 extends JFrame {
private DefaultTableModel model;
private JTable table;
public TableLayoutTesting3() {
super("TableLayoutTesting3");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(600, 600);
setLocationRelativeTo(null);
table = new JTable(0, 2) {
@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 JComponentTableCellRenderer());
table.setDefaultEditor(JComponent.class, new JComponentTableCellEditor());
table.setSurrendersFocusOnKeystroke(true);
table.setRowHeight(27);
table.setTableHeader(null);
model = (DefaultTableModel) table.getModel();
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
for (int i = 0; i < 5; i++) {
model.addRow(new JPanel[]{new MyPanel(), new MyPanel()});
}
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TableLayoutTesting3().setVisible(true);
}
});
}
class MyPanel extends JPanel {
public MyPanel() {
setLayout(new BorderLayout());
final JProgressBar tf = new JProgressBar();
tf.setPreferredSize(new Dimension(150, 20));
JButton text = new JButton(" Value ");
text.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
String desc = JOptionPane.showInputDialog("Old value =" +
tf.getValue() + ", type a new value:");
try {
tf.setValue(Integer.parseInt(desc));
} catch (NumberFormatException e1) {
}
tf.requestFocusInWindow();
}
});
add(tf, BorderLayout.CENTER);
add(text, BorderLayout.EAST);
setBorder(BorderFactory.createEmptyBorder());
tf.setBorder(BorderFactory.createEmptyBorder());
text.setBorder(BorderFactory.createEmptyBorder());
}
}
}
class JComponentTableCellRenderer implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
return (JComponent) value;
}
}
class JComponentTableCellEditor implements TableCellEditor, Serializable {
protected EventListenerList listenerList = new EventListenerList();
protected transient ChangeEvent changeEvent = null;
protected JComponent editorComponent = null;
public Component getComponent() {
return editorComponent;
}
/* implement CellEditor:
getCellEditorValue
isCellEditable
shouldSelectCell
stopCellEditing
cancelCellEditing
addCellEditorListener
removeCellEditorListener
*/
@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() {//used in stopCellEditing
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() {//used in cancelCellEditing
// 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);
}
}
}
// implement 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;
}
}