在 Swing 中绑定组合框 [英] Binding comboboxes in swing

查看:26
本文介绍了在 Swing 中绑定组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Eclipse IDE 开发桌面(swing)应用程序.我有三个组合框(国家、州和城市),当我选择一个新的国家或省时,我需要自动更新数据.我搜索了很多信息,但我找到的所有实现都是在 Ajax 或 NetBeans 中的 beansbinding 框架上进行的.我尝试了 ItemEvent 的解决方案,但是我在启动我的应用程序时遇到了问题,它加载了国家/地区列表而不是其他列表.并通过选择一个国家来收取州列表而不是城市列表.

我的代码:

 jComboBoxCountries.addItemListener(new java.awt.event.ItemListener() {公共无效 itemStateChanged(java.awt.event.ItemEvent evt) {jComboBoxStates.setModel(new javax.swing.DefaultComboBoxModel(statesOf(evt.getItem()).toArray()));}});jComboBoxStates.addItemListener(new java.awt.event.ItemListener() {公共无效 itemStateChanged(java.awt.event.ItemEvent evt) {jComboBoxCities.setModel(new javax.swing.DefaultComboBoxModel(cityOf(evt.getItem()).toArray()));}});jComboBoxCountries.setModel(new javax.swing.DefaultComboBoxModel(countryList.toArray()));

解决方案

我在启动我的应用程序时遇到问题,它加载了国家/地区列表,但没有加载其他列表

好像必须专门设置选定的索引才能调用侦听器.

jComboBoxCountries.setModel(...)jComboBoxCountries.setSelectedIndex(0);

<块引用>

并且通过选择一个国家,会向州列表收费,但不会向城市列表收费.

我猜这是同样的问题,一旦您重置状态组合框的模型,您还需要选择其索引.

另一种方法是不选择默认州或城市,而是提示用户选择一个.下面是一些使用这种方法的代码:

import java.awt.*;导入 java.awt.event.*;导入 java.util.*;导入 javax.swing.*;公共类 ComboBoxTwo 扩展 JFrame 实现 ActionListener{私人 JComboBox mainComboBox;私人 JComboBox subComboBox;private Hashtable subItems = new Hashtable();公共 ComboBoxTwo(){String[] items = { "Select Item", "Color", "Shape", "Fruit" };mainComboBox = new JComboBox( items );mainComboBox.addActionListener( this );//防止在使用向上/向下箭头键时触发动作事件//mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);getContentPane().add( mainComboBox, BorderLayout.WEST );//创建具有多个模型的子组合框subComboBox = new JComboBox();subComboBox.setPrototypeDisplayValue("XXXXXXXXXX");//JDK1.4getContentPane().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 = { "选择水果", "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 );如果(o == 空){subComboBox.setModel( new DefaultComboBoxModel() );}别的{subComboBox.setModel(new DefaultComboBoxModel((String[])o));}}public static void main(String[] args){JFrame frame = new ComboBoxTwo();frame.setDefaultCloseOperation( EXIT_ON_CLOSE );框架.pack();frame.setLocationRelativeTo( null );frame.setVisible(true);}}

I'm working on a desktop (swing) application with Eclipse IDE. I have three comboboxes (countries, states and cities) and I need to update the data automatically when I selecting a new country or province. I searched lot of information, but all the implementations I found are made on Ajax or the beansbinding framework in NetBeans. I tried a solution by ItemEvent, but I have problems starting my application it loads the list of countries but not the other lists. And by selecting a country is charged the list of states but not the list of cities.

My code:

    jComboBoxCountries.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            jComboBoxStates.setModel(new javax.swing.DefaultComboBoxModel(
                    statesOf(evt.getItem()).toArray() ));
            }
        });

    jComboBoxStates.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxCities.setModel(new javax.swing.DefaultComboBoxModel(
                    citiesOf(evt.getItem()).toArray()) );
            }
    });

    jComboBoxCountries.setModel(new javax.swing.DefaultComboBoxModel(
            countryList.toArray()));

解决方案

I have problems starting my application it loads the list of countries but not the other lists

It seems like you have to specifically set the selected index to invoke the listener.

jComboBoxCountries.setModel(...)
jComboBoxCountries.setSelectedIndex(0);

And by selecting a country is charged the list of states but not the list of cities.

I would guess this is the same problem, once you reset the model of the states combobox you would need to select its index as well.

Another approach is to not select a default state or city, but instead prompt the user to select one. Here is some code that uses this approach:

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

这篇关于在 Swing 中绑定组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆