JCombobox从mysql数据库更改另一个JCombobox值

时间:2014-04-08 10:08:36

标签: java mysql swing jcombobox

我使用此代码在数据库的搜索结果中显示组合框。

但我想要第二个组合框向我展示第一个组合子类别。

我该怎么做?

感谢您的帮助。

   private void FillComboTipoEmpresas2(){

    try{
        String sql="select * from tiposempresa";
        pst=(PreparedStatement) conexao.prepareStatement(sql);
        rs=pst.executeQuery();

        while(rs.next()){
            String tiposempresa = rs.getString("descTipoEmpresa");
            jComboBoxTipoEmpresas2.addItem(tiposempresa);
        }

           rs.close();
           pst.close();

}
catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}

}

  private void jComboBoxTipoEmpresasPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
String tmp = (String) jComboBoxTipoEmpresas.getSelectedItem();
 String sql = "select * from tiposempresa where descTipoEmpresa=?";

try{
    pst=(PreparedStatement) conexao.prepareStatement(sql);
    pst.setString(1, tmp);
    rs=pst.executeQuery();
    if(rs.next()){

        String add2 = rs.getString("idTiposEmpresa");
        jTTipoEmpresa.setText(add2);
    }



    rs.close();
           pst.close();
}
catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}
}

如果有人不理解这个问题,我会尝试更好地解释。

再次感谢

1 个答案:

答案 0 :(得分:1)

jComboBoxTipoEmpresas2.addItem(tiposempresa);
  

我需要第二个JComboBox,它只列出第一个Jcombobox的子类别......

看起来您只是添加了一个新项目。首先需要从循环外部的模型中删除所有现有项目,这些项目会向模型中添加新项目。

 comboBox.removeAllItem();

或者另一种方法是创建新模型并替换现有模型。下面是一个示例,说明如何使用硬编码模型完成此操作:

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