简体   繁体   English

区分大小写的jcombobox

[英]case sensitive jcombobox

My problem is a bit tricky. 我的问题有点棘手。 I am using an Editable JComboBox . 我正在使用Editable JComboBox It may contain case sensitive items. 它可能包含区分大小写的项目。 For example, it may have Item1 and item1 . 例如,它可能具有Item1item1 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 ). 无论我选择了哪个项目,它始终会选择第一个项目( Item1 )。 I've searched in Google, but didn't find any solution. 我已经在Google中搜索过,但没有找到任何解决方案。 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 . ItemObject是一个customClass,其中一个字段是BigInteger ,另一个字段是String

getItemTypeComboData is functioning properly. getItemTypeComboData运行正常。 So, you can assume to get a list of ItemObject from here and it will nicely convert it to Vector<ItemObject> 因此,您可以假定从此处获取ItemObject的列表,它将很好地将其转换为Vector<ItemObject>

jcbItemType.getSelectedIndex() always return the same index for Item1 and item1 . jcbItemType.getSelectedIndex()始终为Item1item1返回相同的索引。 But it returns different index for item2 . 但是它为item2返回不同的索引。

I know, it would be better if I can use itemStateChanged event. 我知道,如果可以使用itemStateChanged事件会更好。 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. 但是我的问题是, MouseReleasedFocusLost适用于不同的名称字符串,但不适用于具有不同大小写的相同字符串。 I am really stumbled. 我真的迷迷糊糊了。

Another way to ask the question: 提出问题的另一种方式:

Does MouseReleased or FocusLost event check for case-sensitive items? MouseReleasedFocusLost事件是否检查区分大小写的项目?

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! 这是我的SSCCE ,可以正常工作,如果这不是您想要的,请发布您的SSCCE,以获得更好的尽快帮助!


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(); 现在,您必须以如下数组形式访问已插入到其中的JCombobox元素:-MyItemListener actionListener = new MyItemListener(); cb.addItemListener(actionListener); 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 添加itemListener之后,您可以使用单个JCombobox Item执行不同的任务

Try this, it works fine... 试试这个,效果很好...

use ActionListener() to capture the click... then use getSelectedItem() to capture the item clicked on the JComboBox 使用ActionListener()捕获点击...然后使用getSelectedItem()捕获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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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