السلام عليكم
لدي سؤال عن كيفية إدخال colorChooser في خلية محددة قمت بكتابة كود ولكن لم يفعل شئ
public class table_place
{
DefaultTableModel model;
public static JComboBox comboBox;
public static JTable table;
public static DefaultCellEditor dce1;
public static TableCellEditor editor1;
table_place(JTable jtable)
{
this.table=jtable;
Object[][] data = { { "name", "", },{ "Color Set", new Color(51, 51, 153), }};//,{ "X", "", },{ "Y", "", },{ "Colore", "", },{ "Capacité", "", } };
String[] columnNames = { "Propriétés", "Valeur"};
ListSelectionModel selectionModel = new DefaultListSelectionModel();
selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
comboBox = new JComboBox();
comboBox.addItem("Colset");
comboBox.addItem("Variable");
dce1 = new DefaultCellEditor( comboBox );
editor1 =table.getCellEditor();
table.setModel(new DefaultTableModel(data, columnNames){
@Override
public boolean isCellEditable(int row, int column)
{
if (column == 1 && row != 1)
{
table.getColumnModel().getColumn(column).setCellEditor(editor1);
return true;
}
else if (column == 1 && row == 1)
{
//table.getColumnModel().getColumn(column).setCellEditor(new DefaultCellEditor(comboBox));
// table.getColumnModel().getColumn(column).setCellEditor(editor1);
table.setDefaultRenderer(Color.class,new ColorRenderer(true));
table.setDefaultEditor(Color.class,new ColorEditor());
return true;
}
else
{
return false;
}
}
});
}
table_place()
{
}
public void charger_combobox(String new_item[])
{
int i=0;
comboBox.removeAllItems();
for (i=0;i<new_item.length;i++)
{
comboBox.addItem(new_item);
// System.out.println("new_item["+i+"] :"+new_item);
}
}
}
class ColorEditor extends AbstractCellEditor implements TableCellEditor,ActionListener {
Color currentColor;
JButton button;
JColorChooser colorChooser;
JDialog dialog;
protected static final String EDIT = "edit";
public ColorEditor() {
//Set up the editor (from the table's point of view),
//which is a button.
//This button brings up the color chooser dialog,
//which is the editor from the user's point of view.
button = new JButton();
button.setActionCommand(EDIT);
button.addActionListener(this);
button.setBorderPainted(false);
//Set up the dialog that the button brings up.
colorChooser = new JColorChooser();
dialog = JColorChooser.createDialog(button,
"Pick a Color",
true, //modal
colorChooser,
this, //OK button handler
null); //no CANCEL button handler
}
/**
* Handles events from the editor button and from
* the dialog's OK button.
*/
public void actionPerformed(ActionEvent e) {
if (EDIT.equals(e.getActionCommand())) {
//The user has clicked the cell, so
//bring up the dialog.
button.setBackground(currentColor);
colorChooser.setColor(currentColor);
dialog.setVisible(true);
//Make the renderer reappear.
fireEditingStopped();
} else { //User pressed dialog's "OK" button.
currentColor = colorChooser.getColor();
}
}
//Implement the one CellEditor method that AbstractCellEditor doesn't.
public Object getCellEditorValue() {
return currentColor;
}
//Implement the one method defined by TableCellEditor.
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column) {
currentColor = (Color)value;
return button;
}
}
class ColorRenderer extends JLabel
implements TableCellRenderer {
Border unselectedBorder = null;
Border selectedBorder = null;
boolean isBordered = true;
public ColorRenderer(boolean isBordered) {
this.isBordered = isBordered;
setOpaque(true); //MUST do this for background to show up.
}
public Component getTableCellRendererComponent(
JTable table, Object color,
boolean isSelected, boolean hasFocus,
int row, int column) {
Color newColor = (Color)color;
setBackground(newColor);
if (isBordered) {
if (isSelected) {
if (selectedBorder == null) {
selectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
table.getSelectionBackground());
}
setBorder(selectedBorder);
} else {
if (unselectedBorder == null) {
unselectedBorder = BorderFactory.createMatteBorder(2,5,2,5,
table.getBackground());
}
setBorder(unselectedBorder);
}
}
setToolTipText("RGB value: " + newColor.getRed() + ", "
+ newColor.getGreen() + ", "
+ newColor.getBlue());
return this;
}
}