当 ComboBoxModel 变空时清除最后选定的值 JCombobox

发布于 2024-10-31 11:26:18 字数 1110 浏览 2 评论 0原文

这是一个场景:

我有两个 JComboBox (称它们为combo1 和combo2 )从数据库获取它们的值[在数据库中,这些两者具有 1:M 关系]。当屏幕显示时,我用数据库中的值填充combo1,然后获取列表中的第一个条目并获取其相应的值来填充combo2。

由于组合 2 的值取决于组合 1 中选择的内容,因此每次组合 1 中的选择发生更改时,都会调用数据库以获取匹配值来填充组合 2。

现在有一个问题:

假设我在combo1中有两个条目,而第二个条目对于combo2没有相应的值。当我选择组合 1 的第二个条目时,组合 2 上最后选择的值不会清除。 [请记住,combo2 的模型是空的,因此不应选择任何内容]

问题:如果模型为空,如何清除combo2 中的文本?

这是示例代码:

public void select(final Entry entry) {
      if (entry == null)
         return;

         int index = entryList.indexOf(entry); // instance of SelectionInList from JGoodies
         boolean positive = index >= 0 && index <= entryList.getSize() - 1;

         if (positive) {

              entryList.setSelection(entry);


               subEntryList.setList(entryList.loadSubEntries(entry.getID()));
               if (!subEntryList.isEmpty()) {
                    SubEntry e = subEntryList.getElementAt(0);
                    select(e);
                 }
        }

}

Here is a scenario:

I have two JComboBoxes (call them combo1 and combo2 ) that get their values from a database[In the DB, these two have a 1:M relationship]. When the screen shows up I populate combo1 with values from the database and then take the first entry in the list and get its corresponding values to populate combo2.

Since the values of combo2 depend on what is selected in combo1, every time the selection changes in combo1 a call is made to the database to get matching values to populate combo2.

Now here is a problem:

Say I have two entries in combo1 and the second entry has no corresponding values for combo2. When I select the second entry of combo1, the last selected value on combo2 does not clear. [Remember the model for combo2 is empty and therefore there shouldn't anything selected]

Qeustion: How do I clear the text in combo2 if the model is empty?

Here is a sample code:

public void select(final Entry entry) {
      if (entry == null)
         return;

         int index = entryList.indexOf(entry); // instance of SelectionInList from JGoodies
         boolean positive = index >= 0 && index <= entryList.getSize() - 1;

         if (positive) {

              entryList.setSelection(entry);


               subEntryList.setList(entryList.loadSubEntries(entry.getID()));
               if (!subEntryList.isEmpty()) {
                    SubEntry e = subEntryList.getElementAt(0);
                    select(e);
                 }
        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶花眉 2024-11-07 11:26:18

如果您有一个空的组合框模型,则视图应自动清除。如果您派生了自己的模型,请不要忘记在删除条目时调用 DefaultComboBoxModel.fireIntervalRemoved()

另一种(在本例中不推荐)方法是使用combobox.setSelectedItem(null);

If you have an empty combobox model the view should clear automatically. If you derived an own model do not forget to call DefaultComboBoxModel.fireIntervalRemoved() if you remove entries.

Another (in this case not recommended) way is to use combobox.setSelectedItem(null);.

彩扇题诗 2024-11-07 11:26:18

当您在第一个组合框中进行选择时,替换第二个组合框中的模型:

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

public class ComboBoxTwo extends JFrame implements ActionListener
{
    private JComboBox mainComboBox;
    private JComboBox subComboBox;
    private Hashtable subItems = new Hashtable();

    public ComboBoxTwo()
    {
        String[] items = { "Select Item", "Color", "Shape", "Fruit" };
        mainComboBox = new JComboBox( items );
        mainComboBox.addActionListener( 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 );

        //  Create sub combo box with multiple models

        subComboBox = new JComboBox();
        subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
        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);
    }

    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 ) );
        }
    }

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

Replace the model in the second combo box when you make a selection in the first:

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

public class ComboBoxTwo extends JFrame implements ActionListener
{
    private JComboBox mainComboBox;
    private JComboBox subComboBox;
    private Hashtable subItems = new Hashtable();

    public ComboBoxTwo()
    {
        String[] items = { "Select Item", "Color", "Shape", "Fruit" };
        mainComboBox = new JComboBox( items );
        mainComboBox.addActionListener( 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 );

        //  Create sub combo box with multiple models

        subComboBox = new JComboBox();
        subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
        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);
    }

    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 ) );
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxTwo();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
     }
}
ま昔日黯然 2024-11-07 11:26:18

只需执行以下操作:

jcombobox.setSelectedIndex(-1);

之后:

jcombobox.getSelectedIndex();

Just do this:

jcombobox.setSelectedIndex(-1);

after:

jcombobox.getSelectedIndex();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文