如何根据不同组合框的所选项目更改组合框选项
How to change comboBox options depending on the selected item of a different comboBox
如何根据不同组合框的所选项目更改组合框的选项?
public class Test extends JFrame{
String[] test = {"blank", "blank_1"};
if (comboBox.getSelectedItem() == "blank"){
String[] test_1 = {"test"};
}
else {
String[] test_1 = {"test_1"};
}
JComboBox comboBox = new JComboBox(test);
JComboBox comboBox_1 = new JComboBox(test_1);
}
在第一个中添加一个ActionListener
,触发时从中获取selectedItem
,将第二个的ComboBoxModel
替换为所需的值
您可以使用某种 Map
在第一个组合框中的值与第二个组合框中的数据之间进行映射,让生活更轻松
先看看:
您必须创建一个动作
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jComboBox1.getSelectedItem().toString().equals("blank")) {
String[] test_1 = {"test1"};
DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel(test_1);
jComboBox2.setModel(defaultComboBoxModel);
} else {
String[] test_1 = {"test_1"};
DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel(test_1);
jComboBox2.setModel(defaultComboBoxModel);
}
}
这里有一个简单的例子可以帮助您入门:
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();
}
});
}
}
如何根据不同组合框的所选项目更改组合框的选项?
public class Test extends JFrame{
String[] test = {"blank", "blank_1"};
if (comboBox.getSelectedItem() == "blank"){
String[] test_1 = {"test"};
}
else {
String[] test_1 = {"test_1"};
}
JComboBox comboBox = new JComboBox(test);
JComboBox comboBox_1 = new JComboBox(test_1);
}
在第一个中添加一个ActionListener
,触发时从中获取selectedItem
,将第二个的ComboBoxModel
替换为所需的值
您可以使用某种 Map
在第一个组合框中的值与第二个组合框中的数据之间进行映射,让生活更轻松
先看看:
您必须创建一个动作
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jComboBox1.getSelectedItem().toString().equals("blank")) {
String[] test_1 = {"test1"};
DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel(test_1);
jComboBox2.setModel(defaultComboBoxModel);
} else {
String[] test_1 = {"test_1"};
DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel(test_1);
jComboBox2.setModel(defaultComboBoxModel);
}
}
这里有一个简单的例子可以帮助您入门:
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();
}
});
}
}