Java 如何获得组合框值?
我的代码如下所示:Java 如何获得组合框值?,java,swing,combobox,jtable,jframe,Java,Swing,Combobox,Jtable,Jframe,我的代码如下所示: private void populateRoleNameComboBox() { roleNameJComboBox.removeAllItems(); roleNameJComboBox.addItem(UserAccountDirectory.ADMIN_ROLE); roleNameJComboBox.addItem(UserAccountDirectory.SALESPERSON_ROLE); roleNameJComboBox.ad
private void populateRoleNameComboBox() {
roleNameJComboBox.removeAllItems();
roleNameJComboBox.addItem(UserAccountDirectory.ADMIN_ROLE);
roleNameJComboBox.addItem(UserAccountDirectory.SALESPERSON_ROLE);
roleNameJComboBox.addItem(UserAccountDirectory.SUPPLIER_ROLE);
roleNameJComboBox.addItem(UserAccountDirectory.CUSTOMER_ROLE);
//populatePersonNameComboBox();
}
private void populatePersonNameComboBox() {
personNameJComboBox.removeAllItems();
if(roleNameJComboBox.getSelectedItem().equals(UserAccountDirectory.SUPPLIER_ROLE)){
for(Supplier s : supplierDirectory.getSupplierList()){
personNameJComboBox.addItem(s);
}
}
else if(roleNameJComboBox.getSelectedItem().equals(UserAccountDirectory.SALESPERSON_ROLE)){
for(Person p : employeeDirectory.getSalesPersonList()){
personNameJComboBox.addItem(p);
}
}
else if(roleNameJComboBox.getSelectedItem().equals(UserAccountDirectory.CUSTOMER_ROLE)){
for(Person person : customerDirectory.getCustomerList()){
personNameJComboBox.addItem(person);
}
}
}
我的personNameJComboBox不显示任何值。如何实现基于第一个组合框获取第二个组合框的值的逻辑。下面是一个简单的示例。基本上,只要第一个组合框的选定项发生更改,就需要更改第二个组合框的模型
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 );
}
}
下面是一个简单的示例,让您开始学习。基本上,只要第一个组合框的选定项发生更改,就需要更改第二个组合框的模型
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 );
}
}
下面是一个简单的示例,让您开始学习。基本上,只要第一个组合框的选定项发生更改,就需要更改第二个组合框的模型
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 );
}
}
下面是一个简单的示例,让您开始学习。基本上,只要第一个组合框的选定项发生更改,就需要更改第二个组合框的模型
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 );
}
}
默认情况下,每个
JComboBox
都与DefaultComboxModel
关联,以添加项目或管理项目的状态更改。但是,如果要向jComboxModel
添加/删除项,则必须使用MutableComboxModel
的实现,并且DefaultComboxModel
也实现MutableComboxModel
我不认为您的
personNameJComboBox.addItem(item)
无法工作的任何原因,除非您尝试在不侦听RoleNameJomboBox
的操作事件的情况下填充数据,其中至少会选择on item。确保将ActionListener
与rolenamejombobox
一起使用,并实现actionPerformed()
函数,在该函数中应调用populatePersonNameComboBox()
功能。默认情况下,每个JComboBox
都与defaultComboxModel
关联,以添加项目或管理项目的状态更改。但是,如果要向jComboxModel
添加/删除项,则必须使用MutableComboxModel
的实现,并且DefaultComboxModel
也实现MutableComboxModel
我不认为您的
personNameJComboBox.addItem(item)
无法工作的任何原因,除非您尝试在不侦听RoleNameJomboBox
的操作事件的情况下填充数据,其中至少会选择on item。确保将ActionListener
与rolenamejombobox
一起使用,并实现actionPerformed()
函数,在该函数中应调用populatePersonNameComboBox()
功能。默认情况下,每个JComboBox
都与defaultComboxModel
关联,以添加项目或管理项目的状态更改。但是,如果要向jComboxModel
添加/删除项,则必须使用MutableComboxModel
的实现,并且DefaultComboxModel
也实现MutableComboxModel
我不认为您的
personNameJComboBox.addItem(item)
无法工作的任何原因,除非您尝试在不侦听RoleNameJomboBox
的操作事件的情况下填充数据,其中至少会选择on item。确保将ActionListener
与rolenamejombobox
一起使用,并实现actionPerformed()
函数,在该函数中应调用populatePersonNameComboBox()
功能。默认情况下,每个JComboBox
都与defaultComboxModel
关联,以添加项目或管理项目的状态更改。但是,如果要向jComboxModel
添加/删除项,则必须使用MutableComboxModel
的实现,并且DefaultComboxModel
也实现MutableComboxModel
我不认为您的personNameJComboBox.addItem(item)
无法工作的任何原因,除非您尝试在不侦听RoleNameJomboBox
的操作事件的情况下填充数据,其中至少会选择on item。确保将ActionListener
与rolenamejombobox
一起使用,并实现actionPerformed()
函数,在该函数中应调用populatePersonNameComboBox()
函数