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 9 of 9

Thread: linking of classes

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Nigeria
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default linking of classes

     
    I am new to this forum,please pardon my mistakes.I want to link these two java classes together.here are my codes.

    AustasMain.java(A)

    import java.awt.event.ActionListener.*;
    import java.awt.event.ActionEvent;
    import java.awt.*; 
    import java.awt.event.*;
    import java.awt.Label.*;
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.sql.*;
    public class AustasMain extends JPanel {
    String user;
    double pass;
    double pass1=1234;
    String userr="user";
    Label username,password;
    TextField username1,password1;
    private JButton ok;
    private JTextArea textarea;
    private SecMain sec; 
    public AustasMain () {
    setLayout(new FlowLayout());
    username=new Label("USERNAME");add(username);
    username1= new TextField(10);add(username1);
    password=new Label("PASSWORD");add(password);
    password1=new TextField(10);add(password1);
    ok=new JButton("OK");add(ok);
     
     
    TextHandler handler=new TextHandler();
    username1.addActionListener(handler);
    password1.addActionListener(handler);
    ok.addActionListener(handler);
    }
     
    class TextHandler implements ActionListener {
    public void actionPerformed(ActionEvent event) { 
    if(event.getSource()==username1){
    String user=(username1.getText());
             }
    if(event.getSource()==password1){
    double pass=Double.parseDouble(password1.getText());
             }
    if(event.getSource()==ok) {
    String user=(username1.getText()); 
    int pars=Integer.parseInt(password1.getText());
    String pass=(password1.getText());
    double pass1=1234;
    String userr="user";
    try{
    		 if(user.equals("") && pass.equals("")) {
          JOptionPane.showMessageDialog(null,"Enter login name or password","Error",JOptionPane.ERROR_MESSAGE);
      }
    		else if (user.equals("user") && pass.equals("400")) {
        // this calls the class below.here A passes to B.
         SecsMain sec=new SecsMain();
        sec.pas=pars;
        sec.setVisible(true);
        				}
    		else {
         JOptionPane.showMessageDialog(null,"Invalid login name or password","Error",JOptionPane.ERROR_MESSAGE);
         username1.setText("");
         password1.setText("");
      				}
       }
        catch(Exception e){}                    
    }
    }
    }
    public static void main(String [] args) {
    AustasMain panel=new AustasMain();
    JFrame frame=new JFrame("LOGIN FORM");
    frame.setSize(250,200);
    frame.add(panel);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
                                      }

    the second class is :
    Secs.java(B)

    import java.awt.*;           //
    import java.awt.event.*;    //  PACKAGES
    import javax.swing.*;      //
    import java.sql.*;        //
    import java.awt.event.ActionListener.*;
    import java.awt.event.ActionEvent;
     
     
    public class SecsMain extends JFrame implements ActionListener {
           private JButton mkt,acct,store;int pas;
           //-----Constructor------
     
           public SecsMain() {
                  super("DEPARTMENTS");              
                  setLayout(new GridLayout(1,4));
                  mkt = new JButton("MARKET");
                  mkt.setPreferredSize(new Dimension(150,300));
                  mkt.setSize(mkt.getPreferredSize());              
                  mkt.addActionListener(this);
                  add(mkt);
                  acct = new JButton("ACCOUNT");
                  acct.setPreferredSize(new Dimension(150,300));
                  acct.setSize(acct.getPreferredSize());
                  acct.addActionListener(this);
                  add(acct);
                  JButton store= new JButton ("STORE");
                  store.setPreferredSize(new Dimension(150,300));
                  store.setSize(store.getPreferredSize());
                  add(store);
                  JButton md= new JButton ("MD");
                  md.setPreferredSize(new Dimension(150,300));
                  md.setSize(md.getPreferredSize());
                  add(md);
                  setSize(pas,300);              
    				} 
    public void actionPerformed(ActionEvent event) { }
    }
    //The program compiles but does not call the second program when the username and password is correctly entered.Please help me.
    Last edited by henryjavaprofor; May 3rd, 2012 at 08:21 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: linking of classes

    I moved this thread from the tutorial forum as this is not a tutorial. Also, please use the highlight tags to preserve formatting when you post code.

    What exactly do you mean by "linking of classes"? You're going to have to make an instance of each, then provide a reference in one class to the other instance. How you should do that depends on how these classes are related.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Location
    Nigeria
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: linking of classes

    Thanks for your help but i am writing a program where the first interface being the login frame will get the username and the password and then pass it on to the second interface where it will open a second frame.i want to know how i can pass for instance what class A gets unto class B.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: linking of classes

    class A would need a reference to an instance of class B. Given that, the code in class A can call methods in class B to pass it data.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Location
    Nigeria
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: linking of classes

    thanks.check my codes i did reference it.Didn't i do it well?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: linking of classes

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting. Unformatted code is hard to read and understand.

    Which is class A and which is class B? Where are you trying to pass what class A gets unto class B.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Location
    Nigeria
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: linking of classes

    thanks i have done it

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Location
    Nigeria
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: linking of classes

    please help me.do not leave at this point

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: linking of classes

    Please explain what the problem is now.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Linking class's
    By Tate in forum Object Oriented Programming
    Replies: 3
    Last Post: February 23rd, 2012, 04:12 AM
  2. Re: linking two or more java files using menus
    By simonphilip102 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2011, 06:08 AM
  3. Linking two JLists
    By mib1bee in forum AWT / Java Swing
    Replies: 2
    Last Post: December 30th, 2010, 02:19 PM
  4. Database connection using NetBeans
    By jcc285 in forum Java IDEs
    Replies: 6
    Last Post: June 9th, 2009, 03:23 AM
  5. [SOLVED] How to link two different class?
    By John in forum Object Oriented Programming
    Replies: 11
    Last Post: April 27th, 2009, 02:57 PM