Question

I use this code to display combobox in the search result in the database.

But I wanted a second combobox to show me a subcategory of the first.

How can I do this?

Thanks for help.

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

If someone doesnt understand the question I try to explain better.

Thanks again

Was it helpful?

Solution

jComboBoxTipoEmpresas2.addItem(tiposempresa);

I need a second JComboBox who list only the subcategory of the first Jcombobox...

It looks like you are just adding a new item. You first need to remove all the existing items from the model outside of the loop that adds new items to the model.

 comboBox.removeAllItem();

Or the other approach is to create a new model and replace the existing model. Here is an example that show how this is done with hardcoded models:

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();
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top