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

Thread: Trouble with my applet

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Location
    Maine
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Trouble with my applet

    Hey folks,
    i've been working on this program here, and i've hit a roadblock. Everything compiles fine, and it runs alright. As you can see from the code, there's a window that opens with two buttons, one to display the contents of the ArrayList and another with a form that allows you to add a contact. The latter will not work, however, and i can't for the life of me figure out why. Here's the code, any help would be appreciated.

    /**
     * This applet runs and provides two options; saving a new contact or displaying the database. Choosing to
     * view the database opens a window that will display the contents of the database. Choosing to save a new
     * contact will open up a window with a form that allows for the creation of a contact. Eventually, the 
     * program will save this database into a .txt file, allowing for permanent storage of the ArrayList of 
     * contacts present in the interface Storage.
     * 
     * Cdeed  
     * 4/28/11 
     * version 1.3
     */
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MainPage extends JApplet implements Storage
    {
        public void init()
        {
            JLabel title = new JLabel("Contact Database");
            JButton save = new JButton("New Contact");
            save.addActionListener(new ButtonListener());
            JButton view = new JButton("View Database");
            view.addActionListener(new ButtonListener1());
     
            Container c = getContentPane();
            c.setBackground(Color.WHITE);
            c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
            c.add(title);
            c.add(save);
            c.add(view);
        }
     
        private class ButtonListener implements ActionListener
        {
            private JTextField input, i1, i2, i3, i4;
            public void actionPerformed(ActionEvent event)
            {
                JFrame frame = new JFrame("New Contact");
                frame.getContentPane().setBackground(Color.WHITE);
                frame.getContentPane().setLayout(new FlowLayout());
     
                JLabel label = new JLabel ("Name");
                input = new JTextField (15);
     
                JLabel l1 = new JLabel ("Address");
                i1 = new JTextField (30);
     
                JLabel l2 = new JLabel ("Cell Number");
                i2 = new JTextField (15);
     
                JLabel l3 = new JLabel ("Home Number");
                i3 = new JTextField (15);
     
                JLabel l4 = new JLabel ("E-Mail");
                i4 = new JTextField (30);
     
                JButton save = new JButton("Save");
                save.addActionListener(new ButtonListener2());
     
                frame.getContentPane().add(save);
                frame.getContentPane().setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));
                frame.getContentPane().add(label);
                frame.getContentPane().add(input);
                frame.getContentPane().add(l1);
                frame.getContentPane().add(i1);
                frame.getContentPane().add(l2);
                frame.getContentPane().add(i2);
                frame.getContentPane().add(l3);
                frame.getContentPane().add(i3);
                frame.getContentPane().add(l4);
                frame.getContentPane().add(i4);
                frame.pack();
                frame.setVisible(true);
            }
     
            private class ButtonListener2 implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                    Person p1 = new Person(i2.getText(), i3.getText(), input.getText(), i1.getText(), i4.getText());
                    contacts.add(p1);
                    input.setText("Saved!");
                    i1.setText("Saved!");
                    i2.setText("Saved!");
                    i3.setText("Saved!");
                    i4.setText("Saved!");
                }
            }
        }
     
        private class ButtonListener1 implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
     
                JFrame frame = new JFrame("Contacts");
                frame.getContentPane().setBackground(Color.WHITE);
                frame.getContentPane().setLayout(new FlowLayout());
     
                for(Person a: contacts)
                {
                    JLabel b1 = new JLabel(a.toString());
                    frame.getContentPane().add(b1);
                }
     
                frame.pack();
                frame.setVisible(true);
            }
        }
    }
    Last edited by cdeed; April 29th, 2011 at 08:35 AM. Reason: Put code tags around code


  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: Trouble with my applet

    What do you mean when you say that it "will not work"?
    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 2011
    Location
    Maine
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with my applet

    Sorry, i should have been more clear. When the first JFrame opens up in my editor's (BlueJ) applet viewer, it displays the two JButtons "save" and "view". When "view" is clicked on, another JFrame is opened that would contain the ArrayList full of contacts were it filled with some. When "save" is pressed, it should open a new JFrame that contains several JLabels and JTextFields that allow the user to input information such as a name, address, phone number etc. Instead, clicking the button does nothing, not even opening a new JFrame. I'm not sure if this is due to the ButtonListener classes, or if i'm missing something else entirely. If past experience is anything to go by, then it's probably something relatively simple that i can't seem to find. If you need any additional code, like possibly the Storage interface, i can post that as well.

  4. #4
    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: Trouble with my applet

    Hmm, this seems like it should work. Are you sure the JFrame isn't just popping up under the window the applet is in?

    I'd love to test this, but I can't do that without an SSCCE- the shorter, the better.
    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!

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Location
    Maine
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with my applet

    I checked under the window, it's not there. The other button pops up fine enough, so i'm not entirely sure. As for the SSCCE, it compiles but i can't exactly guarantee that i didn't accidentally take out the problem; hopefully that's not the case.

    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class MainPage2 extends JApplet
    {
        public void init()
        {
            JButton save = new JButton("New Contact");
            save.addActionListener(new ButtonListener());
     
            Container c = getContentPane();
            c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
            c.add(save);
        }
     
        private class ButtonListener implements ActionListener
        {
            private JTextField input, i1, i2, i3, i4;
            public void actionPerformed(ActionEvent event)
            {
                JFrame frame = new JFrame("New Contact");
                frame.getContentPane().setBackground(Color.WHITE);
                frame.getContentPane().setLayout(new FlowLayout());
     
                frame.getContentPane().setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));
     
                frame.setSize(100, 50);
                frame.setVisible(true);
            }
     
            private class ButtonListener2 implements ActionListener
            {
                public void actionPerformed(ActionEvent event)
                {
                }
            }
        }
    }
    Last edited by cdeed; April 29th, 2011 at 12:36 PM.

  6. #6
    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: Trouble with my applet

    Ah, the problem is this line:

    frame.getContentPane().setLayout(new BoxLayout(frame, BoxLayout.PAGE_AXIS));

    You're setting the layout of the JFrame's contentPane, but you're passing the JFrame (not the contentPane) into the BoxLayout constructor. This is generating an exception, which causes the method to exit before it shows the JFrame.

    Exception in thread "AWT-EventQueue-1" java.awt.AWTError: BoxLayout can't be shared
    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!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    cdeed (April 29th, 2011)

  8. #7
    Junior Member
    Join Date
    Apr 2011
    Location
    Maine
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with my applet

    Ahh, i would have never seen that. Thank you!

  9. #8
    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: Trouble with my applet

    No problem. It can be confusing because JFrame contains a few methods that actually are just convenience methods that access the contentPane automatically. So calling JFrame.setLayout() is actually the same as calling JFrame.getContentPane().setLayout(). Same with the add() method, and a couple others.

    Anyway, glad you got it sorted.
    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!

Similar Threads

  1. jTable trouble...
    By _lithium_ in forum AWT / Java Swing
    Replies: 3
    Last Post: March 6th, 2011, 07:05 PM
  2. trouble with GUI
    By MrHardRock in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 04:00 PM
  3. Having trouble with a While statment
    By java0 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 27th, 2010, 12:37 PM
  4. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 02:49 PM
  5. Having trouble with strings
    By Reaperkid77 in forum Java SE APIs
    Replies: 3
    Last Post: October 20th, 2009, 06:30 PM

Tags for this Thread