Results 1 to 15 of 15
  1. #1
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default Could anyone answer me this?

    Hi, I'm wondering whether anyone can help me on understanding an issue I'm stuck on.

    I'm very new to Java and my first little personal project is a basic form which connects to a MySQL database depending on the username/password etc entered.

    Everything is working dandy, until I get to the Action Event stage where I'm having difficulty in retreiving the data entered into the form as the object had to have been instantiated earlier in order to set the form up and load the button.

    PHP Code:
    
    //import javax.swing.JOptionPane;
    import java.awt.event.*;
    
    class Main {
    	
    	public static String location;
    	public static String username;
    	public static String password;
    	public static String database;
    	
    	public static void main(String[] args) {
    		
    		SetGui sg = new SetGui();
    
    	}
    	
    	
    	private static class SetGui implements ActionListener {
    		
    		public SetGui() {
    			
    			BuildGui gui = new BuildGui();
    			gui.setVisible(true);
    								
    			gui.submit.addActionListener(this);
    		}
    		
    		
    		public void actionPerformed(ActionEvent event) {
    			// gui.location.getText() cannot return anything as gui object has been instantiated outside of this method
    			location = gui.location.getText();
    			username = gui.username.getText();
    			password = gui.password.getText();
    			database = gui.database.getText();
    			
    			//MySql dbCon = new MySql(location, username, password, database);
    		}
    		
    		
    	}
    
    }
    Last edited by Jojomofoman; 12-16-2010 at 02:43 AM.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    What IDE did you write this?

    Where did BuildGui come from?

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Want my opinion? you have no idea how to impement java swing components and you are calling it MySql problem...Learn to crawl, before ya walk.

  4. #4
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    you have no idea how to impement java swing components
    Is it really that obvious? :D I've watched loads of tutorials on implementing Swing and they all seem to do it in different ways. If you could point me in the direction of the right way to implement swing then it would be much appreciated.

    But as it stands, the only way I'm going to progress is by pushing myself outside of my comfort zone (the typical "Hello world") and try something a little more challenging.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    I abslotuley agree with you and dig your ambition but we still aint seen nothin you tried. I aint a hard ass I come across as one but I am trying to make you see your talent not ours> dya understand?

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Let me put it this way to you to inspire you... I know a person who talent with the questions they ask and I know a beginnner very fast. But I do not answer the questions different. dya understand. U have to take responsibvility where you should be.

  7. #7
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    I abslotuley agree with you and dig your ambition but we still aint seen nothin you tried. I aint a hard ass I come across as one but I am trying to make you see your talent not ours> dya understand?
    lol don't worry about it, I'm not offended by your bluntness. If anything i'd rather it given to me straight than any other way.

    My Java talent is non existent at the moment, i'm bashing bits and pieces together in an effort to make sense of it all!

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    now you are selling your self short... trust me... what you asked help for you are not far away from...so I am reluctant to tell you it.

  9. #9
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    now you are selling your self short... trust me... what you asked help for you are not far away from...so I am reluctant to tell you it.
    Alright Mr Miyagi, I shall continue my quest for enlightenment. ;) I was supposed to go to bed about 3 hours ago but I've been racking my brains out over this. So maybe a bit of sleep and a fresh look at it tomorrow will help me! :)

  10. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    or dya want the answer?

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Do you want my honest opinion? You will never be a programmer, suere you got talent, sure you can see things not many can see. Do you want to know how i am better than you though you are smarter? I work at it and you dont or maybe you do but you dont really work at it the way somebody like me works at it :) sorry.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jojomofoman View Post
    Hi, I'm wondering whether anyone can help me on understanding an issue I'm stuck on.

    I'm very new to Java and my first little personal project is a basic form which connects to a MySQL database depending on the username/password etc entered.

    Everything is working dandy, until I get to the Action Event stage where I'm having difficulty in retreiving the data entered into the form as the object had to have been instantiated earlier in order to set the form up and load the button.

    PHP Code:
    
    //import javax.swing.JOptionPane;
    import java.awt.event.*;
    
    class Main {
    	
    	public static String location;
    	public static String username;
    	public static String password;
    	public static String database;
    	
    	public static void main(String[] args) {
    		
    		SetGui sg = new SetGui();
    
    	}
    	
    	
    	private static class SetGui implements ActionListener {
    		
    		public SetGui() {
    			
    			BuildGui gui = new BuildGui();
    			gui.setVisible(true);
    								
    			gui.submit.addActionListener(this);
    		}
    		
    		
    		public void actionPerformed(ActionEvent event) {
    			// gui.location.getText() cannot return anything as gui object has been instantiated outside of this method
    			location = gui.location.getText();
    			username = gui.username.getText();
    			password = gui.password.getText();
    			database = gui.database.getText();
    			
    			//MySql dbCon = new MySql(location, username, password, database);
    		}
    		
    		
    	}
    
    }
    The BuildGui object, gui, is declared inside of the SetGui constructor and is thus only visible in there. Hence you cannot access it from an actionPerformed method or in fact any other method of the class. A solution would be to declare this variable in the class, not the constructor so that it will be visible everywhere. But having said that, it looks as though you're trying to create the "view" inside of the "control", which looks a little funny to me, but I suppose it could work.

  13. #13
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    I feel I really let this person down , buh they believed in there programming all d time.

  14. #14
    Jojomofoman is offline Member
    Join Date
    Dec 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    The BuildGui object, gui, is declared inside of the SetGui constructor and is thus only visible in there. Hence you cannot access it from an actionPerformed method or in fact any other method of the class. A solution would be to declare this variable in the class, not the constructor so that it will be visible everywhere. But having said that, it looks as though you're trying to create the "view" inside of the "control", which looks a little funny to me, but I suppose it could work.
    Thank you for your help! You say that it looks a little funny to you. How would you perform the same action? Would you keep the action in the gui class? I'm very new to Java and OOP so any advice no matter how small is always appreciated! :)

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First of all, I'm not a professional, and so please take any recommendations with a grain of salt, but I've tried to split out my GUI components -- the Swing visual parts, from the logic of the program -- the stuff that works in the background. For e.g.,

    Java Code:
    import javax.swing.*;
    
    public class MVC_Main {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createGui();
                }
            });
        }
    
        private static void createGui() {
            GUI2 gui = new GUI2();
            Control control = new Control();
            Model model = new Model();
            
            gui.setControl(control);
            control.setGUI(gui);
            control.setModel(model);
    
            JFrame frame = new JFrame("App");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(gui);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class GUI2 extends JPanel {
        private JTextField userNameField = new JTextField(10);
        private JPasswordField passwordField = new JPasswordField(10);
        private JButton submitButton = new JButton("Submit");
        private Control control;
        
        public GUI2() {
            submitButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (control != null) {
                        control.submitAction();
                    }
                }
            });
            
            add(userNameField);
            add(passwordField);
            add(submitButton);
        }
        
        public void setControl(Control control) {
            this.control = control;
        }
        
        public String getUserName() {
            return userNameField.getText();
        }
        
        public char[] getPassword() {
            return passwordField.getPassword();
        }
    }

    Java Code:
    public class Control {
        private GUI2 gui;
        private Model model;
        
        public void setGUI(GUI2 gui) {
            this.gui = gui;
        }
        
        public void setModel(Model model) {
            this.model = model;
        }
        
        public void submitAction() {
            if (gui == null || model == null) {
                // I would probably throw an exception here
                return;
            }
            String userName = gui.getUserName();
            char[] password = gui.getPassword();
            
            // to show that this works
            System.out.println("user name: " + userName);
            System.out.println("password:  " + new String(password));
            
            // call model SQL methods now with data using a background thread 
            // such as a SwingWorker
        }
    
    }

    Java Code:
    public class Model {
        // SQL stuff done here
    }

Similar Threads

  1. Please answer me quickly
    By sweet girl in forum New To Java
    Replies: 20
    Last Post: 12-10-2010, 10:17 PM
  2. Replies: 12
    Last Post: 07-27-2010, 09:40 AM
  3. Help me answer a few questions
    By namlunxxx in forum XML
    Replies: 3
    Last Post: 06-16-2010, 05:26 PM
  4. Need help with Java Answer
    By alexisasoxfan in forum New To Java
    Replies: 2
    Last Post: 05-10-2010, 07:17 AM
  5. Need answer asap!!!
    By uranis_khai in forum New To Java
    Replies: 3
    Last Post: 07-07-2009, 09:48 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
  •