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

Thread: First GUI Problem

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default First GUI Problem

    I am trying to teach myself some java but I have run into some difficulty in trying to create a GUI. I have made a couple of simple ones but just by copying code and trying to figure out what is going on. I found this code and have been trying to figure things out. I have made a couple of changes already and am down to one error on line 14 "c.add(e);" I am stumped and was wondering if anyone can see what I might be missing.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class simpleForm extends JFrame {
      private JPanel contentPanel;
      private JLabel jLabel1;
      private JLabel jLabel2;
      private JTextField jTextField1;
      private JButton jButton1;
     
      private void addElement (Container c, Component w, int x, int y, int h, int j) {
        w.setBounds(x, y, h, j); 
        c.add(e);
      }
     
      simpleForm() { 
        contentPanel = (JPanel)getContentPane();
        contentPanel.setLayout(null); // a blank form
        jLabel1 = new JLabel("Action:");
        addElement(contentPanel, jLabel1,0,0,100,20); 
        jTextField1 = new JTextField(); 
        addElement(contentPanel, jTextField1,0,20,100,20); 
        jButton1 = new JButton("Click Me");
        addElement(contentPanel, jButton1,0,100,100,20); 
     
        jButton1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) { 
            jTextField1.setText("Button clicked"); 
          } 
        }); 
     
        setTitle("A Simple Form"); 
        setSize(200, 200); // The GUI dimensions 
        setLocation(new Point(150, 150)); //The GUI position
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true); 
      }
     
      public static void main(String args[]) {
        new simpleForm(); 
      }
    }
    Last edited by pbrockway2; March 30th, 2013 at 09:58 PM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    971
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: First GUI Problem

    c.add(e);
    What is e supposed to represent?

    The reason for the compiler message is that you are using the variable e, but it not declared anywhere.

    What do you intend your addElement() method to do?

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: First GUI Problem

    e is connected to the ActionEvent in line 28. I tried declaring it in the addElement parameters, but it did not like that.

    As I said, this is not my original code. I am trying to learn by recreating and analysing existing code. So ultimately I am not really sure what the point of c.add(e) is even for. I can remove that line of code and the program runs but I only get a blank frame with nothing inside. So......

    What I am trying to do is create a frame and add some basic elements within the frame.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    971
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: First GUI Problem

    Honestly, if you can't say what c.add(e) is supposed to do, I can't really say how it could be changed to do that.

    ---

    In my opinion copying other people's code isn't such a great way to learn a technology - not unless the code is full of comments and works step by step elaborating the technology it is trying to illustrate. And in that case it becomes a tutorial.

    But that's just my opinion... and if you want to copy that code, make sure you have copied it correctly. There's a good chance that you have made a typo in the line that says "c.add(e);"

    Unless you copied the code from here in which case the error is in the original. But that page *does* explain what the addElement() method is supposed to do, and what the w parameter is.

    ---

    I feel a bit mean not saying what the error is. So try this:

    private void addElement (Container c, Component w, int x, int y, int h, int j) {
        w.setBounds(x, y, h, j); // set the size and bounds of the widget 
        c.add(w); // add the widget to the container.
    }

    Note that the line is "c.add(w)".

    That said the code is a bit cr@ppy. Not just the typo (although that's symptomatic), but it's not Java style to start the class name with a lower case letter, and not generally good Swing practice to use the setBounds() way of laying out widgets within their container.

    If I knew a really good Swing introductory tutorial I'd recommend it. Perhaps others here do.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    shodai (March 31st, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: First GUI Problem

    Thank you so much for taking the time to find the error. You are totally correct that trying to reverse engineer a persons code is not a great approach, but when I look up tutorials I seem to find that there is no consistency in how people go about doing things. If anyone has a resource or two that they think would be helpful I would be extremely grateful.

    Thanks again pbrockway2.

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    971
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: First GUI Problem

    You're welcome.

    Oracle's Tutorial is very good - arguably the best - but the Swing section assumes some familiarity with Java and it attempts to be rather comprehensive so it's long. Also it presents things a piece at a time rather disconnectedly rather than building up a modest application which is probably a better way to proceed if you're new to programming and/or Swing. Hence my reluctance to simply point you there and say grumpily "don't copy code".

    Whatever you do, understand every line. And any surrounding textual comment. In this case the author did discuss "widgets" enough to give a clue about what was going on (provided you knew enough about Swing to already have a fair idea what was going on!. You can find every method documented in the API docs, so bookmark that resource and consult it so that no method call is a mystery.

    If you find things on the web that don't wrok or are unclear, ask about them on forums like this. And don't be afraid to give the url: the more information, the better.

    When I started to write code, I commented things. A lot. Probably I over commented them, but that's by far a lesser evil and easily fixed later with the delete key. But the act of commenting on discoveries as I made them helped to fix them in my mind. Java comments and meaningful variable names are essential for anything more complex than "Hello World" and the compiler isn't going to help you here: you have to be disciplined and do this for yourself. The easiest thing is if the discipline becomes a habit. The method in question could be rewritten:

        /**
         * Sets the size and position of a component and adds it to a container.
         *
         * @param  con     the container to which the component will be added
         * @param  widget  the component to be added
         * @param  left    the x- coordinate of the component's position
         * @param  top     the y- coordinate of the component's position
         * @param  width   the width to make the component
         * @param  height  the height to make the component
        */
    private void addElement (Container con, Component widget, int left, int top, int width, int height) {
        widget.setBounds(left, top, width, height); 
        con.add(widget);
    }

    (Notice the order of the parameters in setBounds(). The author had called the third one "h" suggesting he thought it was height. This is the sort of mistake I make and it's corrected by using proper variable names and looking up the setBounds() documentation.)

    Good luck, and happy coding!

Similar Threads

  1. problem with coordinates (GUI)
    By Django in forum What's Wrong With My Code?
    Replies: 14
    Last Post: March 8th, 2013, 12:36 PM
  2. Very simple GUI problem
    By clydefrog in forum AWT / Java Swing
    Replies: 8
    Last Post: March 20th, 2012, 11:37 AM
  3. GUI problem
    By Fantasy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 21st, 2011, 10:09 PM
  4. GUI problem
    By Reem in forum AWT / Java Swing
    Replies: 6
    Last Post: November 15th, 2010, 09:45 AM
  5. GUI Problem
    By bulldog in forum AWT / Java Swing
    Replies: 1
    Last Post: December 11th, 2009, 01:47 PM