0

I have populated my JComboBox with the Userdata of database table and I need an idea to display the corresponding value of Userdata in my jtextfield as I select a data from my jcombobox and click generate button.

I am not using Array list instead I am using database table(Sqlyog),below is the program

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class Test extends JFrame implements ActionListener  
{
    JButton b1,b2;
    JComboBox jc;
    String name;
    JTextField t1;
    Connection con,conn;
    Statement st;
    PreparedStatement pst;
    ResultSet rs,rs1;
    Test()
    {
        setLayout(null);       
        JLabel l1=new JLabel("USER SELECTION :");
        l1.setBounds(100, 100, 150, 60);
        add(l1);
        JComboBox jc = new JComboBox();
        jc.setBounds(230,114,120,30);
        jc.addActionListener(this);
        add(jc);
        JButton b1=new JButton("GENERATE PART NO. :");
        b1.setBounds(70, 340, 170, 30);
        b1.addActionListener(this);
        add(b1);
        t1=new JTextField (10);
        t1.setBounds(270, 340, 200, 30);
        add(t1);
        JButton b2=new JButton("BACK");
        b2.setBounds(190, 420,120, 30);
        b2.addActionListener(this);
        add(b2);

        setSize(500, 500);
        setResizable(false);
        this.setVisible(true); 
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con= DriverManager.getConnection("jdbc:mysql://localhost/d02","root","");
            st = con.createStatement();

            String s = "select Userdata from user";

            rs = st.executeQuery(s);

            while(rs.next())
            {
                String name = rs.getString("Userdata");
                jc.addItem(rs.getString("Userdata"));
            }
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "ERROR");
        }
        finally
        {
            try
            {
    //st.close();
                rs.close();
    //con.close();
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(null, "ERROR CLOSE");
            }
        }
    }

    public void actionPerformed(ActionEvent ae)

    {
        String str=ae.getActionCommand();
        if(str.equals("GENERATE PART NO. :"))
        {
            try
            {
                Class.forName("com.mysql.jdbc.Driver");                          
                conn =        DriverManager.getConnection("jdbc:mysql://localhost/d02","root","");

                String query="select value from user where Userdata=?";
                PreparedStatement pst=conn.prepareStatement(query);
                String tmp=(String)jc.getSelectedItem().toString();
                if(jc.getSelectedItem()!=null)
                    pst.setString(1,tmp);
                ResultSet rs1=pst.executeQuery();
                while(rs1.next())
                {
                    String add=rs1.getString("value");
                    t1.setText(add);
                }

            }
            catch(Exception ex)
            {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "ERROR CLOSE");
            }
            if(str.equals("BACK")==true)
            {
                new categ();
                setVisible(false);
            }
        }
    }   
    public static void main(String[] args) throws IOException
    {
        Test td=new Test();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user6443988
  • 57
  • 1
  • 1
  • 11
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Jun 13 '16 at 05:04
  • 1
    Note: `JComboBox jc = new JComboBox();` This statement is 'shadowing' the declaration of the combo box at the top of the class. It likely should be just `jc = new JComboBox(); //create combo and assign to existing variable..` – Andrew Thompson Jun 13 '16 at 05:07

2 Answers2

0

I have modified you code like below. Hope it will help you to progress.

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

class Test extends JFrame implements ActionListener {

    JButton b1, b2;
    JComboBox jc;
    String name;
    JTextField t1;
    Connection con, conn;
    Statement st;
    PreparedStatement pst;
    ResultSet rs, rs1;

    Test() {
        setLayout(null);
        JLabel l1 = new JLabel("USER SELECTION :");
        l1.setBounds(100, 100, 150, 60);
        add(l1);
        add(getUserDataCombo());
        JButton b1 = new JButton("GENERATE PART NO. :");
        b1.setBounds(70, 340, 170, 30);
        b1.addActionListener(this);
        add(b1);
        t1 = new JTextField(10);
        t1.setBounds(270, 340, 200, 30);
        add(t1);
        JButton b2 = new JButton("BACK");
        b2.setBounds(190, 420, 120, 30);
        b2.addActionListener(this);
        add(b2);
        setSize(500, 500);
        setResizable(false);
        initUserData();
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private JComboBox getUserDataCombo() {
        if (jc == null) {
            jc = new JComboBox();
            jc.setBounds(230, 114, 120, 30);
            jc.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JComboBox combo = (JComboBox) e.getSource();
                    String data = String.valueOf(combo.getSelectedItem());
                    //TODO: Do whatever you want with the data
                }
            });
        }
        return jc;
    }

    private void initUserData() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/d02", "root", "");
            st = con.createStatement();
            String s = "select Userdata from user";
            rs = st.executeQuery(s);
            while (rs.next()) {
                String name = rs.getString("Userdata");
                jc.addItem(rs.getString("Userdata"));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "ERROR");
        } finally {
            try {
                //st.close();
                rs.close();
                //con.close();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "ERROR CLOSE");
            }
        }
    }

    public void actionPerformed(ActionEvent ae) {
        String str = ae.getActionCommand();
        if (str.equals("GENERATE PART NO. :")) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost/d02", "root", "");

                String query = "select value from user where Userdata=?";
                PreparedStatement pst = conn.prepareStatement(query);
                String tmp = (String) jc.getSelectedItem().toString();
                if (jc.getSelectedItem() != null) {
                    pst.setString(1, tmp);
                }
                ResultSet rs1 = pst.executeQuery();
                while (rs1.next()) {
                    String add = rs1.getString("value");
                    t1.setText(add);
                }

            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(null, "ERROR CLOSE");
            }
            if (str.equals("BACK") == true) {
                new categ();
                setVisible(false);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        Test td = new Test();
    }
}
Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21
  • what about displaying a serial increment counter(not randomly)with no duplicates within the same textfield everytime a selection is made from the jcombobox???? – user6443988 Jun 13 '16 at 07:49
  • @user6443988 I am not able to understand. Once you get the userdata you can use that value. Not sure what you mean by _serial increment counter(not randomly)with no duplicates_ – Beniton Fernando Jun 13 '16 at 09:16
  • My idea here is to display a serial number count along with the value in the same textfield everytime I click on the button(GENERATE PART NO.) – user6443988 Jun 13 '16 at 09:35
  • It should show the concatenated string of value and generated system count and it should be unique – user6443988 Jun 13 '16 at 09:36
0
     String tmp = (String)cmbCustomers.getSelectedItem();
    String query = "select CUSTOMER_ID from NORTHWIND_CUSTOMER where FULL_NAME = ?";

        try{
            pst = connect.prepareStatement(query);
            pst.setString(1, tmp);
            rs = pst.executeQuery();

             if(rs.next()){

             String add1 = rs.getString("CUSTOMER_ID");
             jTextField1.setText(add1);

             }          



        }catch(Exception e){

            JOptionPane.showMessageDialog(null, e);

        }

With this code you can display the values of the tables into a jTextfields. For do that you have to select the jCombobox right click select event-popup menu-poppumenuwillbecomeinvisible and insert the code above. I forgot to mention that the pst and the rs variable had been declared before they can be use. i declared PreparedStatement pst = null and ResultSet rs = null.