Page 1 of 2 12 LastLast
Results 1 to 20 of 28
Like Tree2Likes

Thread: open a class from another

  1. #1
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default open a class from another

    i have a class named "Shrimp" that is my start run class and its code :
    Java Code:
    package ca.uvic.csr.shrimp;
    
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.net.URI;
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    
    import ca.uvic.csr.shrimp.ShrimpApplication.StandAloneApplication.StandAloneApplication;
    import ca.uvic.csr.shrimp.resource.ResourceHandler;
    
    public class Shrimp {
    
        /**
         * "The" main method to launch stand-alone shrimp
         * @param args Optional Command Line Parameter: <i>filename</i> Opens this file immediately as program starts up.
         */
        public static void main(String[] args) {
    		try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		System.out.println("Starting Shrimp...");
    		System.out.println("java.version=" + System.getProperty("java.version"));
    		//System.out.println("java.vm.version=" + System.getProperty("java.vm.version"));
    		System.out.println("os.name=" + System.getProperty("os.name"));
    		//System.out.println("os.arch=" + System.getProperty("os.arch"));
            //System.out.println("os.version=" + System.getProperty("os.version"));
            //System.out.println("java.io.tmpdir=" + System.getProperty("java.io.tmpdir"));
    
    		final StandAloneApplication application = new StandAloneApplication();
            JFrame frame = application.createParentFrame();
            application.initialize(frame);
            frame.setVisible(true);
    		if (args.length > 0) {
    		    String fileName = args[0];
    		    final URI uri = ResourceHandler.getFileURI(fileName);
    		    if (uri != null) {
    				SwingUtilities.invokeLater(new Runnable () {
    					public void run() {
    						application.openProject(uri);
    					}
    				});
    		    } else {
    		        System.err.println("File '" + fileName + "' does not exist.");
    		    }
    		}
    
    		frame.addWindowListener(new WindowAdapter() {
                @Override
    			public void windowClosed(WindowEvent evt) {
             		// Exit without an error
    				System.exit(0);
    			}
    		});
        }
    }
    i want to add an authentification class, i wrote this code
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * Authentifier.java
     *
     * Created on 8 déc. 2009, 22:01:53
     */
    
    package ca.uvic.csr.shrimp.util;
    
    import ca.uvic.csr.shrimp.Shrimp;
    import java.awt.Font;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author Administrateur
     */
    public class Authentifier extends javax.swing.JFrame
    {
        /** Creates new form Authentifier */
        String[] logins=new String[2];
    
        String[] passwd=new String[2];
    
        Font f = new Font("Times New Roman ", 1, 12);
    
        public Authentifier() 
        {
            this.setIconImage(new ImageIcon("images/DW.jpg").getImage());
            logins[0]="SalesManager";
            logins[1]="CHEF";
            passwd[0]="jamel";
            passwd[1]="jamel";
            setTitle("Authentification");
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            panel_authent = new javax.swing.JPanel();
            label_login = new javax.swing.JLabel();
            label_pwd = new javax.swing.JLabel();
            text_login = new javax.swing.JTextField();
            buttonOK = new javax.swing.JButton();
            buttonCancel = new javax.swing.JButton();
            text_password = new javax.swing.JPasswordField();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(new java.awt.Color(255, 204, 153));
    
            panel_authent.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Authentification", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 2, 18), new java.awt.Color(102, 0, 102))); // NOI18N
    
            label_login.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
            label_login.setText("Login name:");
    
            label_pwd.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
            label_pwd.setText("Password:");
    
            text_login.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
    
            buttonOK.setText("OK");
            buttonOK.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonOKActionPerformed(evt);
                }
            });
    
            buttonCancel.setText("Cancel");
            buttonCancel.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    buttonCancelActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout panel_authentLayout = new javax.swing.GroupLayout(panel_authent);
            panel_authent.setLayout(panel_authentLayout);
            panel_authentLayout.setHorizontalGroup(
                panel_authentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panel_authentLayout.createSequentialGroup()
                    .addGroup(panel_authentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(panel_authentLayout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(panel_authentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(label_login)
                                .addComponent(label_pwd)))
                        .addGroup(panel_authentLayout.createSequentialGroup()
                            .addGap(49, 49, 49)
                            .addComponent(buttonOK)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(panel_authentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_authentLayout.createSequentialGroup()
                            .addComponent(buttonCancel)
                            .addGap(44, 44, 44))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panel_authentLayout.createSequentialGroup()
                            .addGroup(panel_authentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(text_password, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                                .addComponent(text_login, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE))
                            .addContainerGap())))
            );
    
            panel_authentLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonCancel, buttonOK});
    
            panel_authentLayout.setVerticalGroup(
                panel_authentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panel_authentLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(panel_authentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(label_login)
                        .addComponent(text_login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(panel_authentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(label_pwd)
                        .addComponent(text_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(30, 30, 30)
                    .addGroup(panel_authentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(buttonOK)
                        .addComponent(buttonCancel))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(81, 81, 81)
                    .addComponent(panel_authent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(90, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(65, 65, 65)
                    .addComponent(panel_authent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(65, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void buttonOKActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            int i;
            for (i = 0; i < logins.length; i++)
            {
                if (text_login.getText().equalsIgnoreCase(logins[i]))
                {
                    if (!text_password.getText().equalsIgnoreCase(passwd[i]))
                    {
                        System.out.println(text_password.getText());
                        System.out.println(passwd[i]);
                        JOptionPane.showMessageDialog(null, "Invalid password", "Dialog Message", JOptionPane.ERROR_MESSAGE);
                        text_password.setText("");
                        break;
    
                    }
                    else
                    {
                        this.dispose();
                        new Shrimp();
                        break;
                    }
                }
    
            }
            if (i == logins.length)
            {
                JOptionPane.showMessageDialog(null, "Invalid login", "Dialog Message", JOptionPane.ERROR_MESSAGE);
                text_login.setText("");
    
            }
    }                                        
    
        private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {                                             
            // TODO add your handling code here:
            this.setVisible(false);
            System.exit(0);
    }                                            
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[])
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    Authentifier aut=new Authentifier();
                    aut.setVisible(true);
                    aut.setLocationRelativeTo(null);
    //                new Menu().setVisible(true);
    //                new EDD.FrameEDD(null, "commerce").setVisible(true);
    //               new REQUIREMENT.FrameNewRequirement("walid").setVisible(true);
    //                new REQUIREMENT.FrameOldRequirement("ccc.xml").setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton buttonCancel;
        private javax.swing.JButton buttonOK;
        private javax.swing.JLabel label_login;
        private javax.swing.JLabel label_pwd;
        private javax.swing.JPanel panel_authent;
        private javax.swing.JTextField text_login;
        private javax.swing.JPasswordField text_password;
        // End of variables declaration                   
    
    }
    but after good authentification, shrimp doesn't start and the program close

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: open a class from another

    Do you see any error messages? Please copy and paste them here.

    Otherwise explain what the problem is. How do you execute these classes? What happens?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: open a class from another

    Shrimp is not a true object oriented class, but nothing more than a large main method. Consider re-writing it so that it doesn't have a main method but rather has fields, a constructor,... etc.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: open a class from another

    This is just random goo of code; essential classes aren't even mentioned. This blab of code is not worth a further look; try again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: open a class from another

    Quote Originally Posted by JosAH View Post
    This is just random goo of code; essential classes aren't even mentioned. This blab of code is not worth a further look; try again.

    kind regards,

    Jos
    shrimp is a project that i downloaded it and this first class it is its main class
    me i want to add an authentification class

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: open a class from another

    What code is there in the Shrimp class's constructor that will execute when you create an instance of it?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: open a class from another

    Quote Originally Posted by mharsijamel View Post
    shrimp is a project that i downloaded it and this first class it is its main class
    me i want to add an authentification class
    Just shows what happens when you download other code without understanding it. Again, it's not a true OOPs class, it's worthless, don't use it. Again, write your own and start with good OOP principles.

  8. #8
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: open a class from another

    Quote Originally Posted by Norm View Post
    What code is there in the Shrimp class's constructor that will execute when you create an instance of it?
    i said later that shrimp was the first run class(main class)

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: open a class from another

    Did you read my question? Do you know what a class's constructor is? Does the Shrimp class have a constructor?

    Please explain how you execute these classes.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: open a class from another

    Quote Originally Posted by Norm View Post
    Did you read my question? Do you know what a class's constructor is? Does the Shrimp class have a constructor?

    Please explain how you execute these classes.
    this is the whole class of shrimp, and i just download the project and make shrimp.java(what i wrote in the beginning) the run class
    Shrimp | The CHISEL Group, University of Victoria :is the link from what i downloaded it

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: open a class from another

    Please explain how you execute these classes. What command line do you use? What is the name of the class that you start execution with?

    If you do NOT know what a class's constructor is, you need to go to your textbooks and study about it.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: open a class from another

    Quote Originally Posted by Norm View Post
    Please explain how you execute these classes. What command line do you use? What is the name of the class that you start execution with?

    If you do NOT know what a class's constructor is, you need to go to your textbooks and study about it.
    i downloaded this project and i opened it with netbeans and i made shrimp.java(that i wrote later) in start run class

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: open a class from another

    Are you saying that the execution starts with the Shrimp class?
    Does the Authentifier class ever execute? What starts the execution of the Authentifier class?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: open a class from another

    Quote Originally Posted by Norm View Post
    Does the Authentifier class ever execute? What starts the execution of the Authentifier class?
    Nothing does (search for the text "Authentifier"); as I wrote before: this is just code poo and not worth looking at any further.

    kind regards,

    Jos
    Fubarable and Norm like this.
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: open a class from another

    Quote Originally Posted by Norm View Post
    Are you saying that the execution starts with the Shrimp class?
    Does the Authentifier class ever execute? What starts the execution of the Authentifier class?

    i want to say that is in the normal way(in the beginning when i downloaded it), but now i want that authentifier will the first run class before shrimp

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: open a class from another

    Go back and answer the questions asked in posts #6 and #9
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: open a class from another

    Quote Originally Posted by Norm View Post
    Go back and answer the questions asked in posts #6 and #9
    the code what i present in the first post is the whole code, in the project i don't create any instance of it, it is just the first run class

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: open a class from another

    I was asking you to analyze the code and answer the question. I have seen the code you posted.
    Can you answer my questions?

    don't create any instance of it
    Following from your posted code:
    Java Code:
          else
                    {
                        this.dispose();
                        new Shrimp();  //<<<<<<<<<<< WHAT IS THIS????
                        break;
                    }
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    mharsijamel is offline Member
    Join Date
    Aug 2011
    Posts
    60
    Rep Power
    0

    Default Re: open a class from another

    Quote Originally Posted by Norm View Post
    I was asking you to analyze the code and answer the question. I have seen the code you posted.
    Can you answer my questions?


    Following from your posted code:
    Java Code:
          else
                    {
                        this.dispose();
                        new Shrimp();  //<<<<<<<<<<< WHAT IS THIS????
                        break;
                    }


    shrimp does not have a constructor so it will use the default constructor

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: open a class from another

    Can you explain what your problem is?
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. how to open class(jpanel) from external jar?
    By skadron_alfa in forum New To Java
    Replies: 3
    Last Post: 01-15-2012, 05:41 AM
  2. Replies: 6
    Last Post: 07-25-2011, 10:34 AM
  3. how can i open a .class in a text editor
    By footballdude2k3 in forum New To Java
    Replies: 1
    Last Post: 04-14-2011, 09:00 PM
  4. Open next class help
    By Tastosis in forum New To Java
    Replies: 2
    Last Post: 03-04-2011, 01:12 AM
  5. How do I open a file with a class, or can I?
    By bobleny in forum New To Java
    Replies: 4
    Last Post: 04-02-2009, 09:59 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
  •