java - 使用一个 JComboBox 控制另一个 Jcombobox

标签 java sqlite jcombobox populate

我希望一个 JComboBox 的值根据另一个 JComboBox 的值进行更改。在一个 JComboBox 中,我显示了从数据库检索的医生名称。根据该名称,我想在另一个 JComboBox 中显示医生的姓名。所有信息都存储在 doctor 表中。 enter image description here

最佳答案

ActionListener 添加到第一个组合框。在监听器中,您需要根据所选项目重置第二个组合框的 ComboBoxModel

类似于:

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

public class ComboBoxTwo extends JPanel implements ActionListener
{
    private JComboBox<String> mainComboBox;
    private JComboBox<String> subComboBox;
    private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>();

    public ComboBoxTwo()
    {
        String[] items = { "Select Item", "Color", "Shape", "Fruit" };
        mainComboBox = new JComboBox<String>( items );
        mainComboBox.addActionListener( this );

        //  prevent action events from being fired when the up/down arrow keys are used
        mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        add( mainComboBox );

        //  Create sub combo box with multiple models

        subComboBox = new JComboBox<String>();
        subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
        add( subComboBox );

        JButton arrow = SwingUtils.getDescendantOfType(JButton.class, subComboBox, "Text", "");
        Dimension d = arrow.getPreferredSize();
        System.out.println(arrow.getClass());
        System.out.println(d);
        d.width = 100;
        arrow.setPreferredSize(d);

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

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

    private static void createAndShowUI()
    {
        try
        {
//          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) { }
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new ComboBoxTwo() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

关于java - 使用一个 JComboBox 控制另一个 Jcombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43593816/

相关文章:

java - 在 App Engine 上定期运行一些 java?

java - 为什么 jmap -permstat 会报告超过使用的 MaxPermSize 字节数?

ruby-on-rails - 将 sqlite 数据库表复制到 Postgres

c# - SQLite 的 .NET Framework 数据提供程序发生意外错误

java - 如何使字体列表在 JComboBox 中工作

java - 如何让 DocumentFilter 与 JComboBox 一起使用

Java:JComboBox 不接受 enum.values()

java - 分析 i += 1 和 i = i + 1 之间的差异

java - JVM 在没有完成方法的情况下停止,局部变量是否仍保留在堆栈中?

sqlite - PouchDB/SQLite索引崩溃-需要IOS上的大量内存