Results 1 to 10 of 10
- 02-02-2013, 07:51 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Unable to fetch data from other class
Hi,
I have 2 classes - login and MailReader. login builds the GUI and gets data from the user i.e username, password. MailReader has the main function and makes an object of GUI class and calls the buildGUI method. On pressing the OK button the ActionListener for the button after setting the username and password invokes the MailReader class. In the MailReader class i have a try catch block. Now if i use the same object to retrieve the username and password from getters of login class it tells me that object unidentified. If i create a new object and call getters then i get null instead of the data entered.
I sense some basic flaw that i am doin here. Any help is deeply appreciated.
GUI class -
MailReader Class -Java Code:public class login { public static final int WIDTH = 550; public static final int HEIGHT = 400; private static String MONITOR = "MONITOR"; private JFrame controllingFrame; JFrame frame; JTextField emailField; JTextField mailboxField; JPasswordField passwdField; JButton monitorButton; JButton cancelButton; private String email; private String mailbox; private String password; public void buildGUI(){ frame = new JFrame("Welcome to Visualization"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BorderLayout layout = new BorderLayout(); JPanel background = new JPanel(layout); background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); Box labelBox = new Box(BoxLayout.Y_AXIS); //Label 1 JLabel reg = new JLabel("Registration Form"); Font bigFont = new Font("serif", Font.BOLD, 28); reg.setFont(bigFont); labelBox.add(reg); //Label 2 JLabel dir = new JLabel("Please enter the details below :"); labelBox.add(dir); //EMail and Password field JPanel panelB = new JPanel(new GridLayout(3,1)); //JPanel panelB = new JPanel(); panelB.add(new JLabel("Email ID:")); emailField = new JTextField(20); emailField.setToolTipText("Enter your email-ID"); //emailField.setActionCommand(MONITOR); setEmail(emailField.getText()); panelB.add(emailField); panelB.add(new JLabel("Password :")); passwdField = new JPasswordField(20); passwdField.setToolTipText("Enter your password"); //passwdField.setActionCommand(MONITOR); setPassword(passwdField.getPassword()); panelB.add(passwdField); labelBox.add(panelB); JPanel panelC = new JPanel(); monitorButton = new JButton("MONITOR"); monitorButton.addActionListener(new MonitorListener()); panelC.add(monitorButton); cancelButton = new JButton("CANCEL"); cancelButton.addActionListener(new CancelListener()); panelC.add(cancelButton); labelBox.add(panelC); background.add(BorderLayout.WEST, labelBox); frame.getContentPane().add(background); frame.setBounds(50,50,300,300); frame.pack(); frame.setSize(WIDTH, HEIGHT); frame.setVisible(true); } //ActionListener Inner Class for the button MONITOR public class MonitorListener implements ActionListener{ public void actionPerformed(ActionEvent a){ String cmd = a.getActionCommand(); if(MONITOR.equals(cmd)){ setEmail(emailField.getText()); setPassword(passwdField.getPassword()); // Invoking the MailReader Class new MailReader(); } else{ JOptionPane.showMessageDialog(controllingFrame, "Invalid password. Try again.", "Error Message", JOptionPane.ERROR_MESSAGE); } } } public class CancelListener implements ActionListener{ public void actionPerformed(ActionEvent a){ System.exit(0); } } public void setEmail(String emailString){ email = emailString; } public void setPassword(char[] passwordChar){ String passwordString = new String(passwordChar); password = passwordString; } public String getEmail(){ return email; } public String getPasswd(){ return password; } }
Java Code:public class MailReader { private int unreadMessages; private int totalMessages; Folder inbox; public int getUnreadMessages(){ return unreadMessages; } public MailReader(){ Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imap"); try{ Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imap"); // Here is the problem where it is difficult getting the username and password from the login class String mailId = obj1.getEmail(); System.out.println("Mail id fetched is :" + mailId); String passwod = obj1.getPasswd(); store.connect(mailbox, mailId, passwod); System.out.println(store); inbox = store.getFolder("Inbox"); int totalMsgs = inbox.getMessageCount(); int unreadMsgs = inbox.getUnreadMessageCount(); System.out.println("Total no of Messages : " + totalMsgs); System.out.println("No of Unread Messages : " + unreadMsgs); inbox.open(Folder.READ_ONLY); Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false)); /*********** Use a suitable FetchProfile***********/ FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.CONTENT_INFO); inbox.fetch(messages, fp); try{ printAllMessages(messages); inbox.close(true); store.close(); } catch (Exception ex){ System.out.println("Exception arose at the time of read mail"); ex.printStackTrace(); } } catch (NoSuchProviderException e){ e.printStackTrace(); System.exit(1); } catch (MessagingException e){ e.printStackTrace(); System.exit(2); } } public static void main(String[] args) { login obj1 = new login(); obj1.buildGUI(); }Last edited by buggedup; 02-03-2013 at 11:39 AM. Reason: adding code
- 02-04-2013, 11:22 AM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
Re: Unable to fetch data from other class
Hello buggedup,
Check the access modifier for obj1. ;)
Regards.
- 02-04-2013, 11:37 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Unable to fetch data from other class
It doesn't have an access modifier as it is a local variable and so is not visible anywhere else in the code.
Please do not ask for code as refusal often offends.
- 02-04-2013, 12:11 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
Re: Unable to fetch data from other class
Exactly what I was hinting at.
Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Regards.
- 02-05-2013, 11:56 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Re: Unable to fetch data from other class
I think there is a flow problem but i am not able to figure out what. I am calling the builGUI() method from the login class and before buildGUI() completes i invoke the MailReader() constructor. Is this correct?
However i changed the flow and put the main in login class and now am calling buildGUI and then i doing a new MailReader(); in the main. But this has a problem as well. The GUI does not wait for user input at all. It just flashes and goes away.
I am a bit confused and stuck on how to do this. Should i stick to the previous code or this version?Last edited by buggedup; 02-05-2013 at 10:47 PM.
- 02-05-2013, 12:57 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Unable to fetch data from other class
Take a step back.
Your Login class (classnames should begin with an uppercase letter) is your view. It's job is to display stuff and to react to user events.
One of those events involves a button click, which should result in your MailReader code running.
That code is not part of the GUI.
It should either be called by a controller, which would be registered to the Login class, or should itself be registered to the login class.
That would normally be in the form of an interface.
Look at how ActionListener works, and the registering of one.
That's how you want this to work.
Your main() method will create a MailReader, that does nothing in the constructor and implements the monitorEmails method.Java Code:public class Login { private MonitorListener monitorListener; // This is an interface that your MailReader will implement. Names are up to you. public Login (MonitorListener ml) { monitorListener = ml; // rest of your code here. } // Inside your ActionListener, monitor part monitorListener.monitorEmails(email, mailbox, password); etc etc }
That method does what your constructor code does at the moment.
The main() will pass that object into the Login constructor.Please do not ask for code as refusal often offends.
- 02-05-2013, 05:57 PM #7
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Re: Unable to fetch data from other class
I don't exactly get it what you are trying to say. I understand that the controller part should not be a part of the view i.e Login class. But i have 2 inner classes which implement ActionListener i.e MonitorListener and CancelListener. Should i replace both of these with the actionPerformed method ?
main() creates a MailReader, but how can i create an object of MailReader before Login? i shud get input frm user first rite?
- 02-05-2013, 06:05 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Unable to fetch data from other class
No, they are both listeners.
They're fine.
One for each button.
You want, inside the monitor one, to tell the controller to do whatever it is MailReader does.
Move the code in mailReader into its own method.
Don't bother with a constructor for that at all.
Call that method from the MonitorListener.Please do not ask for code as refusal often offends.
- 02-06-2013, 01:00 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 15
- Rep Power
- 0
Re: Unable to fetch data from other class
Just moved the code from the MailReader() constructor to a method montorEmails() and called it from the monitorListener. It worked. But i dont understand why this happened? y would the GUI disappear if i have the code in the MailReader constructor and y would the GUI wait for input if i have the code in the method.
- 02-06-2013, 09:45 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
unable to add JTable data into microsoft access!!! please help me with the code!
By cute in forum JDBCReplies: 8Last Post: 11-22-2011, 10:45 AM -
how to make JScrollpane not to fetch data while scrollbar is adjusting?
By saba in forum AWT / SwingReplies: 4Last Post: 01-21-2010, 05:46 PM -
How to fetch integer data from excel
By nehakuls in forum JDBCReplies: 5Last Post: 11-24-2009, 01:52 PM -
How to fetch integer data from excel
By nehakuls in forum NetBeansReplies: 2Last Post: 11-18-2009, 05:47 AM -
Unable to read data from inputstream
By renuka_anil in forum Java ServletReplies: 0Last Post: 01-29-2009, 03:20 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks