【问题标题】:JCombobox change another JCombobox values from mysql databaseJCombobox 从 mysql 数据库更改另一个 JCombobox 值
【发布时间】:2014-04-08 10:08:35
【问题描述】:

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

但我想要第二个组合框来显示第一个的子类别。

我该怎么做?

感谢您的帮助。

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

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

再次感谢

【问题讨论】:

  • 看来您已经有了解决方案。问题是什么 ? (顺便说一句,你真的应该使用 SwingWorker 在不同的线程中从数据库中获取数据,并且只有在完成后才更新组合框)。
  • 你的代码有什么问题?好像你填了1个JComboBox,然后用选择的值填了第二个JComboBox,不是吗?
  • 感谢大家的回复...第二个代码是解析值到jtextfield。我需要第二个 JComboBox,它只列出第一个 Jcombobox 的子类别...
  • 有人能帮帮我吗?

标签: java mysql swing jcombobox


【解决方案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();
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多