如何更新2个JCombo Box

我有2个Jcombo Boxs:combo1和combo2

我选择combo1,我可以获得combo2的信息,但问题是我可以获得combo2的信息,但它没有更新。 我也尝试使用updata.UI(),但它没有帮助。

这是侧面的代码

public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); String uname1 = (String)cb.getSelectedItem(); combo2 = update(uname1); combo2.updateUI(); } 

这是更新内部的代码

 protected JComboBox update(String name) { JComboBox tmp = new JComboBox(); //Read Content from XML file (University is bigger than Year) NodeList nList = doc.getElementsByTagName("University"); System.out.println("Inside Fn " + name); for(int i = 0 ; i < nList.getLength();i++) { Element el = (Element)nList.item(i); if(name.contentEquals(el.getAttributeNode("name").getNodeValue())) { NodeList tmpyList = el.getElementsByTagName("Year"); for(int j = 0 ; j < tmpyList.getLength();j++) { Element yl = (Element)tmpyList.item(j); System.out.println(yl.getAttribute("yr")); tmp.addItem(yl.getAttribute("yr")); } } } return tmp; //Return ComboBox to combo2 } 

谢谢你的善意,我尝试使用你的代码,但它不起作用(它仍然没有更新),请帮助我

这是我的构造函数

 public JFrameExample() { String[] comboboxdefault = { "Select" }; JComboBox combo1 = Universitylist(); JComboBox combo2 = new JComboBox(comboboxdefault); JComboBox combo3 = new JComboBox(comboboxdefault); uList.addActionListener(this); yList.addActionListener(this); dList.addActionListener(this); JPanel student_information = new JPanel(new GridLayout(0,1)); uList.setName("University List"); yList.setName("Year List"); // University List student_information.add(combo1); // Database Year List student_information.add(combo2); // Programme List student_information.add(combo3); //Add Components to this container, using the default FlowLayout. add(student_information); } 

这是combo2更新它返回String Array

 protected String[] updateyList(String name) { String[] tmp = null; //Read from XML file for(int i = 0 ; i < nList.getLength();i++) { Element el = (Element)nList.item(i); if(name.contentEquals(el.getAttributeNode("name").getNodeValue())) { NodeList tmpyList = el.getElementsByTagName("Year"); tmp = new String[tmpyList.getLength()]; for(int j = 0 ; j < tmpyList.getLength();j++) { Element yl = (Element)tmpyList.item(j); //Add to String Array tmp[j] = yl.getAttribute("yr"); } } } return tmp; } 

在行动表演中

 public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); String uname1 = (String)cb.getSelectedItem(); System.out.println(cb.getName()); // To make sure I got the combo1. try { //I change to the model method DefaultComboBoxModel model = new DefaultComboBoxModel( updateyList(uname1) ); System.out.println(model.getSize()); combo2 = new JComboBox(); // If I don't have this line it will throw error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException combo2.setModel(model); } catch (ParserConfigurationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (SAXException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } 

这是用于创建GUIfunction

 private static void createAndShowLoginGUI() { //Create and set up the window. JFrame frame = new JFrame("Login"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. JFrameExample newContentPane = new JFrameExample(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } 

这是主要function

 public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowLoginGUI(); } }); } 

我想我做错了但我不知道在哪里

正如您现在所做的那样,每次都会重新创建combobox(通过在update方法中返回tmp)。 这似乎没有反映在UI中,也许其他人会发布原因。 但是,如果您可以更改以更新组合值(更改模型或删除当前值并添加新值)而不是重新创建,那么以下post可以帮助您动态更改JComboBox

无需使用updateUI()方法。

如果要更改第二个combobox中的数据,则应更改模型(不要创建新的combobox):

 comboBox2.setModel(...); 

它将自动重绘自己。 您可以创建DefaultComboBoxModel并将数据直接添加到它。

编辑:

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