Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: GUI not showing its J-elements???

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question GUI not showing its J-elements???

    Hi everyone,

    I have created two classes, GUI class and PROVA class. When I run the DB class, it shows a frame, but not the text fields and other elements.

    Can any of you notice any mistake in my code? Thanks in advance..


    PROVA Class:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class prova {
     
     
     
     
     
    Connection con;
    Statement st;
    ResultSet rs;
     
    public prova () {
     
    connect();
     
    }
    public void connect() {
     
    try {
     
    String driver = "sun.jdbc.odbc.Jdbc0dbcDriver";
    Class.forName(driver);
     
    String prova = "jdbc:odbc:db1";
    con = DriverManager.getConnection(prova);
    st = con.createStatement();
    String sq1 = "select * from Table1";
    rs = st.executeQuery(sq1);
     
    while (rs.next())
     
    {
     
    String fname = rs.getString("Fname");
    String lname = rs.getString("Lname");
    String age = rs.getString("Age");
    System.out.println(fname + " " + lname + " " + age);
    }
     
    }catch(Exception e){
    }
    }
     
    public static void main(String[] args) {
     
    new prova();
    new gui();
     
     
    }
    }



    GUI Class

    import javax.swing.*;
    import java.awt.*;
     
    public class gui {
     
    	JFrame f;
    	JLabel fname;
    	JLabel lname;
    	JLabel age;
    	JTextField t;
    	JTextField t1;
    	JTextField t2;
     
     
     
    	public gui() {
     
    		frame();
     
    		}
     
    	public void frame() {
     
    		f = new JFrame ();
    		f.setVisible(true);
    		f.setSize (600,400);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		fname = new JLabel ("First Name");
    		lname = new JLabel ("Last Name");
    		age = new JLabel ("Age");
     
    		t = new JTextField(10);
    		t1 = new JTextField(10);
    		t2 = new JTextField(10);
     
    		JPanel p = new JPanel();
    		p.add(fname);
    		p.add(t);
    		p.add(lname);
    		p.add(t1);
    		p.add(age);
    		p.add(t2);
     
    		f.add(p);
     
     
     
    	}
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: GUI not showing its J-elements???

    notice any mistake in my code
    }catch(Exception e){
    }
    An empty catch block is a mistake. Add a call to the printStackTrace() method to show any errors.

    Another problem is the frame is shown (setVisible) BEFORE anything is added to it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: GUI not showing its J-elements???

    please make the changes in ur code

    >>>> Code removed
    Last edited by Norm; January 3rd, 2013 at 07:07 AM. Reason: code removed - don't spoonfeed

Similar Threads

  1. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  2. Why it is showing error??
    By Sreejith63 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 12th, 2012, 12:16 AM
  3. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM
  4. [SOLVED] Gap between GUI elements
    By petemyster in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2011, 11:44 AM
  5. Showing a picture in a GUI
    By joachim89 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 02:42 PM