ComboBox não funcional na JRE 1.6

4 respostas
I

Pessoal,

Já procurei sobre o assunto e não tive êxito então vou tentar postar aqui para obter uma possível ideia do problema.

Vamos ao causo, implementei um Viewer para uma aplicação em Swing compilada na versão 1.5.0_15.. funcionando normalmente mas recentemente migrei a versão da maquina do usuário para 1.6 14 e o ComboBox existente passou a não se comportar direito.

Como funciona?
O ComboBox exibe uma lista de documentos e quando selecionado um item, exibimos a imagem correspondente ao documento.

Fato!
Após a migração ocorrer ao selecionar um item, nada ocorre.. a combo não dispara nenhum evento mais.. nem exceptions. nada de nada

O que já fiz?
Tentei compilar tudo novamente na versão 1.6 porem tb nao ajudo...
Procurei no forum da Oracle e só achei tiros no escuro ou pessoas dizendo que é um bug...

Segue a implementação da Combo a chamada para obter a seleção do item

public void actionPerformed(CommandEvent event) {
		switch (event.getId()) {

case Command.ACTION_TYPEDOC: {
    String type = (String) ((JComboBox) southTool.getControl("typeCombo")).getSelectedItem();
    break;
}..
.
}
}
ToolComboBox.java
import java.awt.Font;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JPopupMenu;
import javax.swing.JWindow;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

/**
 * This class extends the default swing combo box to provide some new features.
 * 
 * @author Johnnys
 * @see JComboBox
 */
public class ToolComboBox extends JComboBox implements PopupMenuListener {

	private int command;

	/**
	 * This window is set as invoker of the popup menu of the combo box.
	 * Setting it as invoker of the popup (instead of the default invoker), avoids the problem that occurs when the combo is open over an OLE components.
	 */
	private static final JWindow window = new JWindow();

	/**
	 * Builds a tool bar combo box.
	 * 
	 * @param command the command ID fired by the combo box
	 * @param action the listener of the combo box.
	 */
	public ToolComboBox(int command, ItemListener action) {
		super();
		this.command = command;
		setFont(new Font("Tahoma", Font.BOLD, 12));
		addItemListener(action);

		// to fix the problem that occurs when the combo box is open over an OLE component
		addPopupMenuListener(this);
	}

	/**
	 * Gets the command ID that the button fires.
	 * 
	 * @return the command ID
	 */
	public int getCommand() {
		return command;
	}

	/**
	 * Sets the command ID that the button fires.
	 * 
	 * @param command the command ID
	 */
	public void setCommand(int command) {
		this.command = command;
	}

	/**
	 * This method is called before the popup menu becomes visible.
	 * 
	 * @param e the event
	 */
	public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
		Object popup = getUI().getAccessibleChild(this, 0);
		if (!(popup instanceof JPopupMenu))
			return;
		((JPopupMenu)popup).setInvoker(window);
	}
	
	/**
    * This method is called before the popup menu becomes invisible.
    * 
    * @param e the event
    */
	public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
	}

	/**
    * This method is called when the popup menu is canceled
    * 
    * @param e the event
    */
	public void popupMenuCanceled(PopupMenuEvent e) {
	}
}

Desde já agradeço

Abraço!

4 Respostas

mateuscs

Boa tarde,

:lol:

Tive o mesmo problema a pouco tempo :twisted: , não é sua versão 1.6, na verdade o metodo que mudou, vc irá ter que setar o modelo novamente na combo box a cada clique na comboBox 1, setar na comboBox 2.
Ex:

ActionListener comboBoxSelect = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					comboSelected = (String) cbUF.getSelectedItem();
					listaMunicipio = DAOObjetoMeu.getSingleton()
							.obterListaMunicipio(comboSelected).toArray();
					modelCbMunicipio = new DefaultComboBoxModel(listaMunicipio);
					modelCbMunicipio.removeAllElements();
					for (Object obj : listaMunicipio) {
						modelCbMunicipio.addElement(obj);
					}
					getCbMunicipio().setModel(modelCbMunicipio);
				}
			};
			cbUF.addActionListener(comboBoxSelect);

Na verdade quero te mostrar a lógica que você ira utlizar, vc decide o melhor modo.
:wink:

I

Obrigado pelo retorno rápido,

Veja se captei a vossa msg… terei de setar o modelo dentro do meu ItemListener?

/** * This class fires the combo box events to the controller. */ private class ComboActionDispatcher implements ItemListener { public void itemStateChanged(ItemEvent e) { Object src = e.getSource(); if (src instanceof ToolComboBox) fireActionPerformed(new CommandEvent(((ToolComboBox) src).getCommand(), ((ToolComboBox) src).getSelectedItem(), src)); } };

mateuscs

Eu teria que testar na pratica, ou por algotimo essa sua logica, estou meio que sem tempo agora,
mais vou analizar e te retorno, mais seria o basico.

Setar Ação - 1
Adicionar Componentes

Monitorar(ActionListener) - Se cliclou na ComboBox
Remover Componentes
Adicionar Componentes Novos.

mateuscs

mateuscs:
Eu teria que testar na pratica, ou por algotimo essa sua logica, estou meio que sem tempo agora,
mais vou analizar e te retorno, mais seria o basico.

Setar Ação - 1
Adicionar Componentes

Monitorar(ActionListener) - Se cliclou na ComboBox
Remover Componentes
Adicionar Componentes Novos.

ActionListener comboBoxSelect = new ActionListener() {  
                    public void actionPerformed(ActionEvent e) {  
                        comboSelected = (String) cbUF.getSelectedItem();  
                        listaMunicipio = DAOObjetoMeu.getSingleton()  
                                .obterListaMunicipio(comboSelected).toArray();  
                        modelCbMunicipio = new DefaultComboBoxModel(listaMunicipio);  
                        modelCbMunicipio.removeAllElements();  
                        for (Object obj : listaMunicipio) {  
                            modelCbMunicipio.addElement(obj);  
                        }  
                        getCbMunicipio().setModel(modelCbMunicipio);  
                    }  
                };  
                cbUF.addActionListener(comboBoxSelect);

Isso que meu codigo faz.

Criado 26 de agosto de 2011
Ultima resposta 26 de ago. de 2011
Respostas 4
Participantes 2