DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2006
    Posts
    4

    Unhappy How to add a CheckBox into an item within a ListBox?

    Hi all,

    I need to add a CheckBox into the items of a list box to indicate this item has been selected, but I can't find the right way to do so. I will appreciate it if somebody can help me to resolve this problem. Thanks a lot.

    Regards,
    Ken

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Use listcellrenderer

    ..like this. Just remember that a JCheckBox renderer is only a JCheckBox by its looks, e.g. it will not get selected/unselected when you click it in the list. To do that you would have to implement a mouse listener for your list and process the list clicked list element (set it selected/unselected).

    So, don't mix up the list element's isSelected and the JCheckBox's isSelected() ...

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    /**
    * Using ListCellRenderer
    **/
    public class CheckList extends JFrame implements ListCellRenderer, ActionListener {
      private Color stdColor=null;
      BorderLayout borderLayout1 = new BorderLayout();
      JScrollPane jScrollPane1 = new JScrollPane();
      JList theList = new JList();
      JPanel jPanel1 = new JPanel();
      JButton addOnBtn = new JButton();
      JButton addOffBtn = new JButton();
    
      public CheckList() {
        setBounds(10,10,170,270);
        try {
          jbInit();
          stdColor=getBackground();
          addOnBtn.addActionListener(this);
          addOffBtn.addActionListener(this);
          theList.setCellRenderer(this); // <---
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        CheckList radioList = new CheckList();
        radioList.setVisible(true);
      }
      /**
      * The ListCellRenderer interface implementation method
      */
      public Component getListCellRendererComponent(JList list,
                                                    Object value,
                                                    int index,
                                                    boolean isSelected,
                                                    boolean cellHasFocus) {
    
        JCheckBox aCB=(JCheckBox)value;
        if (isSelected) {
          aCB.setBackground(Color.red);
        } else {
          aCB.setBackground(stdColor);
        }
    
        return aCB;
    
      }
      private void jbInit() throws Exception {
        this.getContentPane().setLayout(borderLayout1);
        addOnBtn.setBounds(new Rectangle(20, 7, 60, 21));
        addOnBtn.setPreferredSize(new Dimension(60, 22));
        addOnBtn.setText("On");
        jPanel1.setLayout(null);
        jPanel1.setMinimumSize(new Dimension(1, 1));
        jPanel1.setPreferredSize(new Dimension(1, 32));
        addOffBtn.setBounds(new Rectangle(89, 7, 60, 21));
        addOffBtn.setText("Off");
        this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
        this.getContentPane().add(jPanel1, BorderLayout.SOUTH);
        jPanel1.add(addOnBtn, null);
        jPanel1.add(addOffBtn, null);
        jScrollPane1.getViewport().add(theList, null);
      }
      private void addToList(Object ob) {
        Vector v=new Vector();
        for (int i=0; i<theList.getModel().getSize(); i++) {
          v.addElement(theList.getModel().getElementAt(i));
        }
        v.addElement(ob);
        theList.setListData(v);
      }
      public void actionPerformed(ActionEvent e) {
        JCheckBox cb=null;
        if (e.getSource()==addOnBtn) {
          cb=new JCheckBox("I'm ON",true);
    
        } else {
          cb=new JCheckBox("I'm OFF",false);
        }
        addToList(cb);
      }
    }
    Last edited by sjalle; 01-10-2006 at 02:42 AM.
    eschew obfuscation

  3. #3
    Join Date
    Jan 2006
    Posts
    4
    That is very helpful reply, thanks a lot.

  4. #4
    Join Date
    May 2006
    Posts
    1
    I had the same problem. I fixed it thanks to your code, but now again, I have another problem. I want the user to be able to check the box on and off in the list box. How do I do that, or rather, can it be done???

Similar Threads

  1. Itemdata in Combo box
    By Raj in forum .NET
    Replies: 8
    Last Post: 10-25-2009, 08:36 AM
  2. Listbox with Checkbox
    By chagen in forum VB Classic
    Replies: 1
    Last Post: 01-02-2006, 01:59 PM
  3. Add Items from a listbox to a table
    By Dean Earley in forum VB Classic
    Replies: 4
    Last Post: 06-14-2002, 12:55 PM
  4. Need client-side script to add to listbox
    By mark erickson in forum ASP.NET
    Replies: 2
    Last Post: 11-09-2001, 02:42 PM
  5. using selected item from a checkbox list
    By Deb in forum VB Classic
    Replies: 0
    Last Post: 08-31-2000, 11:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links