jComboBox with database
Hello friends,
I have created three JComboBoxes. first two of them get list from string. However I select these two comboboxes, the third combobox automatically retrieve data of a particular column from my database. But in my code its only get data from first item which automatically seleted by setmodel method. Here I paste the code . Please suggest where I stucked!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* frame1.java
*
* Created on Oct 25, 2012, 2:00:29 PM
*/
package smit.cse;
import java.sql.*;
/**
*
* @author MYPC
*/
public class frame1 extends javax.swing.JFrame {
/** Creates new form frame1 */
public frame1() {
initComponents();
String str=(String)jComboBox1.getSelectedItem();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:feeds");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select * from staffs where dept_name like'"+str+"'");
while(rs.next()){
String str1=rs.getString("subject");
System.out.println(str);
jComboBox2.addItem(str1);
}
}
catch(Exception e){}
}
/** 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() {
jComboBox1 = new javax.swing.JComboBox();
jComboBox2 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "EEE", "ETC", "CSE" }));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(95, 95, 95)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(125, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(181, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frame1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
// End of variables declaration
}
View Answers
October 25, 2012 at 6:29 PM
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* frame1.java
*
* Created on Oct 25, 2012, 2:00:29 PM
*/
package smit.cse;
import java.sql.*;
/**
*
* @author MYPC
*/
public class frame1 extends javax.swing.JFrame {
/** Creates new form frame1 */
public frame1() {
initComponents();
String str=(String)jComboBox1.getSelectedItem();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:feeds");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select * from staffs where dept_name like'"+str+"'");
while(rs.next()){
String str1=rs.getString("subject");
System.out.println(str);
jComboBox2.addItem(str1);
}
}
catch(Exception e){}
}
/** 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() {
jComboBox1 = new javax.swing.JComboBox();
jComboBox2 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "EEE", "ETC", "CSE" }));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(95, 95, 95)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(125, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(181, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new frame1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
// End of variables declaration
}
Related Tutorials/Questions & Answers:
jComboBox with databasejComboBox with database Hello friends,
I have created three JComboBoxes. first two of them get list from string. However I select these two... my
database. But in my code its only get data from first item which
Help on database and JComboBoxHelp on
database and JComboBox I want to select from the
JComboBox and when click onto the "search" button, the selected category (example "new york... the things from
database Advertisements
Link To Database with JComboBox - Java BeginnersLink To
Database with JComboBox sir As ur Source Code i have Load course names from Access
Database in to
JComboBox but now I want to do ,when i select Course name from
Jcombobox i want to display appropriate records relate
How to store JComboBox item into database How to store
JComboBox item into
database import... DefaultComboBoxModel(labels);
final JPanel TypeTF = new JPanel();
JComboBox comboBox1 = new
JComboBox(model);
comboBox1.setEditable(false
Database values in JComboBoxDatabase values in
JComboBox
In this section, you will learn how to display values in
JComboBox from
database. For this, we have allowed the user to enter... the
database will get
displayed on the ComboBox. If there will be no data
binding jComboBox to mysql database - Swing AWTbinding
jComboBox to mysql database I am using netbeans 6.5
How to populate
jComboBox with data of specific column of mysql
database table?
I...){
e.printStackTrace();
}
JComboBox comboTypesList = new
JComboBoxJComboBox I have
jcombobox. In which tha values are loaded from MySql
Database. My problem is that i want to load content of the jtable whenever i change the selected item. Please some one help me to do this.
Thank you
JComboBox have
jcombobox on Jframe form of IDE. In which the values are to be loaded from MySql
Database. My problem is that i want to load content of the jtable whenever i change the selected item in
JComboBox.
Thank you.
(adsbygoogle
JComboBoxJComboBox I want to change the index of one
jComboBox according to the selected index of another
jComboBox. I can do it.Also the user should have the ability to select or change the index of the second combobox according
jComboBoxjComboBox I want to change the index of one
jComboBox according to the selected index of another
jComboBox. I can do it.Also the user should have the ability to select or change the index of the second combobox according
jcomboboxjcombobox hi i have developed a form and i have
jcombobox in which data is retrieved from ms access but the problem is that if we pressed the down key the last 5 data are not showed only the other data can be pressed
JcomboboxJcombobox Hii,
I am doing my final year project and i am using java swing as a front end.I have used
Jcombobox for displaying my on bluetooth devices and i also made a refresh button.when i click on the refresh button i
issue on jcomboboxissue on jcombobox i have JTextfield and
JComboBox. there are several values in combobox.when i select a value from combobox how to make textfiled as a combobox.only few values in the combobox need this functionality. need
JComboBox with AccessDatabase - Java BeginnersJComboBox with AccessDatabase Hello Sir I have Created Course... on Student Admission Form
when I select Course Name from
JComboBox on Student...");
JComboBox combo1=new
JComboBox();
JComboBox combo2=new
JComboBox About jcombobox - Swing AWTAbout jcombobox Hi all,
I am new to this forum.
Can we do auto suggest
jcombobox in swing? If yes is there any jar file for that or any code... JTextField tf;
private final
JComboBox combo = new
JComboBox();
private
JComboBox Event Handling - Java BeginnersJComboBox Event Handling Hi,
I have problem with event handling here.There are Two
JComboBox and a JTextField.When I select an item in first... Compare extends JFrame implements ItemListener
{
JComboBox combo,lcombo
JComboBox Display Problem - Java BeginnersJComboBox Display Problem I am create one program that contain two...,ItemListener
{
Container c;
JLabel lblstart,lblend;
JComboBox cmbstart...");
lblend=new JLabel("End Date");
cmbstart=new
JComboBox();
cmbend=new
Create a JComboBox Component in Java
Create a
JComboBox Component in Java
In this section, you will learn about the
JComboBox Component
of swing in java. The
JComboBox is used to display drop-down list
easy way to make a JCombobox look like a JTextField?easy way to make a
JCombobox look like a JTextField? Is there a good(and easy) way to make a
JCombobox look like a JTextField? By this I mean there should not be a dropdown button, but when the user enters something it should
databasedatabase the code for initializing the
database connection
databasedatabase i want to let the user select the name of
database and then delete that
database.......im doing project in swings netbeans
How to Add JComboBox on JPanel - Java BeginnersHow to Add
JComboBox on JPanel How to Add
JComboBox on JPanel
D:\java>javac ViewElements.java
ViewElements.java:181: expected...);
JLabel lbl4=new JLabel("Branch");
final
JComboBox jc=new
JComboBox();
jc.addItem
databasedatabase use of hyperlink to show the data from
database databasedatabase i need to insert time in my
database. i need a code to insert time in my
database. its very urgent
JComboBox Insert Edited Value Into TableJComboBox Insert Edited Value Into Table
In this section we will read about how to make
JComboBox an editable and then
how to insert the new edited value... in to insert an
editable value of
JComboBox into a table. We will use an Java editor
databasedatabase im doing my project in netbeans swings...wn a user wants to create a
database from the gui...i want to display an error msg if a
database with that name already exists and if it does not exist new
database should
databasedatabase tell me use about
database and give me a small program.
It is secure and can easily be accessed, managed, and updated. Moreover... links:
Connect JSP with
database Mysql
Connect Java with
database Mysql
databasedatabase tell me use about
database and give me a small program.
It is secure and can easily be accessed, managed, and updated. Moreover... links:
Connect JSP with
database Mysql
Connect Java with
database Mysql
databasedatabase tell me use about
database and give me a small program.
It is secure and can easily be accessed, managed, and updated. Moreover... links:
Connect JSP with
database Mysql
Connect Java with
database Mysql
databasedatabase I wanted to know if it is possible to create a
database in mysql by letting the user enter the name of the
database in swing gui..im doing my project in netbeans...Thank You
databasedatabase I wanted to know if it is possible to establish
database connection on a remote pc with mysql using java swings from netbeans and then create a
database on the remote pc....
Kindly help me
DATABASEDATABASE How can i get combo box values from
database??
or how can i get values in the drop down menu of the html which is similar to dat of combo box in java - from
database DatabaseDatabase How to represent data from
database in a tree?
Please visit the following link:
Retrieve data from
database in tree using Java Swing
DatabaseDatabase I am working in jsf2.Fro a demo i created
database in ms access.no wi want to retrive a record from
database for a particular data.I need help as I am stuck
DatabaseDatabase from java code i have to retrieve some data from a
database, which is present in a different timezone.
Scenario :- My
database... in
database timezone is 9PM 15APR2012. I want to retrieve something from
database DATABASEDATABASE I can't send different information from different form into a single table in my
database databasedatabase dateofbirth is not save into the
database while i am enter from html browser.it shows as null.whats the reason
databasedatabase sir..
how we can uplaod the
database in the data file(ms access)please send me a solution