Results 1 to 9 of 9
  1. #1
    raverz2 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Getting class attribute values from JTextField

    Good afternoon everyone,
    I'm fairly new to java, having some trouble, and hopefully someone can help me out here. Basically I have a form with 1 JTextField and 1 Button. Ive defined an abstract class and all of it's attributes like below:

    abstract class Student {

    private Integer studentID;

    public Student(Integer studentID){
    this.StudentID = studentID;
    }

    public Integer getStudentID(){
    return studentID;
    }

    public void setStudentID(Integer id){
    studentID = id;
    }

    What I am having trouble with is getting the studentID from the JTextField when the button is pressed. Right now I would just like it to show a message dialogbox with the value from the JTextField when the button is pressed. I will use this value later to insert to a database. Any help that is offered is greatly appreciated.

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

    Default

    Right now I would just like it to show a message dialogbox with the value from the JTextField when the button is pressed
    Create a JFrame with a text field and a button. Then you add an ActionListener to the button to get the text from the text field.

    This is called a Short, Self Contained, Correct Example. If the code works, great your problem is solved. If not, then you have something simple to post in the forum and we can help solve your problem.

  3. #3
    raverz2 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    So then how would I go about accessing the values in a JTextField utilizing an abstract method ? For instance if I created an abstract method add(), how would I get the value from the JTextField? I am then going to implement that method on a button so when the button is pushed it adds a record to a database.

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

    Default

    If the button's action listener is an inner class, it will have access to the JTextField. Or you can pass a reference to the JTextField to the action listener class when you create it:
    new YOURACTIONLISTNER(refToJTextField); // pass reference to the JTF

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

    Default

    You have to learn to walk before you run. You can add any data to a database until you know how to display a text field and access the data from the text field when you click a button.

    You have posted your SSCCE yet so I can't help you any further because I'm not going to write the code for you.

    You can also read Trail: Creating a GUI With JFC/Swing: Table of Contents (The Java™ Tutorials) which has section on using text fields, write ActionListeners and everything you need to know on using Swing.

  6. #6
    raverz2 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    I appreciate all of the responses. Below is the abstract class that I have created with all of the abstract methods. What I need to do is create a JFrame GUI that contains buttons that calls the abstract methods add(), update(), delete, and query(). These methods need to take the values in JTextFields and add them to a mySQL database. I already have the code for the insert to the database complete. If i understand this correctly i need to add an actionListener to the button and reference the JTextField? Once again thank you for all of your assistance.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package Student_Code;
    
    /**
     *
     * @author Administrator
     */
    abstract class Student {
        private Integer studentID;
        private String firstName;
        private String lastName;
        private float gpa;
        private String status;
        private String mentor;
        
        public Student() {
        }
    
        public Student(Integer studentID, String firstName, String lastName, float gpa, String status, String mentor) {
            this.studentID = studentID;
            this.firstName = firstName;
            this.lastName = lastName;
            this.gpa = gpa;
            this.status = status;
            this.mentor = mentor;
        }
    
        public Integer getStudentID() {
            return studentID;
        }
    
        public void setStudentID(Integer studentID) {
           
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
           
        }
    
        public Float getGpa() {
            return gpa;
        }
    
        public void setGpa(Float gpa) {
            
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
    
        }
    
        public String getMentor() {
            return mentor;
        }
    
        public void setMentor(String mentor) {
        
        }
            
        abstract void calculateTuition();
        
        abstract void update();
        
        abstract void add();
        
        abstract void delete();
        
        abstract void query();
    }
    
    
        abstract class Undergraduate extends Student {
        
            public String level;
            
            public Undergraduate(String lvl, Integer studentID, String firstName, String lastName, float gpa, String status, String mentor){
                super(studentID, firstName, lastName, gpa, status, mentor);
                level = lvl;
      
            }
            
            public String getLevel() {
            return level;
            }
    
            public void setLevel(String level) {
            
            }
            
            @Override
            void calculateTuition() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void update() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void add() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void delete() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void query() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
            
          abstract class Graduate extends Student {
              private String thesisTitle;
              private String thesisAdvisor;
            
            public Graduate(String title, String advisor, Integer studentID, String firstName, String lastName, float gpa, String status, String mentor){
                super(studentID, firstName, lastName, gpa, status, mentor);
                thesisTitle = title;
                thesisAdvisor = advisor;
            } 
            
            public String getThesisTitle() {
            return thesisTitle;
            }
    
            public void setThesisTitle(String thesisTitle) {
            
            }
            
            public String getThesisAdvisor() {
            return thesisTitle;
            }
    
            public void setThesisAdvisor(String thesisAdvisor) {
            
            }
            
            @Override
            void calculateTuition() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void update() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void add() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void delete() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void query() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
            
            abstract class PartTime extends Student {
             private String company;
              
            
            public PartTime(String comp, Integer studentID, String firstName, String lastName, float gpa, String status, String mentor){
                super(studentID, firstName, lastName, gpa, status, mentor);
                company = comp;
            }    
            
            public String getCompany() {
            return company;
            }
    
            public void setCompany(String company) {
            
            }
            
            @Override
            void calculateTuition() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void update() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void add() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void delete() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
    
            @Override
            void query() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
            
            }
          }
        }

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

    Default

    I appreciate all of the responses.
    But you haven't listened to a single answer that has been given to you.

    1. You still haven't posted a SSCCE
    2) Your question is about getting the value of a text field when a button is clicked. The Student class is irrelevant to your question.

    If i understand this correctly i need to add an actionListener to the button and reference the JTextField?
    Yes, so do it. If you have problems then post the SSCCE your created We are not going to write the code for you. You have been given links to the tutorial which show every you need to know to do this.

  8. #8
    raverz2 is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Problem solved

    I would like to thank everyone for their suggestions which pointed me in the right directions. And I would especially like to thank camickr for his hostility. I neither expected anyone to code for me or provide me the corrected code. I had a few questions that were politely answered and I was on my way. Thanks for all the help once again.

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

    Default

    And I would especially like to thank camickr for his hostility
    I think you meant to say:

    "for taking his valuable time to help me learn problem solving techniques that will help me know and in the tuture".

Similar Threads

  1. Replies: 5
    Last Post: 09-16-2011, 07:01 AM
  2. Replies: 16
    Last Post: 05-24-2011, 09:29 AM
  3. Replies: 0
    Last Post: 03-19-2011, 11:54 PM
  4. Checking for XML Attribute Values
    By garbar in forum XML
    Replies: 3
    Last Post: 07-23-2010, 05:52 PM
  5. Invalid usebean class attribute - new to java
    By grinsalot in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-04-2008, 04:54 AM

Posting Permissions

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