Results 1 to 5 of 5
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default JCheckBox, ListCellRenderer and mousListener

    Hi I am trying to add mouseListener to ListCellRenderer, but seems that
    ListCellRenderer do not recognize mouseEvent. I think Im doing it far from the right way.

    I try to click on of the list but it does not change its state.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JListAndJCheckBox extends JFrame
    {	JList lists;
    	public JListAndJCheckBox()
    	{	Object[] listData = {"Hello", "Java", "World"};
    		lists = new JList(listData);
    		lists.setCellRenderer(new JListCheckBox());
    
    		this.setLayout(new BorderLayout());
    		this.add(lists, BorderLayout.CENTER);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setVisible(true);
    		pack();
    	}
    
    	public static void main(String[] args)
    	{	JListAndJCheckBox jlcb = new JListAndJCheckBox();
    	}
    
    	protected class JListCheckBox extends JCheckBox implements ListCellRenderer
    	{
        		int clicked = 0;
        		JCheckBox checkBox;
    
        		public Component getListCellRendererComponent(JList list, Object value,
                			int index, boolean isSelected, boolean cellHasFocus)
        		{
            			checkBox = new JCheckBox();
            			checkBox.setText(value.toString());
    
            			checkBox.addMouseListener(new MouseAdapter()
            			{   	public void mouseclicked(MouseEvent me)
                				{   	System.out.println("CLICKED");
                    				if(checkBox.isSelected())
                    				{   	checkBox.setBackground(getBackground());
                        					checkBox.setForeground(new Color(0, 0, 0));
                        					checkBox.setSelected(false);
                    				}
                    				else
                    				{   	checkBox.setBackground(new Color(50, 100, 150));
                        					checkBox.setForeground(new Color(255, 255, 255));
                        					checkBox.setSelected(true);
                    				}
                				}
                				public void mousepressed(MouseEvent me)
                				{   	System.out.println("PRESSED");
                    				if(checkBox.isSelected())
                    				{   	checkBox.setBackground(getBackground());
                        					checkBox.setForeground(new Color(0, 0, 0));
                        					checkBox.setSelected(false);
                    				}
                    				else
                    				{   	checkBox.setBackground(new Color(50, 100, 150));
                        					checkBox.setForeground(new Color(255, 255, 255));
                        					checkBox.setSelected(true);
                    				}
                				}
            			});
    
    
            			return checkBox;
        		}
    	}
    }
    Thanks in advance

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    MouseListener is for mouse position and click events in a window space - you generally add it to the JFrame or the JPanel in the JFrame. Are you just trying to listen for click events on the button? or click events in the window in general?

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Sorry if I reply just today, I got sick. Anyway, about your question, what I am trying to do is
    to add a mouselistener to each JListCheckbox in JList. JListCheckbox is a class that extends
    JCheckbox and implements ListCellRenderer. But it seems that JListCheckbox do not add mouseListener
    to itself.

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    There are no check boxes in a JList as JLists don't hold components but rather just render them. You're looking for editing capabilities which I believe JLists don't have (at least not in standard JLists). A JTable might work better since you can have both renderers and editors.

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    JList does not have cellEditor unlike JTables that's why eventhough I add mouselistener to JListCheckBox it does not recognize any mouse event.

    I even try to put this event in JList's mouse listener, and convert it to a JListCheckBox/JCheckBox
    Java Code:
    public class JListAndJCheckBox extends JFrame
    {   ...
        public class JListAndJCheckBox()
        {   ...
            Object[] listData = {"Hello", "Java", "World"}; //<-- I input string
    	lists = new JList(listData);
            lists.addMouseListener(new MouseAdapter()
            {   public void mouseClicked(MouseEvent evt)
                {   listsMouseClicked(evt);
                }
            });
            ...
        }
    
        private void listsMouseClicked(MouseEvent evt)
        {	int selIndex = lists.locationToIndex(evt.getPoint());
    
            [b]thats why lists.getModel().getElementAt(selIndex) return string and give me error
            //JListCheckBox selItem = (JListCheckBox) lists.getModel().getElementAt(selIndex);
            //JCheckBox selItem = (JCheckBox) lists.getModel().getElementAt(selIndex);
    	[/b]
    	if(selItem.isSelected())
    	{   selItem.setSelected(false);
    	}
    	else
    	{   selItem.setSelected(true);
    	}
        } 
    }
    
    [b][u]ERROR:[/u][/b]
        Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to javax.swing.JCheckBox
        ...
    then I found this example How do add a checkbox to items in a JList? | web development helpdesk and realizes my errors.

    Here is the updated code:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JListAndJCheckBox extends JFrame
    {	JList lists;
    	public JListAndJCheckBox()
    	{	JListCheckBoxItem[] listData = {new JListCheckBoxItem("Hello"), 
    			new JListCheckBoxItem("Java"), 
    			new JListCheckBoxItem("World")};
    		lists = new JList(listData);
    		lists.setCellRenderer(new JListCheckBox());
    		lists.addMouseListener(new MouseAdapter()
    		{	public void mouseClicked(MouseEvent evt)
    			{	listsMouseClicked(evt);
    			}
    		});
    		lists.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		this.setLayout(new BorderLayout());
    		this.add(lists, BorderLayout.CENTER);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setVisible(true);
    		pack();
    	}
    	
    	private void listsMouseClicked(MouseEvent evt)
    	{	int selIndex = lists.locationToIndex(evt.getPoint());
    		JListCheckBoxItem selItem = (JListCheckBoxItem) lists.getModel().getElementAt(selIndex);
    		System.out.println("CLICKED #1: " + selItem.isSelected());
    		if(selItem.isSelected())
    		{	selItem.setSelected(false);
    		}
    		else
    		{	selItem.setSelected(true);
    		}
    		lists.repaint(lists.getCellBounds(selIndex, selIndex));
    		System.out.println("CLICKED #2: " + selItem.isSelected());
    
    	}
    	
    	public static void main(String[] args)
    	{	JListAndJCheckBox jlcb = new JListAndJCheckBox();
    	}
    
    	protected class JListCheckBoxItem
    	{	String  label;
        		boolean isSelected = false;
    
        		public JListCheckBoxItem(String label)
        		{	this.label = label;
        		}
    
        		public boolean isSelected()
        		{	return isSelected;
        		}
    
        		public void setSelected(boolean isSelected)
        		{	this.isSelected = isSelected;
        		}
    
        		public String toString()
        		{	return label;
        		}
    	}
    
    	protected class JListCheckBox extends JCheckBox implements ListCellRenderer
    	{
        		int clicked = 0;
        		JCheckBox checkBox;
    
        		public Component getListCellRendererComponent(JList list, Object value,
                			int index, boolean isSelected, boolean cellHasFocus)
        		{
            			checkBox = new JCheckBox();
            			checkBox.setText(value.toString());
    			JListCheckBoxItem newValue = (JListCheckBoxItem)value; //convert Object value to a JListCheckBoxItem
    			if( newValue.isSelected())
            			{
                				checkBox.setBackground(getBackground());
                				checkBox.setForeground(new Color(0, 0, 0));
            			}
            			else
            			{   	checkBox.setBackground(new Color(50, 100, 150));
                				checkBox.setForeground(new Color(255, 255, 255));
            			}
            
    			checkBox.setSelected(newValue.isSelected());
           	 		
    			if(cellHasFocus)
            			{  	checkBox.setBackground(list.getSelectionBackground());
                				checkBox.setForeground(list.getSelectionForeground());
            			}
            			else
            			{   	checkBox.setBackground(list.getBackground());
                				checkBox.setForeground(list.getForeground());
            			}
            			return checkBox;
        		}
    	}
    }
    Thanks for the help. :)

Similar Threads

  1. JCheckbox in jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 3
    Last Post: 09-07-2010, 05:29 AM
  2. How to add a jcheckbox in jtable
    By lakshayghai in forum AWT / Swing
    Replies: 9
    Last Post: 04-14-2010, 11:32 PM
  3. How can I add JCheckBox to each row?
    By batya in forum AWT / Swing
    Replies: 1
    Last Post: 11-04-2009, 09:25 PM
  4. JCheckbox in Jtree
    By shajuantony in forum AWT / Swing
    Replies: 10
    Last Post: 09-09-2009, 10:42 AM
  5. jcheckbox issues need help. thanks.
    By carlos123 in forum New To Java
    Replies: 3
    Last Post: 11-05-2007, 10:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •