根据选定的JComboBox更改的JList内容 [英] JList content that changes depending on the selected JComboBox made in
问题描述
我想用Java创建一个由JComboBox和JList组成的GUI.
I would like to create a GUI in Java that is composed of a JComboBox and JList.
从JComboBox {A,B,C}中选择替代项. 根据做出的选择(A,B或C)显示在JList中的不同列表中.
The alternatives are selected from the JComboBox {A, B, C}. Depending on the choice made (A, B, or C) are shown in JList different lists.
示例: 如果我在JComboBox中选择A,则JList将显示以下字符串:A1,A2,A3,A4
example: if I select A in the JComboBox, JList will show the following strings: A1, A2, A3, A4
如果我在JComboBox中选择B,则JList将显示以下字符串:B1,B2,B3,B4
if I select B in the JComboBox, JList will show the following strings: B1, B2, B3, B4
如果我在JComboBox中选择C,则JList将显示以下字符串:C1,C2,C3,C4
if I select C in the JComboBox, JList will show the following strings: C1, C2, C3, C4
我该怎么做?
谢谢
推荐答案
您将ActionListener添加到组合框中.然后,当一个项目被选定则用一个新的ListModel更新JList中.
You add an ActionListener to the combo box. Then when an item is selected you update the JList with a new ListModel.
这里有一个带有两个组合框的示例,但是对于一个组合框和一个JList来说,概念是相同的.
Here is an example with two combo boxes, but the concept would be the same with a combo box and a JList.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
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 );
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()
{
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();
}
});
}
}
这篇关于根据选定的JComboBox更改的JList内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!