简体   繁体   中英

case sensitive jcombobox

My problem is a bit tricky. I am using an Editable JComboBox . It may contain case sensitive items. For example, it may have Item1 and item1 . So, these two items should be treated as different in my case.

But the problem is, these two items is treated as same. No matter which Items I have selected, it always select the first one ( Item1 ). I've searched in Google, but didn't find any solution. That's why, I am here.

Code:

//loading of Items
jdcbmItemType = new javax.swing.DefaultComboBoxModel(ItemTypeHandler.getItemTypeComboData(MainFrame.companyId));

private void jcbItemTypeMouseReleased(MouseEvent evt)
{
    if (jcbItemType.getSelectedIndex() != -1)
    {
        loadItemTypeDetails(((ItemObject) jcbItemType.getSelectedItem()).getId());
    }
    else
    {
        resetFields();
    }
}

public static Vector<ItemObject> getItemTypeComboDataV(BigInteger companyId, BigInteger categoryId, boolean addFirstElement, TriState deleted) throws ExceptionWrapper, EJBException
{
    try
    {            
        return (Vector<ItemObject>)lookupItemTypeFacade().getItemTypeComboData(companyId, categoryId, addFirstElement, deleted);
    } catch (ExceptionWrapper exceptionWrapper)
    {
        throw exceptionWrapper;
    } catch (EJBException ejbEx)
    {
        throw ejbEx;
    } catch (Exception ex)
    {
        throw new ExceptionWrapper(ex.getMessage());
    }
}

ItemObject is a customClass where one field is BigInteger and another is String .

getItemTypeComboData is functioning properly. So, you can assume to get a list of ItemObject from here and it will nicely convert it to Vector<ItemObject>

jcbItemType.getSelectedIndex() always return the same index for Item1 and item1 . But it returns different index for item2 .

I know, it would be better if I can use itemStateChanged event. But in my case, I can't use it. But my question is, MouseReleased and FocusLost works fine for different name string but not same string with different case. I am really stumbled.

Another way to ask the question:

Does MouseReleased or FocusLost event check for case-sensitive items?

How to resolve this problem?

Thanks.

Here is my SSCCE and this works fine , If this is not what youre looking for, then post your SSCCE for better sooner help!


import javax.swing.*;
import java.awt.event.*;

public class ComboBoxTest {

    JComboBox combo;
    JTextField txt;

    public static void main(String[] args) {
        new ComboBoxTest();
    }

    public ComboBoxTest() {
        String items[] = {"Item1", "item1"};
        JFrame frame = new JFrame("JComboBox Case-sensitivity Test");
        JPanel panel = new JPanel();
        combo = new JComboBox(items);
        combo.setEditable(true);

        txt = new JTextField(10);
        panel.add(combo);
        panel.add(txt);
        frame.add(panel);
        combo.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent ie) {
                String str = (String) combo.getSelectedItem();
                txt.setText(str);
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 100);
        frame.setVisible(true);
    }
}

I think you are doing like this :-

String[] items = {"item1", "item2"};
JComboBox cb = new JComboBox(items);
cb.setEditable(true);

Now you have to access the JCombobox elements which you have insert into this as in array form like this:- MyItemListener actionListener = new MyItemListener(); cb.addItemListener(actionListener);

class MyItemListener implements ItemListener {
// This method is called only if a new item has been selected.
public void itemStateChanged(ItemEvent evt) {
    JComboBox cb = (JComboBox)evt.getSource();

    // Get the affected item
    Object item = evt.getItem();

    if (evt.getStateChange() == ItemEvent.SELECTED) {
        // Item was just selected
    } else if (evt.getStateChange() == ItemEvent.DESELECTED) {
        // Item is no longer selected
    }
    }
 }

After adding the itemListener you can do your different tasks with individual JCombobox Item

Try this, it works fine...

use ActionListener() to capture the click... then use getSelectedItem() to capture the item clicked on the JComboBox

try this,

check in your console for the output

myComboBox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent ie) {
            String str = (String) myComboBox.getSelectedItem();
           System.out.println(str);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM