import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.table.*;
public class TableDemo extends JFrame {
private final HashMap<String, Color> colorMap;
private final JTable table;
private final JDialog dialog;
private final JToolBar toolbar;
private final JButton btColor, btOK;
private final JPanel mainpanel;
private final JTextField textfield;
public TableDemo() {
dialog = new JDialog(this);
toolbar = new JToolBar();
btColor = new JButton("Color...");
mainpanel = new JPanel();
textfield = new JTextField();
btOK = new JButton("OK");
table = new JTable() {
@Override
public Component prepareRenderer(final TableCellRenderer renderer,
final int row, final int column) {
Component c = super.prepareRenderer(renderer, row, column);
int rowM = convertRowIndexToModel(row);
int columnM = convertColumnIndexToModel(column);
String key = rowM + "," + columnM;
Color color = colorMap.get(key);
c.setBackground(color);
return c;
}
};
dialog.setModal(true);
dialog.setResizable(false);
toolbar.setRollover(true);
btColor.setFocusable(false);
btColor.setHorizontalTextPosition(SwingConstants.CENTER);
btColor.setVerticalTextPosition(SwingConstants.BOTTOM);
btColor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent evt) {
Color color = JColorChooser.showDialog(TableDemo.this, "Color Chooser",
Color.WHITE);
if (color == null) {
color = Color.WHITE;
}
int selectedRow = table.getSelectedRow();
int selectedColumn = table.getSelectedColumn();
String key = table.convertRowIndexToModel(selectedRow) + ","
+ table.convertColumnIndexToModel(selectedColumn);
colorMap.put(key, color);
textfield.setBackground(color);
}
});
toolbar.add(btColor);
dialog.getContentPane().add(toolbar, BorderLayout.PAGE_START);
textfield.setPreferredSize(new Dimension(200, 20));
mainpanel.add(textfield);
btOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent evt) {
dialog.setVisible(false);
int selectedColumn = table.getSelectedColumn();
int selectedRow = table.getSelectedRow();
table.setValueAt(textfield.getText(), selectedRow, selectedColumn);
}
});
mainpanel.add(btOK);
dialog.getContentPane().add(mainpanel, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
table.setModel(new DefaultTableModel(
new Object[][]{
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String[]{
"Title 1", "Title 2", "Title 3", "Title 4"
}) {
Class[] types = new Class[]{
String.class, String.class, String.class, String.class
};
boolean[] canEdit = new boolean[]{
false, false, false, false
};
@Override
public Class getColumnClass(final int columnIndex) {
return types[columnIndex];
}
@Override
public boolean isCellEditable(final int rowIndex,
final int columnIndex) {
return canEdit[columnIndex];
}
});
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(final MouseEvent evt) {
boolean rightMouseButton = SwingUtilities.isRightMouseButton(evt);
if (rightMouseButton) {
Point point = evt.getPoint();
int rowAtPoint = table.rowAtPoint(point);
int columnAtPoint = table.columnAtPoint(point);
table.changeSelection(rowAtPoint, columnAtPoint, false, false);
dialog.setLocationRelativeTo(TableDemo.this);
textfield.setText("");
textfield.setBackground(Color.WHITE);
Object value = table.getValueAt(rowAtPoint, columnAtPoint);
if (value != null) {
textfield.setText(value.toString());
}
String key = table.convertRowIndexToModel(rowAtPoint) + ","
+ table.convertColumnIndexToModel(columnAtPoint);
Color color = colorMap.get(key);
if (color != null) {
textfield.setBackground(color);
}
dialog.setVisible(true);
}
}
});
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
pack();
dialog.pack();
colorMap = new HashMap<String, Color>();
}
public static void main(final String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TableDemo().setVisible(true);
}
});
}
}