GUI example won't work
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: GUI example won't work

  1. #1
    Join Date
    Jul 2009
    Location
    USA
    Posts
    49

    Exclamation GUI example won't work

    I got this code from the Java Sun site.

    Why won't it run?

    Code:
    package components;
    
    /* TextDemo.java requires no other files. */
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class TextDemo extends JPanel implements ActionListener {
        protected JTextField textField;
        protected JTextArea textArea;
        private final static String newline = "\n";
    
        public TextDemo() {
            super(new GridBagLayout());
    
            textField = new JTextField(20);
            textField.addActionListener(this);
    
            textArea = new JTextArea(5, 20);
            textArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(textArea);
    
            //Add Components to this panel.
            GridBagConstraints c = new GridBagConstraints();
            c.gridwidth = GridBagConstraints.REMAINDER;
    
            c.fill = GridBagConstraints.HORIZONTAL;
            add(textField, c);
    
            c.fill = GridBagConstraints.BOTH;
            c.weightx = 1.0;
            c.weighty = 1.0;
            add(scrollPane, c);
        }
    
        public void actionPerformed(ActionEvent evt) {
            String text = textField.getText();
            textArea.append(text + newline);
            textField.selectAll();
    
            //Make sure the new text is visible, even if there
            //was a selection in the text area.
            textArea.setCaretPosition(textArea.getDocument().getLength());
        }
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event dispatch thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("TextDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Add contents to the window.
            frame.add(new TextDemo());
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event dispatch thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

  2. #2
    Join Date
    Oct 2008
    Posts
    77

    Re: GUI example won't work

    wow, is is that hard to make package 'components', compile the project with single file and run it via its main method

    it works
    http://img15.imageshack.us/img15/2164/textdemo.jpg

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: GUI example won't work

    Quote Originally Posted by coder752 View Post
    Why won't it run?
    It works fine here.

    Dude - we're not mind readers - if you want help with something, give us information - how are you trying to run it? what happens when you try to run it? What did you expect to happen? Do you get an error message? If so, post up the full text of the error message.

    Judge a man by his questions, rather than his answers...
    Voltaire
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Jul 2009
    Location
    USA
    Posts
    49

    Wink Re: GUI example won't work

    Error message reads like this:

    Exception in thread "main" java.lang.NoClassDefFoundError: TextDemo (wrong name:
    components/TextDemo)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    Could not find the main class: TextDemo. Program will exit.

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: GUI example won't work

    Quote Originally Posted by coder752 View Post
    Error message reads like this:

    Exception in thread "main" java.lang.NoClassDefFoundError: TextDemo (wrong name:
    components/TextDemo)
    ...
    Could not find the main class: TextDemo. Program will exit.
    OK, you didn't answer my question about how you ran it, which isn't very helpful, but I'll take a guess anyway...

    The TextDemo class is in package directory 'components', so its fully qualified name is 'components.TextDemo', but I'm guessing you told it to run just 'TextDemo' instead, from inside the 'components' directory. The runtime read the TextDemo class, saw the 'components' package declaration and realised the name was wrong. When you run classes, you should run them from the directory that contains the root package directory, and specify the fully qualified name (i.e. include the package or packages in the name).

    This is very basic stuff - I recommend you learn about packages and how to run a Java program with java.exe.

    Unless in communicating with it [a computer] one says exactly what one means, trouble is bound to result...
    A. Turing
    Last edited by dlorde; August 1st, 2009 at 01:49 PM.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    Jul 2009
    Location
    USA
    Posts
    49

    Resolved Re: GUI example won't work

    Ok, so I transferred my code to a new directory called components. I placed TextDemo.java in there. I added this line of code in the heading as well
    Code:
    import components.*;
    Next...
    I did a javac TextDemo.java (compiles)
    Then I did java components.TextDemo

    I ran it, but again, it gave me the same error.




    I read the Java Sun Tutorial, helpful in other ways, just not this one.

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: GUI example won't work

    Quote Originally Posted by coder752 View Post
    I added this line of code in the heading as well
    Code:
    import components.*;
    Why? There are no other classes involved - the comments in the code itself say no other files are required.

    I did a javac TextDemo.java (compiles)
    Then I did java components.TextDemo

    I ran it, but again, it gave me the same error.
    You probably missed the bit in my previous post where I said:
    When you run classes, you should run them from the directory that contains the root package directory
    This will be the directory where you put 'components'. The Java runtime expects to be pointed to the directory that contains the packages (this directory is often called 'classes'). You can do this either by being in that directory when you run it, or by putting that directory on the classpath so Java knows where to look for the packages. Setting the classpath is probably the next thing you'll learn, so for now just run it from the directory that contains the 'components' directory. So when you run it, it should see the directory 'components' that matches the package name in the Java command, and inside that it will find TextDemo.class, which matches the class name in the Java command.

    If you understand what you're doing, you're not learning anything...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center