Inserir JCheckBox em JComboBox

3 respostas
viniciusfaleiro

Alguém sabe como eu faço para inserir vários JCheckBox com um texto ao lado dele, dentro de um JComboBox? E como tratar os itens selecionados??

3 Respostas

M

Caramba??Usar num painel ou em uma tabela não é mais fácil?

viniciusfaleiro
Também acho que seria. Más que graça teria se minha intenção é aprender coisas novas? então, segue o código:

import java.awt.<em>;

import java.awt.event.</em>;

import javax.swing.*;
public class CheckCombo implements ActionListener{   
	public void actionPerformed(ActionEvent e)    {        
		JComboBox cb = (JComboBox)e.getSource();        
		CheckComboStore store = (CheckComboStore)cb.getSelectedItem();        
		CheckComboRenderer ccr = (CheckComboRenderer)cb.getRenderer();       
		ccr.checkBox.setSelected((store.state != store.state));    }     
	private JPanel getContent()    {        
		String[] ids = { "north", "west", "south", "east" };        
		Boolean[] values =        
		{            Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE        };        
		CheckComboStore[] stores = new CheckComboStore[ids.length];       
		for(int j = 0; j < ids.length; j++)            
			stores[j] = new CheckComboStore(ids[j], values[j]);        
		JComboBox combo = new JComboBox(stores);        
		combo.setRenderer(new CheckComboRenderer());        
		combo.addActionListener(this);        
		JPanel panel = new JPanel();        
		panel.add(combo);        
		return panel;    }     
	
	public static void main(String[] args)    {       
		JFrame f = new JFrame();        
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
		f.getContentPane().add(new CheckCombo().getContent());        
		f.setSize(300,160);        
		f.setLocation(200,200);        
		f.setVisible(true);    }} /** adapted from comment section of ListCellRenderer api */

class CheckComboRenderer implements ListCellRenderer{    
	JCheckBox checkBox;     
	public CheckComboRenderer()    
	{        
		checkBox = new JCheckBox();    }    
	public Component getListCellRendererComponent(JList list,
			Object value,                                                  
			int index,                                                  
			boolean isSelected,                                                  
			boolean cellHasFocus)    {        
		CheckComboStore store = (CheckComboStore)value;        
		checkBox.setText(store.id);       
		checkBox.setSelected(((Boolean)store.state).booleanValue());        
		checkBox.setBackground(isSelected ? Color.red : Color.white);        
		checkBox.setForeground(isSelected ? Color.white : Color.black);        
		return checkBox;    }} 

class CheckComboStore{    
	String id;    
	Boolean state;     
	public CheckComboStore(String id, Boolean state)    {        
		this.id = id;        
		this.state = state;    }
	
	
}

é quase isso que eu queria,más ja é meio caminho andado.

M

Se você colocar isto no método de ação do combobox:

if(store.state==false)
	store.state = true;        
else
	store.state =false;
Criado 29 de março de 2008
Ultima resposta 30 de mar. de 2008
Respostas 3
Participantes 2