How to declare a Combobox without using a string in its declaration?

Ads
 

How to declare a Combobox without using a string in its declaration?

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!

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);
    }
}

Ads









Related Tutorials/Questions & Answers:
How to declare a Combobox without using a string in its declaration?
how to reverse a string without changing its place
Advertisements
how to retrieve data from database using combobox value without using request.getParameter in jsp - JSP-Servlet
How to declare String array in Java?
how to start intergrated webcam of a laptop without calling its exe file using java only - Java Beginners
Java repeat string without using loops
Breaking a string into words without using StringTokenizer
String length without using length() method in java
Java reverse string without using inbuilt functions
Declare string array in Java (one and two dimensional)
How to insert data from a combobox and textbox values into DB using JSP?
How to make addition of two combobox values using jsp and javascript?
how to delete a row in sql without using delete command.
how to validate the telephone number without using jquery in html with javascript
How to declare array in Java?
How to declare NSMatrix
how can i achieve multiple inheritance in java without using inheritance ?
how to build a colorful clock in JSP without using Flash.
how to access the object of one frame on clicking a button without using this keyword
print reverse string without api
Jdbc Login Page Validation using Combobox
how to use an editable combobox
How to split string in Java using comma?
how to count words in string using java
How to pass and catch HTML parameters to a Java program using REST services?(without using servlet/jsp)
without using built-in functions
How to make a CRUD without using SQL Server? by just using your GUI? (CRUD = Creating, Register, Update, Delete)
How to Declare Abstract Class in PHP ?
Without Using Form in Java Script
How to Declare fflush() Function in PHP
how to convert xml string to soap request object using axis1.4?

Ads