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

Thread: Stopping the entries in a JFrame

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Stopping the entries in a JFrame

    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
     
     
    public class Array extends JFrame{
     
        private JTextField number = new JTextField(10);
        private JLabel result;
        int num;
        ArrayList<Integer> list = new ArrayList<>();
     
     
        public static void main(String[] args){
     
            Array frame = new Array();
            frame.setTitle("Collections");
            frame.setSize(700, 300);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
     
     
        }
     
        public Array() {
     
     
            add(new JLabel("You will enter 5 integers."), BorderLayout.NORTH);
            setLayout(new FlowLayout(FlowLayout.LEFT, 10,20));
            add(new JLabel("Enter a number and click the enter button:"));
            add(number);
     
            JButton jbtEnter = new JButton("Enter");
            jbtEnter.setToolTipText("This will store your number in the program");
            add(jbtEnter);
     
            jbtEnter.addActionListener(new EnterListener());
     
     
            JButton jbtDisplay = new JButton("Display");
            jbtDisplay.setToolTipText("This will display all the integers in numberical order");
            add(jbtDisplay);
     
            jbtDisplay.addActionListener(new DisplayListener());
     
            result = (new JLabel(""));
            add (result);
    }
        class EnterListener implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent e){
     
     
                try{
                int i = 0; 
                if (i < 5){
     
     
                num = Integer.parseInt(number.getText());
                list.add(num);
                number.setText(null);
                i++;
     
                }
                catch (IndexOutOfBoundsException ex){
                    JOptionPane.showMessageDialog(null, "Error. You cannot exceed the limit of 5 entries.  click other button.", "Error", JOptionPane.ERROR_MESSAGE);
                }
     
             }
        }
     
     
        class DisplayListener implements ActionListener{
     
            @Override
            public void actionPerformed(ActionEvent e) {
     
     
                Collections.sort(list);
                result.setText("The numbers you have entered, listed in numerical order is: " + list );
                }
     
        }
     
    }

    How can I make the user stop entering after 5 entries? the catch is to tell the user they can longer add entries.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Stopping the entries in a JFrame

    Also at java-forums.org

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Stopping the entries in a JFrame

    There's one big thing I noticed. The variable i is being defined inside that listener. It is set to 0.

    Every time they click the button, it will be 0 again. If you changed it to be a class variable or something instead and set it to 0 in the constructor, it would eventually reach 5, though it's not, I think, ever going to throw an IndexOufOfBoundsException.

    Just have an if and else there instead of the try catch I think.

Similar Threads

  1. Cumulative sum of entries
    By CSUTD in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 1st, 2011, 06:32 PM
  2. PhoneBook entries with ArrayList problem
    By gpelefty9 in forum Collections and Generics
    Replies: 3
    Last Post: October 30th, 2011, 03:32 AM
  3. [SOLVED] Stopping java in terminal
    By Scotty in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2011, 06:53 PM
  4. While loop stopping?
    By Echo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 16th, 2010, 05:40 PM
  5. HELP!!! Entry Class to represent entries in a telephone directory
    By Princess D in forum Java Theory & Questions
    Replies: 10
    Last Post: January 22nd, 2010, 05:39 AM