الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيفية إدخال colorChooser في خلية jtable

بدأه mohammeding في 17 مارس 2011 · 4 رد · 377 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

لدي سؤال عن كيفية إدخال 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;
    }
}
#3

نعم يأخي تتبعت ذلك ولكم الكود لم يفلح معي لا أعرف لماذا

وبأخص هذا

table.setDefaultRenderer(Color.class,
                                 new ColorRenderer(true));
        table.setDefaultEditor(Color.class,
                               new ColorEditor());
#4

تفضل

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;


public class table_place {

	DefaultTableModel model;
	public JComboBox comboBox;
	public JTable table;
	public DefaultCellEditor dce1;
	public 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.getColumnModel().getColumn(column).setCellRenderer(new ColorRenderer(true));
//                	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(Color.red);
    	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);
        	}
    	}
    	return this;
	}
}

Software Developer
Mahmoudkelany.com


 

#5

بارك الله فيك أخ محمود

حل المشكل

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…