Results 1 to 15 of 15
Thread: Could anyway answer me this?
- 12-16-2010, 03:16 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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 03:43 AM.
- 12-16-2010, 03:22 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
What IDE did you write this?
Where did BuildGui come from?
- 12-16-2010, 03:28 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
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.
- 12-16-2010, 03:38 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
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.
- 12-16-2010, 03:43 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
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?
- 12-16-2010, 03:47 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
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.
- 12-16-2010, 03:50 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
- 12-16-2010, 03:56 AM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
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.
- 12-16-2010, 03:58 AM #9
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
- 12-16-2010, 04:02 AM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
or dya want the answer?
- 12-16-2010, 04:09 AM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
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.
-
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.
- 12-16-2010, 04:35 AM #13
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
I feel I really let this person down , buh they believed in there programming all d time.
- 12-16-2010, 11:29 PM #14
Member
- Join Date
- Dec 2010
- Posts
- 21
- Rep Power
- 0
-
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
-
Please answer me quickly
By sweet girl in forum New To JavaReplies: 20Last Post: 12-10-2010, 11:17 PM -
A QUESTION U CAN ALL ANSWER, EXCEPT ME, (if not, come and sit with me lol)
By mansoor2 in forum New To JavaReplies: 12Last Post: 07-27-2010, 10:40 AM -
Help me answer a few questions
By namlunxxx in forum XMLReplies: 3Last Post: 06-16-2010, 06:26 PM -
Need help with Java Answer
By alexisasoxfan in forum New To JavaReplies: 2Last Post: 05-10-2010, 08:17 AM -
Need answer asap!!!
By uranis_khai in forum New To JavaReplies: 3Last Post: 07-07-2009, 10:48 AM
Bookmarks