0

I am using the following code to display the particular user's details by using JCombobox to select his username from the database. The combo box is to list the user names from the table. But the combo box is not visible when i run the code. (no error though). It would be helpful if someone tell whats wrong with code. Thanks in advance.

public class EmpSearchApp extends JFrame implements ActionListener {

    JLabel l, l1, l2, l3, l4, l5, l6, l7, l8;
    JButton b;
    JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7;
    JComboBox bx;
    String str;

    EmpSearchApp() {
        setVisible(true);
        setSize(700, 700);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("USER DATA");

        l = new JLabel("Select Name:");
        b = new JButton("Submit");

        tf1 = new JTextField();
        tf2 = new JTextField();
        tf3 = new JTextField();
        tf4 = new JTextField();
        tf5 = new JTextField();
        tf6 = new JTextField();
        tf7 = new JTextField();

        l.setBounds(20, 20, 200, 20);
        b.setBounds(50, 50, 150, 30);

        add(l);
        add(b);

        tf1.setEditable(false);
        tf2.setEditable(false);
        tf3.setEditable(false);
        tf4.setEditable(false);
        tf5.setEditable(false);
        tf6.setEditable(false);
        tf7.setEditable(false);
        b.addActionListener(this);

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.45:1521:orcl", "gemsexam", "gems123");
            PreparedStatement ps = con.prepareStatement("select uname from logf");
            ResultSet rs = ps.executeQuery();
            Vector v = new Vector();
            while (rs.next()) {
                String s = rs.getString(1);

                v.add(s);
            }
            bx = new JComboBox(v);
            bx.setBounds(240, 20, 200, 20);
            add(bx);

        } catch (Exception ex) {
            System.out.println(ex);
        }

    }

    public void actionPerformed(ActionEvent e) {
        showData();
    }

    public void showData() {
        JFrame f1 = new JFrame();
        f1.setVisible(true);
        f1.setSize(500, 500);
        f1.setLayout(null);
        f1.setTitle("USER DATA");

        l5 = new JLabel("Displaying Data:");
        l5.setForeground(Color.red);
        l5.setFont(new Font("Serif", Font.BOLD, 20));
        l1 = new JLabel("Name:");
        l2 = new JLabel("Contact:");
        l3 = new JLabel("email:");
        l4 = new JLabel("qual:");
        l6 = new JLabel("Tech:");
        l7 = new JLabel("status");
        l8 = new JLabel("address");

        l5.setBounds(100, 50, 300, 30);
        l1.setBounds(20, 110, 200, 20);
        l2.setBounds(20, 140, 200, 20);
        l3.setBounds(20, 170, 200, 20);
        l4.setBounds(20, 200, 200, 20);
        l6.setBounds(20, 230, 200, 20);
        l7.setBounds(20, 260, 200, 20);
        l8.setBounds(20, 290, 200, 20);

        tf1.setBounds(240, 110, 200, 20);
        tf2.setBounds(240, 140, 200, 20);
        tf3.setBounds(240, 170, 200, 20);
        tf4.setBounds(240, 200, 200, 20);
        tf5.setBounds(240, 230, 200, 20);
        tf6.setBounds(240, 260, 200, 20);
        tf7.setBounds(240, 290, 200, 20);

        f1.add(l5);
        f1.add(l1);
        f1.add(tf1);
        f1.add(l2);
        f1.add(tf2);
        f1.add(l3);
        f1.add(tf3);
        f1.add(l4);
        f1.add(tf4);
        f1.add(l6);
        f1.add(tf5);
        f1.add(l7);
        f1.add(tf6);
        f1.add(l8);
        f1.add(tf7);

        str = (String) bx.getSelectedItem();
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.45:1521:orcl", "gemsexam", "gems123");
            PreparedStatement ps = con.prepareStatement("select * from logf where uname=?");
            ps.setString(1, str);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {

                tf1.setText(rs.getString(1));
                tf2.setText(rs.getString(2));
                tf3.setText(rs.getString(3));
                tf4.setText(rs.getString(4));
                tf5.setText(rs.getString(4));
                tf6.setText(rs.getString(4));
                tf7.setText(rs.getString(4));

            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public static void main(String arr[]) {
        new EmpSearchApp();
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Meenu Shankar
  • 63
  • 1
  • 10
  • 1
    Does it work if you remove the database stuff and hardcode the query result? You need to narrow down your problem (and your code): database issue or swing issue. – assylias Jun 06 '14 at 06:09
  • Rather than `} catch (Exception ex) { System.out.println(ex);..` I'd suggest `} catch (Exception ex) { e.printStackTrace()..`. When bad things happen, we want as much detail as possible. Can you do this with hard-coded data? For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Jun 06 '14 at 06:10
  • 1
    Don't use `null` layouts. Pixel perfect layouts are an illusion in modern UI design, you have no control over fonts, DPI, rendering pipelines or other factors that will change the way that you components will be rendered on the screen. Swing was designed to work with layout managers to overcome these issues. If you insist on ignoring these features and work against the API design, be prepared for a lot of headaches and never ending hard work... – MadProgrammer Jun 06 '14 at 06:14
  • 2
    1JFrame f1 = new JFrame(); f1.setVisible(true);` The call to `setVisible(true)` should be *last*, immediately after `pack()`. That might cause some (further) problems with things like `l5.setBounds(100, 50, 300, 30); ..`, but then, you should use layouts to layout a GUI - [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jun 06 '14 at 06:14
  • 1
    Further tips: 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Jun 06 '14 at 06:17

3 Answers3

2

Just put setVisible(true) at the end of constructor.

Completely agree with MadProgrammer that better to use specific layout rather than setting bounds for all the elements.

Assuming You are getting proper data from Oracle Database.

akash
  • 22,664
  • 11
  • 59
  • 87
1

The combination of null layouts and the timing of the setVisible call are working against you.

First, don't use null layouts, they produce more problems than they're worth, second make setVisble the last call you make after you construct the window if you can.

I would also avoid mixing the error state of your data retrieval with the construction of your UI elements, for example, in your code, if for some reason the data retrieval fails, the combobox will never be shown, personally, better to present an empty combobox and an error, for example...

bx = new JComboBox();
bx.setBounds(240, 20, 200, 20);
add(bx);
try {
    Vector v = new Vector();
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.45:1521:orcl", "gemsexam", "gems123");
    PreparedStatement ps = con.prepareStatement("select uname from logf");
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
        String s = rs.getString(1);

        v.add(s);
    }
    bx.setModel(new DefaultComboBoxModel(v));

} catch (Exception ex) {
    System.out.println(ex);
    JOptionPane.showMessageDialog(this, "I have failed");
}

Take a look at Laying Out Components Within a Container for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yes , I agree. I ll be using the Layout managers instead of null layouts. Adding catch block will be an additional use. Thanks. – Meenu Shankar Jun 06 '14 at 06:26
0

You should initialize and add JComboBox outside of try block. Because, if any database exception have occur than JComboBox will not be added or be visible. And better to create the JComboBox at start-up and populate it when the DB query is resolved.

 // Write these line, outside of try block.
 bx = new JComboBox(v);
 bx.setBounds(240, 20, 200, 20);
 add(bx);
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • Perhaps better to create the combo. at app. start-up and simply *populate* it when the DB query is resolved. – Andrew Thompson Jun 06 '14 at 06:15
  • @AndrewThompson, Yes, that will be better. Thanks. – Masudul Jun 06 '14 at 06:17
  • Thanks everyone , i ll be following all the suggested practices and the error is that my Jdbc driver was not included in the eclipse IDE. And I solved the error. – Meenu Shankar Jun 06 '14 at 06:22
  • BTW - see [this answer](http://stackoverflow.com/a/21659516/418556) for a good way (`GroupLayout`) to align the labels and text fields of the `Displaying Data:` frame (but it should be a dialog). – Andrew Thompson Jun 06 '14 at 06:30