JComboBox

Ads
 

JComboBox

I am using NetBeans IDE for developing application.

I 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.

View Answers

March 30, 2011 at 10:23 AM

import java.sql.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class  JComboBoxJTable{

    public static void main(String[] args) throws Exception{
        JFrame f=new JFrame();
        f.setLayout(null);
        JLabel lab=new JLabel("Select");
        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("id"));
           }

        ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        ItemSelectable is = (ItemSelectable)actionEvent.getSource();
        int ide=Integer.parseInt(selectedString(is));
        System.out.println("Selected: " + selectedString(is));
        try{
        ResultSet rs1= st.executeQuery("Select * from employee where id="+ide+"");
        String id="",name="",address="",contactNo="",email="";
        while(rs1.next()){
            id=rs1.getString(1);
            name=rs1.getString(2);
            address=rs1.getString(3);
            contactNo=rs1.getString(4);
            email=rs1.getString(5);
        }
    String data[][]={{id,name,address,contactNo,email}};
    String columnNames[]={"ID","Name","Address","Contact No","Email"};

    JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
JFrame fr=new JFrame();
fr.add(scrollPane);
fr.pack();
fr.setVisible(true);
   }
catch(Exception e){}
      }
    };
       combo.addActionListener(actionListener);
        lab.setBounds(20,20,100,20);
        combo.setBounds(120,20,80,20);
        f.add(lab);
        f.add(combo);

        f.setVisible(true);
        f.setSize(300,120);
    }
    static private String selectedString(ItemSelectable is) {
    Object selected[] = is.getSelectedObjects();
    return ((selected.length == 0) ? "null" : (String)selected[0]);
  } 
}

Ads









Related Tutorials/Questions & Answers:
JComboBox
JComboBox  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
jComboBox
jComboBox  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
Advertisements
JComboBox
JComboBox  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
jcombobox  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
JComboBox
have jcombobox on Jframe form of IDE. In which the values are to be loaded from... change the selected item in JComboBox. Thank you.   import java.sql....); JLabel lab=new JLabel("Select"); final JComboBox combo=new JComboBox
Jcombobox
Jcombobox  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 jcombobox
issue 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 database
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
JComboBox on JRadioButton - Java Beginners
JComboBox on JRadioButton  How to add JComboBox on Jpanel ,Give Me Sample Code
Help on database and JComboBox
Help on database and JComboBox  I want to select from the JComboBox and when click onto the "search" button, the selected category (example "new york") all the each new york picture and detail will be seen. But how to grab
problem with addactionlistener on JComboBox - Java Beginners
[] = { "A", "B", "C"}; JFrame frame = new JFrame(); JComboBox combo = new JComboBox(st); frame.add(combo); ActionListener actionListener = new
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
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
JComboBox Insert Edited Value Into Table
JComboBox 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
Java JComboBox Get Selected Item Value
Java JComboBox Get Selected Item Value In this section we will discuss about how to get the selected item value form JComboBox. javax.swing.JComboBox... of JComboBox into the database table. To store the selected item value of JComboBox
show null point Exception """ while trying 2 load a JComboBox"" data is fetch from database ms-access
show null point Exception """ while trying 2 load a JComboBox"" data is fetch...*; import java.sql.*; class Comboo extends JFrame { JComboBox jcb; Connection con...().setBackground(d); setDefaultCloseOperation(JFrame.EXITONCLOSE); jcb=new JComboBox
show null point Exception """ while trying 2 load a JComboBox"" data is fetch from database ms-access
show null point Exception """ while trying 2 load a JComboBox"" data is fetch...*; import java.sql.*; class Comboo extends JFrame { JComboBox jcb; Connection con...().setBackground(d); setDefaultCloseOperation(JFrame.EXITONCLOSE); jcb=new JComboBox
show null point Exception """ while trying 2 load a JComboBox"" data is fetch from database ms-access
show null point Exception """ while trying 2 load a JComboBox"" data is fetch...*; import java.sql.*; class Comboo extends JFrame { JComboBox jcb; Connection con...().setBackground(d); setDefaultCloseOperation(JFrame.EXITONCLOSE); jcb=new JComboBox
Combo box In Java
a value in the editable field. Combo box can be created using JCombobox in java.... Constructor of JCombobox are as follows: JComboBox() : Create a combo box with a default. JCombobox(ComboBoxModel a) : Create a combo box that accept
Jlist
JcomboBox to a Jlist  How to transfer a data from a JcomboBox to a Jlist ? Each time I select 1 item from a JComboBox, it will display in a JList
Jcombo box
Jcombo box  I want To add ittem in JComboBox which is stored in Mysql Database. Database Column name -Course Items -MBA,MCA,MBA IT,MBA FINANCE I want Load Database records in to my JComboBox
Urgent requirement
Urgent requirement  I want to implement Autocompletion code for Jcombobox in swing can you please help me out from
java
java   Applet program using JComboBox and JCheckBox. Explain in Detil about JRdio button and give exaple. how do you add a number to a GUI using swing? Briefly describe about JTable with exaple program. what do you mean
How to declare a Combobox without using a string in its declaration?
the values later? For example JComboBox x= new JComboBox(); ... String s={"Alpha... lab=new JLabel("Course"); final JComboBox combo=new JComboBox
Java Swings
Java Swings  I am doing one project on java Swings, in this i have created one jframe where i defined some JButtons and Jcombobox's, here i need to insert one JTable with headers(IN CURRENT JFrame only).pls help me urgently
java
java  hi i am new to java swing and i want a code in which the data would be taken from ms access database and it should be populated in jcombobox and then if a letter is typed then that letters informaion should be shown
java netbeans
java netbeans  i am making project in core java using Netbeans. Regarding my project i want to know that How to fetch the data from ms-access database in jcombobox ? and How to insert, update or delete the data in the database
java netbeans
java netbeans  i am making project in core java using Netbeans. Regarding my project i want to know that How to fetch the data from ms-access database in jcombobox ? and How to insert, update or delete the data in the database
jdbc odbc
jdbc odbc  Sir, i want to get the selected value from JCombobox to ms...://www.roseindia.net/java/example/java/swing/store-jcombobox-selecteditem-value...("JDBC All in One"); JComboBox petList = new JComboBox(petStrings
swings - Java Beginners
","image3.gif"}; public Combo() { JComboBox combo = new JComboBox...("JCombobox Demo"); frame.add(new Combo()); frame.pack

Ads