栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JCombobox更改另一个JCombobox

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JCombobox更改另一个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);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/437691.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号