English 中文(简体)
Java Swing Combo箱 要求项目国家的所有项目 变化吗?
原标题:Java Swing Combobox removeAllItems calling ItemStateChanged also?
最佳回答

As nIcE cOw在其例子中已经说明过,在你使用时,它当然应该发挥作用。 (他的样本代码就是这种情况,尽管是在屏幕后面。)

我可以解释你就非<代码”所遭遇的行为。 DefaultComboBoxModel case,尽管你的代码表示你使用。 查阅<代码>JComboBox#remove AllItems的源代码。 由于<代码>取消所有Elements方法不属于<代码>的一部分,因此有一条不同的代码。 MutableComboBoxModel界面

public void removeAllItems() {
    checkMutableComboBoxModel();
    MutableComboBoxModel<E> model = (MutableComboBoxModel<E>)dataModel;
    int size = model.getSize();

    if ( model instanceof DefaultComboBoxModel ) {
        ((DefaultComboBoxModel)model).removeAllElements();
    }
    else {
        for ( int i = 0; i < size; ++i ) {
            E element = model.getElementAt( 0 );
            model.removeElement( element );
        }
    }
    selectedItemReminder = null;
    if (isEditable()) {
        editor.setItem(null);
    }
}

因此,有非<代码>。 DefaultComboBoxModel 各位将逐一拆除这些物品。 这意味着,在某个时候,你将删除选定的要素。 可能实施你的模型可能会改变当时选定的要素。 请参看<代码>DefaultComboBoxModel中的实施情况。 (虽然这部法典不会启动),但你可以清楚地看到它改变了选择。

public void removeElementAt(int index) {
    if ( getElementAt( index ) == selectedObject ) {
        if ( index == 0 ) {
            setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
        }
        else {
            setSelectedItem( getElementAt( index - 1 ) );
        }
    }

    objects.removeElementAt(index);

    fireIntervalRemoved(this, index, index);
}

也许你的模式类似,这解释了这一事件。 若要完成这一职务,则在<代码>后面的代码。 DefaultComboBox Model#removeAllElements 如果你能够清楚地看到,它把甄选安排到<代码>null,而不选择另一个标的。 该守则中仅有的一点是,它不会首先发射<条码>> 签字<>> > > 签字> 。 虽然你知道,如果你听说<条码>休会的活动,选择已经发生变化......,但这与你的问题无关。

public void removeAllElements() {
    if ( objects.size() > 0 ) {
        int firstIndex = 0;
        int lastIndex = objects.size() - 1;
        objects.removeAllElements();
        selectedObject = null;
        fireIntervalRemoved(this, firstIndex, lastIndex);
    } else {
        selectedObject = null;
    }
}

最后: 我说,解决你的问题的办法是以你的模式,而不是在你颁布的法典中。

问题回答

在这里,如果你打电话<条码>删除:

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

public class ComboState 
{
    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Combo State Testing : ");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JComboBox cbox = new JComboBox();
        cbox.addItem("One");
        cbox.addItem("Two");
        cbox.addItem("Three");
        cbox.addItem("Four");
        cbox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent ie)
            {
                if (ie.getStateChange() == ItemEvent.SELECTED)
                {
                    System.out.println("Item Selected is : " + ie.getItem());
                }
                /*else
                {
                    System.out.println("I am called for other reasons.");
                }*/
            }
        });

        /*
         * Click me to remove JComboBox s items.
         */
        JButton removeButton = new JButton("REMOVE");
        removeButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                cbox.removeAllItems();
            }
        });

        frame.add(cbox, BorderLayout.CENTER);
        frame.add(removeButton, BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ComboState().createAndDisplayGUI();
            }
        });
    }
}

这样做的一个快速办法是,在你要求拆除一切照旧器和背后作假。 只有在ool变不实的情况下,才在贵件中删除。

理想的做法是,你可以超越排除一切职能。

整个讨论并不明确,

  • 您在删除所有物品之前,必须从JComboBox中删除所有清单,然后删除项目,然后把清单放在名单上。

  • 仍然无法确定您是否希望添加和删除; 具有活力的项目,或您可以为另一个联合协调委员会确定任何价值,

  • (简单事情复杂起来)你确实看到这一点,

import java。awt。*;
import java。awt。event。*;
import java。util。*;
import javax。swing。*;

public class ComboBoxTwo extends JFrame implements ActionListener, ItemListener {

    private static final long serialVersionUID = 1L;
    private JComboBox mainComboBox;
    private JComboBox subComboBox;
    private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();

    public ComboBoxTwo() {
        String[] items = {"Select Item", "Color", "Shape", "Fruit"};
        mainComboBox = new JComboBox(items);
        mainComboBox。addActionListener(this);
        mainComboBox。addItemListener(this);
        //prevent action events from being fired when the up/down arrow keys are used
        //mainComboBox。putClientProperty("JComboBox。isTableCellEditor", Boolean。TRUE);
        getContentPane()。add(mainComboBox, BorderLayout。WEST);
        subComboBox = new JComboBox();//  Create sub combo box with multiple models
        subComboBox。setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1。4
        subComboBox。addItemListener(this);
        getContentPane()。add(subComboBox, BorderLayout。EAST);
        String[] subItems1 = {"Select Color", "Red", "Blue", "Green"};
        subItems。put(items[1], subItems1);
        String[] subItems2 = {"Select Shape", "Circle", "Square", "Triangle"};
        subItems。put(items[2], subItems2);
        String[] subItems3 = {"Select Fruit", "Apple", "Orange", "Banana"};
        subItems。put(items[3], subItems3);
//      mainComboBox。setSelectedIndex(1);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String item = (String) mainComboBox。getSelectedItem();
        Object o = subItems。get(item);
        if (o == null) {
            subComboBox。setModel(new DefaultComboBoxModel());
        } else {
            subComboBox。setModel(new DefaultComboBoxModel((String[]) o));
        }
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e。getStateChange() == ItemEvent。SELECTED) {
            if (e。getSource() == mainComboBox) {
                if (mainComboBox。getSelectedIndex() != 0) {
                    FirstDialog firstDialog = new FirstDialog(ComboBoxTwo。this,
                            mainComboBox。getSelectedItem()。toString(), "Please wait,  Searching for 。。。。。 ");
                }
            } 
        }
    }

    private class FirstDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        FirstDialog(final Frame parent, String winTitle, String msgString) {
            super(parent, winTitle);
            setModalityType(Dialog。ModalityType。APPLICATION_MODAL);
            JLabel myLabel = new JLabel(msgString);
            JButton bNext = new JButton("Stop Processes");
            add(myLabel, BorderLayout。CENTER);
            add(bNext, BorderLayout。SOUTH);
            bNext。addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            javax。swing。Timer t = new javax。swing。Timer(1000, new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }
            });
            t。setRepeats(false);
            t。start();
            setLocationRelativeTo(parent);
            setSize(new Dimension(400, 100));
            setVisible(true);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new ComboBoxTwo();
        frame。setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame。pack();
        frame。setLocationRelativeTo(null);
        frame。setVisible(true);
    }
}

删除的方法 所有项目都不叫作项目说明,但称之为行动指示,你可以通过实施这一简单守则加以检查:

public class Tuto {

    public static void main(String[] args) {
        //create the main frame
        JFrame frame = new JFrame();
        frame.setResizable(false);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setLocation(new Point(10, 10));
        frame.setPreferredSize(new Dimension(400, 300));

        JComboBox<String> combo = new JComboBox();
        combo.addItem("item 1");
        combo.addItem("item 2");
        combo.addItem("item 3");
        combo.setBounds(50, 30, 300, 20);
        combo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.out.println(" action Performed ");
            }
        });
        frame.add(combo);

        JButton button = new JButton("Remove");
        button.setBounds(50, 100, 100, 30);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                combo.removeAllItems();
            }
        });
        frame.add(button);

        frame.pack();
        frame.setVisible(true);
    }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...