Results 1 to 5 of 5
  1. #1
    ppreeti is offline Member
    Join Date
    Mar 2014
    Posts
    17
    Rep Power
    0

    Default combobox not getting displayed

    So, my requirement was to Display Records From Database Using JTable in Java. I referred the this link.
    Display Records From Database Using JTable in Java
    But the error is that when I run it in Eclipse, I can't see the combobox as it should ideally appear. How to fix it ?
    Java Code:
    import java.awt.*;
        import java.awt.event.*;
        import java.sql.*;
        import java.util.Vector;
    
        import javax.swing.*;
        import javax.swing.table.DefaultTableModel;
    
        public class DisplayEmpData extends JFrame implements ActionListener {
    
            JFrame frame1;
            JLabel l0, l1, l2;
            JComboBox c1;
            JButton b1;
            Connection con;
            ResultSet rs, rs1;
            Statement st, st1;
            PreparedStatement pst;
            String ids;
            static JTable table;
            String[] columnNames = {"User name", "Email", "Password", "Country"};
            String from;
    
            DisplayEmpData() {
    
                l0 = new JLabel("Fatching Employee Information");
                l0.setForeground(Color.red);
                l0.setFont(new Font("Serif", Font.BOLD, 20));
                l1 = new JLabel("Select name");
                b1 = new JButton("submit");
    
                l0.setBounds(100, 50, 350, 40);
                l1.setBounds(75, 110, 75, 20);
                b1.setBounds(150, 150, 150, 20);
                b1.addActionListener(this);
    
                setTitle("Fetching Student Info From DataBase");
                setLayout(null);
                setVisible(true);
                setSize(500, 500);
                setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
                add(l0);
                add(l1);;
                add(b1);
                try {
    
                    String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\users\\ppreeti\\emp_details.accdb";
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con=DriverManager.getConnection(url,"","");
                    /*con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome");*/
                    st = con.createStatement();
                    rs = st.executeQuery("select uname from emp");
                    Vector v = new Vector();
                    while (rs.next()) {
                        ids = rs.getString(1);
                        v.add(ids);
                    }
                    c1 = new JComboBox(v);
                    c1.setBounds(150, 110, 150, 20);
    
                    add(c1);
                    st.close();
                    rs.close();
                } catch (Exception e) {
                }
            }
    
            public void actionPerformed(ActionEvent ae) {
                if (ae.getSource() == b1) {
                    showTableData();
                }
    
            }
    
            public void showTableData() {
    
                frame1 = new JFrame("Database Search Result");
                frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame1.setLayout(new BorderLayout());
        //TableModel tm = new TableModel();
                DefaultTableModel model = new DefaultTableModel();
                model.setColumnIdentifiers(columnNames);
        //DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames());
        //table = new JTable(model);
                table = new JTable();
                table.setModel(model);
                table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
                table.setFillsViewportHeight(true);
                JScrollPane scroll = new JScrollPane(table);
                scroll.setHorizontalScrollBarPolicy(
                        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                scroll.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
                from = (String) c1.getSelectedItem();
        //String textvalue = textbox.getText();
                String uname = "";
                String email = "";
                String pass = "";
                String cou = "";
    
                try {
                    pst = con.prepareStatement("select * from emp where UNAME='" + from + "'");
                    ResultSet rs = pst.executeQuery();
                    int i = 0;
                    if (rs.next()) {
                        uname = rs.getString("uname");
                        email = rs.getString("umail");
                        pass = rs.getString("upass");
                        cou = rs.getString("ucountry");
                        model.addRow(new Object[]{uname, email, pass, cou});
                        i++;
                    }
                    if (i < 1) {
                        JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                    if (i == 1) {
                        System.out.println(i + " Record Found");
                    } else {
                        System.out.println(i + " Records Found");
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }
                frame1.add(scroll);
                frame1.setVisible(true);
                frame1.setSize(400, 300);
            }
    
            public static void main(String args[]) {
                new DisplayEmpData();
            }
        }
    Last edited by ppreeti; 05-09-2014 at 08:10 AM.

  2. #2
    ppreeti is offline Member
    Join Date
    Mar 2014
    Posts
    17
    Rep Power
    0

    Default combobox not getting displayed

    So, my requirement was to Display Records From Database Using JTable in Java. I referred the this link.
    Display Records From Database Using JTable in Java
    But the error is that when I run it in Eclipse, I can't see the combobox as it should ideally appear. How to fix it ?
    Java Code:
    import java.awt.*;
        import java.awt.event.*;
        import java.sql.*;
        import java.util.Vector;
     
        import javax.swing.*;
        import javax.swing.table.DefaultTableModel;
     
        public class DisplayEmpData extends JFrame implements ActionListener {
     
            JFrame frame1;
            JLabel l0, l1, l2;
            JComboBox c1;
            JButton b1;
            Connection con;
            ResultSet rs, rs1;
            Statement st, st1;
            PreparedStatement pst;
            String ids;
            static JTable table;
            String[] columnNames = {"User name", "Email", "Password", "Country"};
            String from;
     
            DisplayEmpData() {
     
                l0 = new JLabel("Fatching Employee Information");
                l0.setForeground(Color.red);
                l0.setFont(new Font("Serif", Font.BOLD, 20));
                l1 = new JLabel("Select name");
                b1 = new JButton("submit");
     
                l0.setBounds(100, 50, 350, 40);
                l1.setBounds(75, 110, 75, 20);
                b1.setBounds(150, 150, 150, 20);
                b1.addActionListener(this);
     
                setTitle("Fetching Student Info From DataBase");
                setLayout(null);
                setVisible(true);
                setSize(500, 500);
                setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
     
                add(l0);
                add(l1);;
                add(b1);
                try {
     
                    String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\users\\ppreeti\\emp_details.accdb";
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con=DriverManager.getConnection(url,"","");
                    /*con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome");*/
                    st = con.createStatement();
                    rs = st.executeQuery("select uname from emp");
                    Vector v = new Vector();
                    while (rs.next()) {
                        ids = rs.getString(1);
                        v.add(ids);
                    }
                    c1 = new JComboBox(v);
                    c1.setBounds(150, 110, 150, 20);
     
                    add(c1);
                    st.close();
                    rs.close();
                } catch (Exception e) {
                }
            }
     
            public void actionPerformed(ActionEvent ae) {
                if (ae.getSource() == b1) {
                    showTableData();
                }
     
            }
     
            public void showTableData() {
     
                frame1 = new JFrame("Database Search Result");
                frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame1.setLayout(new BorderLayout());
        //TableModel tm = new TableModel();
                DefaultTableModel model = new DefaultTableModel();
                model.setColumnIdentifiers(columnNames);
        //DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames());
        //table = new JTable(model);
                table = new JTable();
                table.setModel(model);
                table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
                table.setFillsViewportHeight(true);
                JScrollPane scroll = new JScrollPane(table);
                scroll.setHorizontalScrollBarPolicy(
                        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                scroll.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
                from = (String) c1.getSelectedItem();
        //String textvalue = textbox.getText();
                String uname = "";
                String email = "";
                String pass = "";
                String cou = "";
     
                try {
                    pst = con.prepareStatement("select * from emp where UNAME='" + from + "'");
                    ResultSet rs = pst.executeQuery();
                    int i = 0;
                    if (rs.next()) {
                        uname = rs.getString("uname");
                        email = rs.getString("umail");
                        pass = rs.getString("upass");
                        cou = rs.getString("ucountry");
                        model.addRow(new Object[]{uname, email, pass, cou});
                        i++;
                    }
                    if (i < 1) {
                        JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                    if (i == 1) {
                        System.out.println(i + " Record Found");
                    } else {
                        System.out.println(i + " Records Found");
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }
                frame1.add(scroll);
                frame1.setVisible(true);
                frame1.setSize(400, 300);
            }
     
            public static void main(String args[]) {
                new DisplayEmpData();
            }
        }

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: combobox not getting displayed

    Duplicate threads merged
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,021
    Rep Power
    6

    Default Re: combobox not getting displayed

    I'm not going to say anything more than this:

    Java Code:
    } catch (Exception e) {
    }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: combobox not getting displayed

    Use meaningful variable names. "l1", "c1", "b1" mean absolutely nothing. I'm not even going to look at the code. Programming code should be readable and understandable.

    Crossposted: http://stackoverflow.com/questions/2...ting-displayed
    Last edited by camickr; 05-09-2014 at 04:20 PM.

Similar Threads

  1. Problem: Fill a comboBox from another combobox
    By Ridha in forum AWT / Swing
    Replies: 2
    Last Post: 03-08-2013, 02:34 PM
  2. Menu not getting displayed!
    By javabeginner29 in forum AWT / Swing
    Replies: 5
    Last Post: 03-12-2012, 11:58 AM
  3. Same name displayed twice!
    By javabeginner29 in forum New To Java
    Replies: 2
    Last Post: 02-24-2012, 06:17 AM
  4. Act on a not displayed applet
    By cachente in forum New To Java
    Replies: 4
    Last Post: 09-09-2011, 02:37 PM
  5. Tooltip not getting displayed...
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 07-31-2008, 10:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •