Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
TDD for a Shopping Website LiveProject
this week in the
Testing
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forums:
Swing / AWT / SWT
Beginning Java
How to use populated jcombobox for selecting another jcombobox values?
md nayeem
Greenhorn
Posts: 3
posted 5 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ 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 }
JCOMBOBOX.png
Rob Camick
Rancher
Posts: 3262
30
posted 5 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
What is the point of this question?
You were given a working example in your last question on this topic:
https://coderanch.com/t/672751/java/Loading-Values-JComboBox-depending-selection
Evil is afoot. But this tiny ad is just an ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How to add JTable into a JPanel
Socket Connection within same class
Pressing a button to open a new window
Trouble finding main
How do I display two columns in my jcombobox?
More...