Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners How to declare a Combobox without using a string in its declaration?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


An B
How to declare a Combobox without using a string in its declaration?
2 Answer(s)      3 years and a month ago
Posted in : Java Beginners


What i mean to ask is how can i declare a combobox first and initialise the values later?

For example

JComboBox x= new JComboBox(); ... String s={"Alpha","Beta","Gamma"}; ... setSomething.JComboBox(s);

Is it even possible? From all the examples and tutorials so far i haven't found any which tells me how to do this.

Help will be very much appreciated!


Advertisement
View Answers

February 2, 2012 at 8:51 PM


Thanks a lot !!



February 2, 2012 at 4:19 PM


import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class DynamicCombobox {

public static void main(final String args[])throws Exception {
JFrame frame = new JFrame();
frame.setLayout(null);
JLabel lab=new JLabel("Course");
        final JComboBox combo=new JComboBox();
        combo.addItem("--Select--");
        Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
           final Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from employee");

           while(rs.next()){
           combo.addItem(rs.getString("name"));
           }

    lab.setBounds(10,10,100,20);
    combo.setBounds(120,10,100,20);

    frame.add(lab);
    frame.add(combo);

    frame.setSize(300,100);
    frame.setVisible(true);
    }
}


Related Tutorials/Questions & Answers:
Advertisements
Advertisements
 

 

 

DMCA.com