-
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
-
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
-
That is very helpful reply, thanks a lot.
-
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
-
Replies: 8
Last Post: 10-25-2009, 08:36 AM
-
By chagen in forum VB Classic
Replies: 1
Last Post: 01-02-2006, 01:59 PM
-
By Dean Earley in forum VB Classic
Replies: 4
Last Post: 06-14-2002, 12:55 PM
-
By mark erickson in forum ASP.NET
Replies: 2
Last Post: 11-09-2001, 02:42 PM
-
By Deb in forum VB Classic
Replies: 0
Last Post: 08-31-2000, 11:27 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
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
|
Bookmarks