Results 1 to 11 of 11
  1. #1
    nayeem is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default Loading Values into JComboBox, depending on selection from another JComboBox?

    Loading Values into JComboBox, depending on selection from another JComboBox?
    if i hava a jcombobox value with Division likeDhaka,Rajshahi,Rangpur.then i want to load another combobox ciity values like Motijheel,Mirpur,Rajbari .if i select Dhaka from the first jcombobox then automatically the city Motijheel will be selected in the second jcombobox.I need detail explaination about this kind of programming.I am now learning java programming from Scratch.
    Attached Thumbnails Attached Thumbnails Loading Values into JComboBox, depending on selection from another JComboBox?-untitled.png  
    Last edited by nayeem; 11-14-2016 at 09:32 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Loading Values into JComboBox, depending on selection from another JComboBox?

    Add an ActionListener to the first JComboBox. Everytime an item is selected, the actionPerformed method is executed, and you can take the value of the first one and use that to populate the second.
    Java Code:
    package forums.ui;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    import java.util.List;
    
    public class TwoComboBox {
        private static Map<String, java.util.List<String>> values;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    values = new HashMap<>();
    
                    values.put("Fruit", Arrays.asList("Apple", "Banana", "Lemon", "Cherry"));
                    values.put("Vegetable", Arrays.asList("Carrot", "Cauliflower", "Brussels Sprouts"));
                    values.put("Drinks", Arrays.asList("Beer", "Lemonade", "Wine"));
    
                    JFrame frame = new JFrame();
                    Container contentPane = frame.getContentPane();
                    contentPane.setLayout(new GridLayout(2, 1, 0, 10));
    
                    JComboBox<String> first = new JComboBox<>(values.keySet().toArray(new String[values.keySet().size()]));
                    first.setSelectedItem(null);
                    JComboBox<String> second = new JComboBox<>(new DefaultComboBoxModel<>());
    
                    first.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            String value = (String) first.getSelectedItem();
                            List<String> secondValues = values.get(value);
    
                            DefaultComboBoxModel model = (DefaultComboBoxModel) second.getModel();
                            model.removeAllElements();
                            for (String s : secondValues) {
                                model.addElement(s);
                            }
                        }
                    });
                    contentPane.add(first);
                    contentPane.add(second);
    
                    frame.pack();
                    frame.setLocationRelativeTo(null);
    
                    frame.setVisible(true);
                }
            });
    
        }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    nayeem is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default How to select two jcombobox values automatically by a combobox

    HOW TO SELECT TWO JCOMBOBOX VALUES AUTOMATICALLY BY A JCOMBOBOX.
    I AM USING THIS KIND OF CODING.
    String[] array= { "name1","name2" };
    for (int i = 0; i < countries.length; i++) {
    comboBox.addItem(countries[i]);
    then if you would like to reset the combo box you have to call the array again then use this code:

    for (int i = 0; i < array.length; i++) {
    //here you can give your combo the number of index
    comboBox.setSelected Index(0);
    comboBox.add Item(countries[i]);
    }
    IF IT IS POSSIBLE GIVE ME SOME EXAMPLE BY USING THIS KIND OF PROGRAMMING FORMAT.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: How to select two jcombobox values automatically by a combobox

    Can you explain in more detail what you are trying to do?
    How many JComboBox instances do you have? What do you want to do it when items are selected in those combo boxes?

    Please release the CAPS LOCK when typing. All CAPS is very annoying to read.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    nayeem is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default hOW CAN I CHANGE THE VALUE AUTOMATICALLY WHEN SELECT 2ND jCOMBOBOX THE 3RD J COMBOB

    NOW I AM CHANGING 2ND COMBOBOX SEMESTER VALUE BY SELECTING 1ST COMBOBOX DEPERTMENT VALUE.WHEN I SELECT DEPERTMENT VALUE THEN THE SEMESTER VALUE IS CHANGED AUTOMATICALLY.NOW IF I WANT TO SELECT 3RD COMBOBOX BOOK VALUE WILL BE CHANGE WHEN THE SEMESTER VALUE IS CHANGED BY 1ST jCOMBOBOX DEPERTMENT VALUE IS SELECTED BY ME.I NEED SOME EXAMPLE.I WANT TO CONTROL 3RD JCOMBOBOX VALUE BY THE SELECTION FROM 2ND JCOMBOBOX VALUE WHICH IS CHANGED BY 1ST JCOMBOBOX.
    MY CODES RE HERE:
    String[] dep={"Select Depaartment","Computer","Civil","Electrical","Tele com"};
    String[] Computer={"FirststSemester","SecondndSemester","Th irdrdSemester","FourthSemester","FifthSemester","S ixthSemester","SeventhSemester"};
    String[] Civil={"FirststSemester","SecondndSemester","Third rdSemester","FourthSemester","FifthSemester","Sixt hSemester","SeventhSemester"};
    String[] Electrical={"FirststSemester","SecondndSemester"," ThirdrdSemester","FourthSemester","FifthSemester", "SixthSemester","SeventhSemester"};
    String[] Telecom={"FirststSemester","SecondndSemester","Thi rdrdSemester","FourthSemester","FifthSemester","Si xthSemester","SeventhSemester"};





    cmbDep.removeAllItems();

    for (int i = 0; i < dep.length; i++) {
    cmbDep.addItem(dep[i]);
    }

    private void cmbDepItemStateChanged(java.awt.event.ItemEvent evt) {
    // TODO add your handling code here:
    cmbSems.removeAllItems();

    if(cmbDep.getSelectedIndex() !=0)
    {
    String[] c;

    if (cmbDep.getSelectedIndex()==1)
    {
    c=Computer;
    }
    else if(cmbDep.getSelectedIndex()==2)
    {
    c =Civil;
    }
    else if(cmbDep.getSelectedIndex()==3)
    {
    c=Electrical;
    }


    else
    {
    c=Telecom;
    }

    for(int i=0; i<c.length; i++)
    {
    cmbSems.addItem(c[i]);
    }







    }



    }
    Attached Thumbnails Attached Thumbnails Loading Values into JComboBox, depending on selection from another JComboBox?-untitled.png  

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Loading Values into JComboBox, depending on selection from another JComboBox?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Threads merged. Please stop making duplicate posts.

    Please release the CAPS LOCK when typing. All CAPS is very annoying to read.
    Last edited by Norm; 11-17-2016 at 10:54 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Loading Values into JComboBox, depending on selection from another JComboBox?

    I gave you a perfectly working example. If you don't understand parts of it, just ask.

    When adding a third JComboBox box, the procedure is the same. You can add 100 JComboBoxes and it's all the same procedure.

    And I'm with Norm, if you don't stop using all caps and refuse to use code tags like he instructed, I am going to ignore your posts.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    nayeem is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default How to use populated jcombobox for selecting another jcombobox values?

    How to use populated jcombobox for selecting another jcombobox values?
    i have 3 j combobox for Depertment,Semester and Books.I populate the Semester Jcombobox by Depertment jcombobox.now i want to use Semester jCombobox to populate the Books Jcombobox.Need some Example.
    my codes are here:
    Java Code:
    import java.sql.Connection;
    import java.sql.*;
    import javax.swing.JOptionPane;
     
    /**
     *
     * @author gsl
     */
    public class Employee extends javax.swing.JInternalFrame {
     
         
        String gender="";
        int id;
         
      String[] dep={"Select Depaartment","Computer","Civil","Electrical","Telecom"};
        String[] Computer={"FirststSemester","SecondndSemester","ThirdrdSemester","FourthSemester","FifthSemester","SixthSemester","SeventhSemester"};
        String[] Civil={"FirststSemester","SecondndSemester","ThirdrdSemester","FourthSemester","FifthSemester","SixthSemester","SeventhSemester"};
        String[] Electrical={"FirststSemester","SecondndSemester","ThirdrdSemester","FourthSemester","FifthSemester","SixthSemester","SeventhSemester"};
        String[] Telecom={"FirststSemester","SecondndSemester","ThirdrdSemester","FourthSemester","FifthSemester","SixthSemester","SeventhSemester"};
         
         
      String[] books={"Select Books", "FirststSemesterBooks","SecondSemesterBooks","ThirdSemesterBooks","FourthSemesterBooks","FifthSemesterBooks","SixthSemesterBooks","SeventhSemesterBooks"}; 
       
         
        
         
         
       //driver under class  
         
        static final String JDBC_Driver="con.mysql.jdbc.Driver";
        static final String Db_URL="jdbc:mysql://localhost/employee_sys";
         
        static final String User="root";
        static final String pass= "";
        
    public static Connection myconnection=null; 
    public static Statement dstatement=null;
     
     
         
        public Employee() {
               try {
             //Registaring Driver
             Class.forName("com.mysql.jdbc.Driver");
             // Open Connection
             myconnection=DriverManager.getConnection(Db_URL, User, pass);
             dstatement = myconnection.createStatement();
              
           }
                    
     catch(Exception ex){
          
         System.out.println(ex.getMessage());
         
      
      
     }      
            initComponents();
             
             
             
      //under in components
        
            
       cmbDep.removeAllItems();
     
            for (int i = 0; i < dep.length; i++) {
                 cmbDep.addItem(dep[i]);
            }
       
     
             }
     
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            txtName = new javax.swing.JTextField();
            txtAdrs = new javax.swing.JTextField();
            txtPhone = new javax.swing.JTextField();
            txtSalary = new javax.swing.JTextField();
            jLabel5 = new javax.swing.JLabel();
            cmbDep = new javax.swing.JComboBox<>();
            jLabel6 = new javax.swing.JLabel();
            cmbSems = new javax.swing.JComboBox<>();
            jLabel7 = new javax.swing.JLabel();
            cmbPos = new javax.swing.JComboBox<>();
            jLabel8 = new javax.swing.JLabel();
            btnMale = new javax.swing.JRadioButton();
            btnFemale = new javax.swing.JRadioButton();
            jPanel2 = new javax.swing.JPanel();
            btnSave = new javax.swing.JButton();
            btnSearch = new javax.swing.JButton();
            btnUpdate = new javax.swing.JButton();
     
            setClosable(true);
            setIconifiable(true);
            setMaximizable(true);
     
            jPanel1.setBackground(new java.awt.Color(102, 102, 255));
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("EmployeeInfo"));
     
            jLabel1.setText("Name");
     
            jLabel2.setText("Address");
     
            jLabel3.setText("Phone");
     
            jLabel4.setText("Salary");
     
            jLabel5.setText("Depertment");
     
            cmbDep.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
            cmbDep.addItemListener(new java.awt.event.ItemListener() {
                public void itemStateChanged(java.awt.event.ItemEvent evt) {
                    cmbDepItemStateChanged(evt);
                }
            });
     
            jLabel6.setText("Semester");
     
            cmbSems.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
     
            jLabel7.setText("Books");
     
            cmbPos.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item1", "Item2", "Item3", "Item4" }));
     
            jLabel8.setText("Gender");
     
            btnMale.setText("Male");
            btnMale.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnMaleActionPerformed(evt);
                }
            });
     
            btnFemale.setText("Female");
            btnFemale.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnFemaleActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(128, 128, 128)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1)
                        .addComponent(jLabel3)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel5)
                                .addComponent(jLabel7)
                                .addComponent(jLabel8)
                                .addComponent(jLabel4))))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addGap(38, 38, 38)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtSalary, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtPhone, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(btnMale, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(btnFemale, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                                .addComponent(cmbDep, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cmbSems, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cmbPos, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtAdrs)
                                .addComponent(txtName))))
                    .addContainerGap(75, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(33, 33, 33)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(txtAdrs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(27, 27, 27)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(37, 37, 37)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel4)
                        .addComponent(txtSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(27, 27, 27)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(cmbDep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(30, 30, 30)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(cmbSems, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(23, 23, 23)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(cmbPos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(26, 26, 26)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel8)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnMale)
                            .addComponent(btnFemale)))
                    .addContainerGap(15, Short.MAX_VALUE))
            );
     
            jPanel2.setBackground(new java.awt.Color(255, 255, 102));
     
            btnSave.setText("save");
            btnSave.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnSaveActionPerformed(evt);
                }
            });
     
            btnSearch.setText("search");
            btnSearch.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnSearchActionPerformed(evt);
                }
            });
     
            btnUpdate.setText("update");
            btnUpdate.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btnUpdateActionPerformed(evt);
                }
            });
     
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(94, 94, 94)
                    .addComponent(btnSave)
                    .addGap(38, 38, 38)
                    .addComponent(btnSearch)
                    .addGap(34, 34, 34)
                    .addComponent(btnUpdate)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnSave)
                        .addComponent(btnSearch)
                        .addComponent(btnUpdate))
                    .addContainerGap(24, Short.MAX_VALUE))
            );
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>                        
     
        private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
            // TODO add your handling code here:
             
       try{
            
           String  name=txtName.getText();
           String  add=txtAdrs.getText();
           String  phone=txtPhone.getText();
           double  salary=Double.parseDouble(txtSalary.getText());
           String  dis=cmbDep.getSelectedItem().toString();
           String  city=cmbSems.getSelectedItem().toString();
           String  pos=cmbPos.getSelectedItem().toString();
             
                    
                    
           String sql="insert into  employee(name,address,phone,salary,district,city,position,gender) values ('"+name+"','"+add+"','"+phone+"',"+ salary+",'"+dis+"','"+city+"','"+pos+"','"+gender+"')";
                 dstatement.executeUpdate(sql);
                  
                 JOptionPane.showMessageDialog (null,"Added Successful!");
             } catch (SQLException ex) {
                 JOptionPane.showMessageDialog (null,ex.getMessage()+ "Added Failed!");
                  
             }      
             
     
             
        }                                       
     
        private void btnMaleActionPerformed(java.awt.event.ActionEvent evt) {                                        
            // TODO add your handling code here:
            btnFemale.setSelected(false);
            gender="Male";  
        }                                       
     
        private void btnFemaleActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here:
            btnMale.setSelected(false);
            gender="Female";
        }                                         
     
        private void cmbDepItemStateChanged(java.awt.event.ItemEvent evt) {                                        
            // TODO add your handling code here:
              cmbSems.removeAllItems();
             
            if(cmbDep.getSelectedIndex() !=0)
            {
                String[] c;
                 
                if (cmbDep.getSelectedIndex()==1)
                {
                    c=Computer;
                }
                else if(cmbDep.getSelectedIndex()==2)
                {
                    c =Civil;
                }
                else if(cmbDep.getSelectedIndex()==3)
                {
                    c=Electrical;
                }
                
                 
                else
                {
                    c=Telecom;
                }
                 
                for(int i=0; i<c.length; i++)
                {
                    cmbSems.addItem(c[i]);
                }
                 
                 
          
                 
                 
                 
                 
              }
        
        
             
             
        }                                       
     
        private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here:
            try{
             
            String name=txtName.getText();
            String sql="select* from  employee where name='"+name+"'";
            ResultSet rs=dstatement.executeQuery(sql);
             
            rs.first();
            id=rs.getInt("id");
            String sname=rs.getString("name");
            String saddress=rs.getString("address");
            String sphone=rs.getString("phone");
            double ssalary=rs.getDouble("salary");
            String sdistrict=rs.getString("district");
            String scity=rs.getString("city");
            String sposition =rs.getString("position");
            String sgender=rs.getString("gender");
             
            txtName.setText(sname+"");
            txtAdrs.setText(saddress+"");
            txtPhone.setText(sphone+"");
            txtSalary.setText(ssalary+"");
           cmbDep.setSelectedItem(sdistrict+"");
           cmbSems.setSelectedItem(scity+"");
           cmbPos.setSelectedItem(sposition+"");
            
           if (sgender.equals("Female")){
                btnMale.setSelected(false);
                btnFemale.setSelected(true);
            } else {
                btnFemale.setSelected(false);
                btnMale.setSelected(true);
            }
            
      
          JOptionPane.showMessageDialog(null,"Data Found");
             
          }catch(SQLException ex){
           
           
         JOptionPane.showMessageDialog(null,"Data Not Found"+ex.getMessage());
           
          }
     
        }                                         
     
        private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here:
             
         try{
            
           String  name=txtName.getText();
           String  add=txtAdrs.getText();
           String  phone=txtPhone.getText();
           double  salary=Double.parseDouble(txtSalary.getText());
           String  dis=cmbDep.getSelectedItem().toString();
           String  city=cmbSems.getSelectedItem().toString();
           String  pos=cmbPos.getSelectedItem().toString();
             
            
           String sqlupdate="update   employee set name='"+name+"',address='"+add+"',phone='"+phone+"',salary="+salary+", district='"+ dis+"',city='"+city+"',position='"+pos+"',gender='"+gender+"' where id ="+ id;
                 dstatement.executeUpdate(sqlupdate);
                  
                 JOptionPane.showMessageDialog(null, "Updated Successfully");
             } catch (SQLException ex) {
             JOptionPane.showMessageDialog(null, "Updated Failed!"+"/n"+ex.getMessage());    
                  
             }    
     
        }                                         
     
     
        // Variables declaration - do not modify                     
        private javax.swing.JRadioButton btnFemale;
        private javax.swing.JRadioButton btnMale;
        private javax.swing.JButton btnSave;
        private javax.swing.JButton btnSearch;
        private javax.swing.JButton btnUpdate;
        private javax.swing.JComboBox<String> cmbDep;
        private javax.swing.JComboBox<String> cmbPos;
        private javax.swing.JComboBox<String> cmbSems;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JLabel jLabel8;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JTextField txtAdrs;
        private javax.swing.JTextField txtName;
        private javax.swing.JTextField txtPhone;
        private javax.swing.JTextField txtSalary;
        // End of variables declaration                   
    }
    Attached Thumbnails Attached Thumbnails Loading Values into JComboBox, depending on selection from another JComboBox?-jcombobox.png  
    Last edited by Norm; 11-18-2016 at 01:33 PM. Reason: Changed code tags <> to []

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Loading Values into JComboBox, depending on selection from another JComboBox?

    See post#2 for an example

    Threads Merged
    Last edited by Norm; 11-18-2016 at 01:35 PM.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    nayeem is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default How to solve this Jcomboboxes problem for library management system.

    i am now working with three combobox.if i select compurter from first combobox depertment then in the second combobox it is select all the semesters.
    And the code is working perfectly.
    Java Code:
     String[] dep={"Select Depertment","Compurter","Civil","Electrical","Telecom"};
     String[] Compurter={"FirstSemester","SecondSemester","ThirdSemester","FourthSemester","FifthSemester","SixthSemester","SeventhSemester"};
        String[] Civil={"FirstSemester","SecondSemester","ThirdSemester","FourthSemester","FifthSemester","SixthSemester","SeventhSemester"};
      String[] Electrical={"FirstSemester","SecondSemester","ThirdSemester","FourthSemester","FifthSemester","SixthSemester","SeventhSemester"};
      String[] Telecom={"FirstSemester","SecondSemester","ThirdSemester","FourthSemester","FifthSemester","SixthSemester","SeventhSemester"};
        
    divcomboDepertment.removeAllItems();
            
            for(int i=0; i< dep.length;i++)
            {
            divcomboDepertment.addItem(dep[i]);
            }   
    
     private void divcomboDepertmentItemStateChanged(java.awt.event.ItemEvent evt) {                                                    
            // TODO add your handling code here:
        divcomboSemester.removeAllItems();
            
            if(divcomboDepertment.getSelectedIndex() !=0)
            {
                String[] c;
                
                if (divcomboDepertment.getSelectedIndex()==1)
                {
                    c=Compurter;
                }
                else if(divcomboDepertment.getSelectedIndex()==2)
                {
                    c =Civil;
                }
                else if(divcomboDepertment.getSelectedIndex()==3)
                {
                    c=Electrical;
                }
               
               else 
                {
                    c=Telecom;
                }
                
                for(int i=0; i<c.length; i++)
                {
                    divcomboSemester.addItem(c[i]);
                }
            }
    but now after select the semesters i want to select again all the books from book combobox by select all semesters one by one and when i click a semester all the semester book will appear in the book combobox.i am coding like this for this part of program.
    Java Code:
    for(int i=0; i< Compurter.length;i++)
            {
            divcomboBook.addItem(Compurter[i]);
            }       
    private void divcomboSemesterItemStateChanged(java.awt.event.ItemEvent evt) {                                                  
            // TODO add your handling code here:
              divcomboBook.removeAllItems();
            
            if(divcomboSemester.getSelectedIndex() !=0)
            {
                String[] b;
                
                if (divcomboSemester.getSelectedIndex()==1)
                {
                    b=FirstSemester;
                }
                else if(divcomboSemester.getSelectedIndex()==2)
                {
                    b =SecondSemester;
                }
                else if(divcomboSemester.getSelectedIndex()==3)
                {
                    b=ThirdSemester;
                }
               else if(divcomboSemester.getSelectedIndex()==4)
                {
                    b=FourthSemester;
                }
               else if(divcomboSemester.getSelectedIndex()==5)
                {
                    b=FifthSemester;
                }
               else if(divcomboSemester.getSelectedIndex()==6)
                {
                    b=SixthSemester;
                }
               else 
                {
                    b=SeventhSemester;
                }
                
                for(int i=0; i<b.length; i++)
                {
                    divcomboBook.addItem(b[i]);
                }
            }     
            
        }
    But this part of this program is not working perfectly.when i select computer from depertment combobox then semester is selecting perfectly.but after select the semester the books are not loading perfectly in the third book combobox.now how can i fix this problem.
    Attached Thumbnails Attached Thumbnails Loading Values into JComboBox, depending on selection from another JComboBox?-untitled.png  
    Last edited by Norm; 11-19-2016 at 09:17 PM. Reason: Changed ending code tag \ to /

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Loading Values into JComboBox, depending on selection from another JComboBox?

    Threads merged again. This is the last one I'm doing. Next time the extra thread will be removed.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. JComboBox change text but keep the values
    By Xzar in forum New To Java
    Replies: 4
    Last Post: 11-14-2012, 11:38 PM
  2. Replies: 1
    Last Post: 08-08-2012, 05:20 AM
  3. Showing different values in JComboBox inside JTable
    By d3n1s in forum Advanced Java
    Replies: 2
    Last Post: 05-02-2011, 08:25 PM
  4. Replies: 1
    Last Post: 11-23-2010, 06:26 PM
  5. Replies: 2
    Last Post: 12-10-2009, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •