Having Problem with my JComboBox
I'm trying to populate my JComboBox with data in my database with the use of arraylist but sadly there must be a mistake in my code can some help me Please and btw my connection is working well.
In my table (tblcuri) i have 3 fields (fldCurId,fldSy,fldDescp). i want to fldDescp to be displayed in my JComboBox but i don't know how.. Please Help me i'm only a 3day old java use
---------------------------------------------------------------------------------
Code in my dbFunction class:
public List getCurriculum() {
ResultSet rsRec;
List curriculum = new ArrayList();
String sql;
sql = "select * from tblcuri order by fldDescp";
try {
rsRec = st.executeQuery(sql);
while (rsRec.next()) {
curriculum.add(rsRec.getString("fldDescp"));
}
rsRec.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return curriculum;
}
----------------------------------------------------------------------------------------------
Code: (student Jframe Class) name of the JCombox(cbCurr)
public student() {
initComponents();
List cList = dbFunctions.getCurriculum();
cbCurr = new JComboBox();
Iterator<List> iterator = cList.iterator();
while (iterator.hasNext()) {
List list = iterator.next();
cbCurr.addItem(iterator.next());
}
}
Re: Having Problem with my JComboBox
For future reference, please flank you code with the [highlight=java][/highlight] tags or code tags. There are a few problems with the code you posted. It boils down to what you retrieve from the database and what the client tries to use. I presume you wish to add Strings to your JComboBox...your dbFunction retrieves a list of String values and places them in a list, but the client (JFrame class) tries to retrieve a List containing Lists. Using Generics will better enforce these constraints at compile time.
Code :
Iterator<String> iterator = cList.iterator();
while (iterator.hasNext()) {
String string = iterator.next();
Re: Having Problem with my JComboBox
Sorry for not highlighting my code and Thank you sir for the fast respond i already change it but my JComboBox doesn't show anything only a blank Jcombobox sir
DbFunction Class
Code java:
import java.util.*;
import java.sql.*;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
public class DbFunctions {
private static Connection dbConn;
private static Statement st;
private static String dbURL = "jdbc:odbc:studODBC";
private static String dbUsername = "root";
private static String dbPassword = "";
public DbFunctions() {
DbFunctions.connectDB();
}
public static boolean connectDB() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbConn = DriverManager.getConnection(dbURL, dbUsername, dbPassword);
st = dbConn.createStatement();
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
public List getCurriculum() {
ResultSet rsRec;
List curriculum = new ArrayList();
String sql;
sql = "select * from tblcuri order by fldDescp";
try {
rsRec = st.executeQuery(sql);
while (rsRec.next()) {
/* List row = new ArrayList();
row.add(rsRec.getString("fldCurId"));
row.add(rsRec.getString("fldSy"));
row.add(rsRec.getString("fldDescp"));
*/
curriculum.add(rsRec.getString("fldDescp"));
}
rsRec.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
return curriculum;
}
Student (Jframe) class
Code java:
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.table.DefaultTableModel;
import java.util.*;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.*;
public class student extends javax.swing.JFrame {
/** Creates new form student */
public student() {
initComponents();
List cList = dbFunctions.getCurriculum();
cbCurr = new JComboBox();
Iterator<String> iterator = cList.iterator();
while (iterator.hasNext()) {
String string = iterator.next();
cbCurr.addItem(iterator.next());
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new student().setVisible(true);
}
});
}
}
Re: Having Problem with my JComboBox
There are missing portions of your code. It helps both you and us to boil the problem down to a short, concise, and compilable example that can demonstrate the problem. I'd suggest first playing with the JComboBox, adding 'dummy' values to verify it is working in the context you have. Then expand that to incorporate you database values. When in doubt, add some System.out.println statements to make sure the values are what you expect. In other words, one of the keys to debugging is to break the problem down into individual components, get them to work, then expand them into each other
Re: Having Problem with my JComboBox
Thank Very Much sir my JcomboBox is working now thanks again