Results 1 to 17 of 17
Like Tree8Likes
  • 1 Post By JosAH
  • 1 Post By Norm
  • 1 Post By DarrylBurke
  • 1 Post By jim829
  • 1 Post By DarrylBurke
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By jim829

Thread: Multiple Classes in One Package?

  1. #1
    PrinceT is offline Member
    Join Date
    Dec 2014
    Posts
    17
    Rep Power
    0

    Default Multiple Classes in One Package?

    Hi Friends.
    I'm having problems with this I've code like:

    Java Code:
    Public Class{
    Objects
    Constructor(){}
    ActionListener Method(){}
    Main Function()}
    
    Class2{}
    Class3{}
    Class4{}
    I want to execute class 2,3 or 4 via Action Listener;
    Java Code:
    public void actionPerformed(ActionEvent ta){
            if(b5==ta.getSource())
                System.exit(0);
            if(b4==ta.getSource())
                Class1 tani=new Class1();
    but I cannot declare variables in this method, please guide me how can I do this what can be the simplest method?

    In addition: I've a marksheet class, a calculator class, & in main class I've 2 buttons one for marksheet & one for calculator, how can I make it so that if user clicks on marksheet button that class should be called & so?

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

    Default Re: Multiple Classes in One Package?

    Quote Originally Posted by PrinceT View Post
    but I cannot declare variables in this method
    Why not? Did your compiler complain? If so, show us the compiler output and a bit of the relevant code.

    kind regards,

    Jos
    PrinceT likes this.
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    PrinceT is offline Member
    Join Date
    Dec 2014
    Posts
    17
    Rep Power
    0

    Default Re: Multiple Classes in One Package?

    Quote Originally Posted by JosAH View Post
    Why not? Did your compiler complain? If so, show us the compiler output and a bit of the relevant code.

    kind regards,

    Jos
    yes bro netbeans is not accepting, according to it: variables declaration not allowed here.

    Java Code:
    package multi;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Multi implements ActionListener{
        JFrame f=new JFrame("Pick your Task");
        JButton b1=new JButton("Claculate Marks");
        JButton b2=new JButton("Simple Calculator");
        JButton b4=new JButton("Play Tic Tac Toe");
        JButton b3=new JButton("View FB Welcome Page");
        JButton b5=new JButton("Exit");
        Multi(){
            f.setLayout(null);
            f.setBounds(900,50,205,278);
            b1.setBounds(0,0,200,50);
            b2.setBounds(0,50,200,50);
            b3.setBounds(0,100,200,50);
            b4.setBounds(0,150,200,50);
            b5.setBounds(0,200,200,50);
            f.setResizable(false);
            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            b4.addActionListener(this);
            b5.addActionListener(this);
            
            f.add(b1);
            f.add(b2);
            f.add(b3);
            f.add(b4);
            f.add(b5);
            f.show();
        }
        public void actionPerformed(ActionEvent ta){
            if(b5==ta.getSource())
                System.exit(0);
            if(b4==ta.getSource())
                Marksheet tani2=new Marksheet ();
            }
        public static void main(String[] args) {
            Multi Tani=new Multi();
            
        }
    }
    
    ////Here's my class for marksheet
    class Marksheet implements ActionListener {
        JFrame f=new JFrame("Marksheet");
        //JLabel subs=new JLabel("Subjects");
        //JLabel marks=new JLabel("Marks");
        //JLabel grade=new JLabel("Grade");
        JLabel total=new JLabel("Total");
        JLabel ob=new JLabel("Obtained");
        JLabel perc=new JLabel("Percentage");
        JLabel java=new JLabel("Enter Java Marks:");
        JLabel cpp=new JLabel("Enter C++ Marks:");
        JLabel php=new JLabel("Enter PHP Marks:");
        JLabel vfb=new JLabel("Enter vfB Marks:");
        JLabel dnet=new JLabel("Enter dotNet Marks:");
        JButton calc=new JButton("Calculate");
        JButton clear=new JButton("Clear");
        JButton exit=new JButton("Exit");
        JTextField m1=new JTextField();
        JTextField m2=new JTextField();
        JTextField m3=new JTextField();
        JTextField m4=new JTextField();
        JTextField m5=new JTextField();
        JTextField total1=new JTextField();
        JTextField ob1=new JTextField();
        JTextField perc1=new JTextField();
        Font ff=new Font("aria",Font.BOLD,14);
        Font ff2=new Font("georgia",Font.BOLD,20);
        Font ff3=new Font("times new roman",Font.BOLD,16);
        //JTextField g1=new JTextField();
        //JTextField g2=new JTextField();
        //JTextField g3=new JTextField();
        //JTextField g4=new JTextField();
        //JTextField g5=new JTextField();
        Marksheet(){
      //      subs.setBounds(10,10,100,30);
    //        marks.setBounds(110,10,100,30);
            java.setBounds(10,10,150,30);
            java.setFont(ff);
            m1.setBounds(160,10,50,30);
            m1.setFont(ff2);
            cpp.setBounds(10,40,150,30);
            cpp.setFont(ff);
            m2.setBounds(160,40,50,30);
            m2.setFont(ff2);
            php.setBounds(10,70,150,30);
            php.setFont(ff);
            m3.setBounds(160,70,50,30);
            m3.setFont(ff2);
            vfb.setBounds(10,100,150,30);
            vfb.setFont(ff);
            m4.setBounds(160,100,50,30);
            m4.setFont(ff2);
            dnet.setBounds(10,130,150,30);
            dnet.setFont(ff);
            m5.setBounds(160,130,50,30);
            m5.setFont(ff2);
            total.setBounds(100,190,100,30);
            total.setFont(ff3);
            total1.setBounds(200,190,70,30);
            total1.setFont(ff3);
            ob.setBounds(100,220,100,30);
            ob.setFont(ff3);
            ob1.setBounds(200,220,70,30);
            ob1.setFont(ff3);
            perc.setBounds(100,250,100,30);
            perc.setFont(ff3);
            perc1.setBounds(200,250,70,30);
            perc1.setFont(ff3);
            clear.setBounds(10,300,70,30);
            calc.setBounds(100,300,100,30);
            exit.setBounds(210,300,70,30);
            
            clear.addActionListener(this);
            exit.addActionListener(this);
            calc.addActionListener(this);
            total1.setEnabled(false);
            ob1.setEnabled(false);
            perc1.setEnabled(false);
            
            f.setLayout(null);
            f.setBounds(100,100,306,400);
            f.add(m1);
            f.add(m2);
            f.add(m3);
            f.add(m4);
            f.add(m5);
            f.add(java);
            f.add(cpp);
            f.add(vfb);
            f.add(php);
            f.add(dnet);
            f.add(perc);
            f.add(perc1);
            f.add(ob);
            f.add(ob1);
            f.add(total);
            f.add(total1);
            f.add(calc);
            f.add(exit);
            f.add(clear);
            f.show();
            Marksheet marks=new Marksheet();
        }
        public void actionPerformed(ActionEvent ta){
            if(clear==ta.getSource()){
                m1.setText("");
                m2.setText("");
                m3.setText("");
                m4.setText("");
                m5.setText("");
                perc1.setText("");
                ob1.setText("");
                total1.setText("");
             }
            if(exit==ta.getSource())
                System.exit(0);
            if(calc==ta.getSource()){
                total1.setText("500");
                String mm1=m1.getText();
                String mm2=m2.getText();
                String mm3=m3.getText();
                String mm4=m4.getText();
                String mm5=m5.getText();
                int m11=Integer.parseInt(mm1);
                int m22=Integer.parseInt(mm2);
                int m33=Integer.parseInt(mm3);
                int m44=Integer.parseInt(mm4);
                int m55=Integer.parseInt(mm5);
                int obb=m11+m22+m33+m44+m55;
                ob1.setText(""+obb);
                float pers=obb*100/500;
                perc1.setText(""+pers);
            }
        }
    }
    Now I want to use my that marksheet class within the main class that's Multi.
    Last edited by PrinceT; 01-31-2015 at 07:19 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Multiple Classes in One Package?

    variables declaration not allowed here.
    Can you post the full text of the error message that shows the source line that was flagged and its line number in the program?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    PrinceT is offline Member
    Join Date
    Dec 2014
    Posts
    17
    Rep Power
    0

    Default Re: Multiple Classes in One Package?

    Quote Originally Posted by Norm View Post
    Can you post the full text of the error message that shows the source line that was flagged and its line number in the program?
    this is the error:
    run:
    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at multi.Multi.actionPerformed(Multi.java:214)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6525)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:629 0)
    at java.awt.Container.processEvent(Container.java:223 4)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4881)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2292)
    at java.awt.Component.dispatchEvent(Component.java:47 03)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739 )
    at java.awt.Component.dispatchEvent(Component.java:47 03)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 716)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:82)


    & the following line is flagged;
    Marksheet tani2=new Marksheet ();

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Multiple Classes in One Package?

    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at multi.Multi.actionPerformed(Multi.java:214)
    I've never seen that error message and don't know what it means.
    What code is at line 214?

    Why is the variable: tani2 created? It is never used.
    PrinceT likes this.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Multiple Classes in One Package?

    I believe this is a NetBeans related error. Something within NetBeans is out of sync so it can't compile the code. I haven't found any specific solution or cause though.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Multiple Classes in One Package?

    No, it's not an error of NetBeans. The IDE may put up a dialog which says (as nearly as I can remember) 'One or more source files failed to compile. Run anyway?'

    It's ok to go ahead if you know what you're doing; that is, if the file(s) that fail to compile will not be used for the current run. The problems start when people who don't know what they're doing think this is a magic wand that allows you to run any cr@p.

    db

    edit Tried to post a screenshot but it appears that function of the forum is also borked so I uploaded it here: http://i660.photobucket.com/albums/u...ps2e227748.jpg

    PrinceT: if you have checked the option to not show the dialog, here's how to reset that: http://stackoverflow.com/questions/2...errors-in-code
    Last edited by DarrylBurke; 02-01-2015 at 11:46 PM.
    PrinceT likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Multiple Classes in One Package?

    Quote Originally Posted by DarrylBurke View Post
    No, it's not an error of NetBeans.
    Are you certain it is not specific to NetBeans?? I use Eclipse and have never seen this particular error message. I also found this as a reported bug on NetBeans.org.

    "Netbeans will sometimes give me an "Uncompilable source code - Erroneous sym type" error then there is no error"

    Note: I am not saying this is the same error. Just that the similar wording is possibly germaine to NetBeans reporting on the OPs code.

    Regards,
    Jim
    Last edited by jim829; 02-02-2015 at 12:00 AM.
    PrinceT likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Multiple Classes in One Package?

    Quote Originally Posted by jim829 View Post
    "Netbeans will sometimes give me an "Uncompilable source code - Erroneous sym type" error then there is no error"
    That's a different situation, commonly resolved by doing a 'Clean and Build'. The OP posted
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException
    That could only happen when attempting to execute uncompilable code.

    db
    PrinceT likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    PrinceT is offline Member
    Join Date
    Dec 2014
    Posts
    17
    Rep Power
    0

    Default Re: Multiple Classes in One Package?

    Thanks a lot guys I did it.
    Java Code:
            if(b4==ta.getSource())
                Marksheet tani2=new Marksheet ();
    Doesn't work, but the following will work;
    Java Code:
            if(b4==ta.getSource()){
                Marksheet tani2=new Marksheet ();}

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Multiple Classes in One Package?

    Misleading. You changed this:

    Java Code:
    if(b4==ta.getSource())
      Marksheet tani2=new Marksheet ();
    }
    To this:

    Java Code:
    if(b4==ta.getSource()){
      Marksheet tani2=new Marksheet ();
    }
    Because indeed the opening curly brace was missing.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Multiple Classes in One Package?

    @OP. Thanks for the vote of confidence. Unfortunately I was mistaken in my analysis.

    Regards,
    Jim
    PrinceT likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    PrinceT is offline Member
    Join Date
    Dec 2014
    Posts
    17
    Rep Power
    0

    Default Re: Multiple Classes in One Package?

    Quote Originally Posted by gimbal2 View Post
    Misleading. You changed this:

    Java Code:
    if(b4==ta.getSource())
      Marksheet tani2=new Marksheet ();
    }
    To this:

    Java Code:
    if(b4==ta.getSource()){
      Marksheet tani2=new Marksheet ();
    }
    Because indeed the opening curly brace was missing.
    no bro that was closing of actionPerformed method.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Multiple Classes in One Package?

    Well, I just did a little test with Eclipse. Here is the code:

    Java Code:
    public class Test {
       public static void main(String[] args) {
          int a = 10;
          if (a == 10)
             int abc = 1;
       }
    }
    This the abc assignment causes a syntax error. Here is the complaint.

    Multiple markers at this line
    - Syntax error, insert ". class" to complete Expression
    - Syntax error, insert "AssignmentOperator Expression" to complete
    Assignment
    - The left-hand side of an assignment must be a variable
    - Syntax error, insert ";" to complete Statement
    - abc cannot be resolved to a variable
    If you put curly braces around the logical if expression, then the error goes away. I have never encountered this before. Probably because the variable is local and immediately goes out of scope so there is no point in doing it. This is probably covered in the JLS.

    Edit: Looking thru the JLS, section 14 (Blocks and Statements) I could not find a specific paragraph which said that a declaration may not be the target of an if statement. But the section does discuss what types of statements are allowed. None of these include declaration statements unless an explicit block is present.

    Regards,
    Jim
    Last edited by jim829; 02-02-2015 at 05:17 PM.
    PrinceT likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Multiple Classes in One Package?

    Quote Originally Posted by jim829 View Post
    Well, I just did a little test with Eclipse.
    <snip/>
    This the abc assignment causes a syntax error.
    Not the assignment. The declaration.

    This recent discussion might throw some light on why: Declaretion and Initialization on seperate statement in a class (Beginning Java forum at JavaRanch)

    JLS 14.4 says
    Every local variable declaration statement is immediately contained by a block.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Multiple Classes in One Package?

    Quote Originally Posted by DarrylBurke View Post
    Not the assignment. The declaration.
    Yeah, I knew that. I even said as much in my follow-up edit. Geez, you can't get away with anything in this forum. At least the font automatically dots the i's and crosses the t's. :)

    Regards,
    Jim
    Last edited by jim829; 02-02-2015 at 10:00 PM.
    PrinceT likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Connecting multiple classes in same package
    By Madhatter40 in forum New To Java
    Replies: 2
    Last Post: 01-18-2015, 11:16 PM
  2. Replies: 3
    Last Post: 03-27-2012, 09:27 AM
  3. HTML and Classes belonging to a package
    By alkismavridis in forum Java Applets
    Replies: 0
    Last Post: 10-25-2011, 06:31 PM
  4. Replies: 4
    Last Post: 02-07-2011, 07:38 PM
  5. get classes of a certain package
    By ashraf shaban in forum New To Java
    Replies: 4
    Last Post: 07-08-2008, 07:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •