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

Thread: Class inside a class

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Class inside a class

    Hello,

    I'm trying to create a class (TextAnalyzer) which contains another class (GUI_Interface). I want to create a GUI_Interface object (called fereastra) in TextAnalyzer's public static void main() function. But I get an error here: GUI_Interface fereastra = new GUI_Interface();

    I'm using Eclipse JUNO. If I try to write the classes separately (not one inside the other), it also shows an error, telling me that I have to put each class into its separate file. I'd like to avoid doing that because I'll use a lot of classes (it's a complex project), and I'm trying group the classes as much as I can.

    This is my code:
     
    import javax.swing.*;
    import java.awt.Color;
     
     
     
    public class TextAnalyzer {
     
     
    	public class GUI_Interface extends JFrame{
     
    		public GUI_Interface(){
     
    			super("Psy Analyzer");
    			super.setBounds(100, 50, 800, 800);
    			super.setBackground(Color.WHITE);
     
    			JPanel content  = new JPanel();
     
    			JLabel eticheta = new JLabel("Drag file here");
    			content.add(eticheta);
     
    			this.getContentPane().add(content);
     
    		}
     
    	}
     
    	public static void main(String args[]){
     
    		GUI_Interface fereastra = new GUI_Interface();		
    		//window 
    		fereastra.setVisible(true);
    	}
     
     
     
     
     
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Class inside a class

    If I try to write the classes separately (not one inside the other), it also shows an error, telling me that I have to put each class into its separate file.
    The name of the java file has to be identical to the class name.
    I'd like to avoid doing that because I'll use a lot of classes (it's a complex project), and I'm trying group the classes as much as I can.
    Bad idea. Use packages.

    Also you should start a GUI from the EDT. You're asking for trouble with that code.

Similar Threads

  1. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  2. Inside the Graphics class
    By Garaen in forum Java Theory & Questions
    Replies: 2
    Last Post: September 2nd, 2012, 09:31 PM
  3. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  4. Retrieving arrays of java class inside C++ code
    By sattu in forum Java Native Interface
    Replies: 2
    Last Post: May 20th, 2011, 02:15 AM
  5. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 12:45 PM

Tags for this Thread