Results 1 to 9 of 9
  1. #1
    nicocannon is offline Member
    Join Date
    Dec 2014
    Posts
    30
    Rep Power
    0

    Post My first Swing application

    Hey guys Im knew to java and swing and I have just finished my first application using swing. It is a basic calculator and I wanted some feedback on it to see what i might need work in in swing and java. The github address is here: https://github.com/nicodasiko/SimpleCalc
    Thanks guys!

  2. #2
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: My first Swing application

    I wouldn't normally be up for providing free code reviews for entire projects. We are all volunteers after all.

    Is there a particular part of your program that you would like to discuss? If so, provide some information about that and ask a specific question.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: My first Swing application

    - It's good practice to be as defensive as possible. If there is no very good reason for default access to your instance variables, make them private.
    Java Code:
    String cmd = ""
    becomes
    Java Code:
    private String cmd = ""
    - Repeat the above for your methods:
    Java Code:
    void saveFile() {...}
    becomes
    Java Code:
    private void saveFile() {...}
    - Another good practice: design by contract. Don't use the implementation class for the declaration of the collection, but use the interface instead.
    Java Code:
    ArrayList<String> strNums = new ArrayList<String>();
    becomes
    Java Code:
    List<String> strNums = new ArrayList<String>();
    - Classnames start with a uppercase letter and are CamelCase. Also the inner classes.
    - Don't use == to check for String equality. String has a method for that.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    nicocannon is offline Member
    Join Date
    Dec 2014
    Posts
    30
    Rep Power
    0

    Default Re: My first Swing application

    Yeah this is great thanks. I was a but sceptical about all the button event handlers even for all the numbers (0 through 9) I don't like copying code like that and I couldn't really find a way around it. I would have liked to have one event handler for all the number buttons to keep the file shorter. The ActionEvent object that is passed through actionPerformed am I able to access like an instance variable or something in it to know which button fired the event instead of having a different inner class for each button. And the == thing is an old habit from C and C++.

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

    Default Re: My first Swing application

    Also, as of Java 1.6 I believe, you can do this:

    Java Code:
     List<String> list = new ArrayList<>(); // you don't need the second generic type.
    However, if you create anonymous instances, you do need it.

    Java Code:
    List<String> list  = new ArrayList<String>() {
              // stuff here if you need to override or initialize something.
    }
    ;


    Don't use == to compare strings. Use equals(). You probably got away with it since you were using string constants which are retrieved from a pool.

    And use the same listener for all of your similar buttons. It will greatly reduce your code. In your case, you can set the action command to be the number and just add it when the event is received. In fact, you don't even need instances of your buttons to hang around in your code. You can also get the source of the event from a listener by using the getSource() method of the event argument.

    Java Code:
    public void actionPerformed(ActionEvent ae) {
             setNum(ae.getActionCommand());
    }
    You could also use the button name.

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

  6. #6
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: My first Swing application

    Quote Originally Posted by jim829 View Post
    Also, as of Java 1.6 I believe, you can do this:

    Java Code:
     List<String> list = new ArrayList<>(); // you don't need the second generic type.
    That feature was added in Java 7

  7. #7
    nicocannon is offline Member
    Join Date
    Dec 2014
    Posts
    30
    Rep Power
    0

    Default Re: My first Swing application

    Oh ok so the getActionCommand method will return which button fired the event instead of having an event for every button?

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

    Default Re: My first Swing application

    Quote Originally Posted by trcooke View Post
    That feature was added in Java 7
    Wasn't certain and I didn't take the time to check the JLS.

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

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

    Default Re: My first Swing application

    Quote Originally Posted by nicocannon View Post
    Oh ok so the getActionCommand method will return which button fired the event instead of having an event for every button?
    Yes, there are lots of different ways to use common listeners. One is to get the source from the event object. Then you can use instanceof to check for the desired class. You can then cast the source to the proper class and use any of its methods. And quite often you don't need to keep any of the objects around. For example, you could do this:

    Java Code:
    JPanel panel = new JPanel();
    MyButtonListener myButtonListener = new MyButtonListener();
    for (int j = 0; j < 10; j++) {
         String s = Integer.toString(j);
         JButton b = new JButton(s);
         b.setActionCommand(s);
         b.addActionListener(myButtonListener);
         panel.add(b);
    }
    You don't really need to keep the buttons around since they are in the panel and will generate the appropriate events. But there are always exceptions. Sometimes one wants to see if getSource() is equal to a particular object. So you need to have a reference to the object. And if the buttons do drastically different things, it may make sense to have different listeners. It can make the code easier to read too.

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

Similar Threads

  1. Swing Application
    By Zelaine in forum New To Java
    Replies: 2
    Last Post: 09-13-2014, 07:35 PM
  2. Execution of Swing application on another pc
    By ksaketh2 in forum Advanced Java
    Replies: 5
    Last Post: 04-09-2014, 02:27 PM
  3. Existing swing application
    By macason in forum AWT / Swing
    Replies: 3
    Last Post: 11-30-2009, 10:51 PM
  4. Better Swing Application Framework
    By etf in forum Java Software
    Replies: 1
    Last Post: 09-07-2009, 02:25 PM
  5. designing application in swing
    By programmer_007 in forum AWT / Swing
    Replies: 12
    Last Post: 09-03-2009, 07:50 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
  •