Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何填充第二个组合框_Java_Swing_Jcombobox - Fatal编程技术网

Java 如何填充第二个组合框

Java 如何填充第二个组合框,java,swing,jcombobox,Java,Swing,Jcombobox,如何填火鸡的第二份套餐? 当选择第一个组合土耳其时,安卡拉伊兹密尔填充到第二个组合 String[] s = {"Turkey", "Rusia", "Italia"}; for (String string : s) { comboBox.addItem(string); } String x = String.valueOf(comboBox.getSelectedItem()); if (x.equal

如何填火鸡的第二份套餐? 当选择第一个组合土耳其时,安卡拉伊兹密尔填充到第二个组合

String[] s = {"Turkey", "Rusia", "Italia"};
        for (String string : s) {
            comboBox.addItem(string);
        }
        String x = String.valueOf(comboBox.getSelectedItem());
        if (x.equalsIgnoreCase("Turkey")) {
            String[] s1 = {"Ankara", "Izmir"};
            for (String ss : s1) {
                comboBox1.addItem(ss);
            }
        } 

您需要将ActionListener添加到第一个组合框中,以便在选择项目时执行操作

比如:

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 = 35;
        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();
            }
        });
    }
}
import java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入javax.swing.*;
导入javax.swing.plaf.basic.*;
公共类ComboBoxTwo扩展JPanel实现ActionListener
{
专用JComboBox主组合框;
专用JComboBox子omboBox;
私有哈希表子项=新哈希表();
公营机构
{
字符串[]项={“选择项”、“颜色”、“形状”、“水果”};
mainComboBox=新的JComboBox(项目);
mainComboBox.addActionListener(这个);
//使用向上/向下箭头键时防止触发操作事件
mainComboBox.putClientProperty(“JComboBox.isTableCellEditor”,Boolean.TRUE);
添加(主组合框);
//创建具有多个模型的子组合框
subComboBox=新的JComboBox();
subComboBox.setPrototypeDisplayValue(“xxxxxxxxx”);//JDK1.4
添加(子控制台);
JButton arrow=SwingUtils.getDegenantofType(JButton.class,子omboBox,“Text”,“”);
维度d=arrow.getPreferredSize();
System.out.println(arrow.getClass());
系统输出打印ln(d);
d、 宽度=35;
箭头。设置首选大小(d);
字符串[]子项1={“选择颜色”、“红色”、“蓝色”、“绿色”};
子项目。投入(项目[1],子项目1);
字符串[]子项2={“选择形状”、“圆”、“正方形”、“三角形”};
子项目。投入(项目[2],子项目2);
String[]subItems3={“选择水果”、“苹果”、“橘子”、“香蕉”};
子项目。投入(项目[3],子项目3);
}
已执行的公共无效操作(操作事件e)
{
String item=(String)mainComboBox.getSelectedItem();
对象o=子项。获取(项);
如果(o==null)
{
setModel(新的DefaultComboxModel());
}
其他的
{
setModel(新的DefaultComboxModel((字符串[])o));
}
}
私有静态void createAndShowUI()
{
尝试
{
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
捕获(例外e){}
JFrame=新JFrame(“SSCCE”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新的ComboBoxTwo());
frame.setLocationByPlatform(真);
frame.pack();
frame.setVisible(true);
}
公共静态void main(字符串[]args)
{
invokeLater(新的Runnable()
{
公开募捐
{
createAndShowUI();
}
});
}
}

您必须使用DeafultComboxModel,准备所需数量的数组,然后从ItemListener更改另一个模型中的数组,最后将另一个模型添加到另一个JComboxModel,除此之外,顺便说几次,关于“?”的意思是什么?请展示一个示例?我如何将数据存储在MySQL数据库中,当我使用这个Java swing添加新数据时?在搜索完google之后,在单独的问题中提问。我可以问吗?
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 = 35;
        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();
            }
        });
    }
}