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.


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
    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.setSize(700, 300);
        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:"));
            JButton jbtEnter = new JButton("Enter");
            jbtEnter.setToolTipText("This will store your number in the program");
            jbtEnter.addActionListener(new EnterListener());
            JButton jbtDisplay = new JButton("Display");
            jbtDisplay.setToolTipText("This will display all the integers in numberical order");
            jbtDisplay.addActionListener(new DisplayListener());
            result = (new JLabel(""));
            add (result);
        class EnterListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
                int i = 0; 
                if (i < 5){
                num = Integer.parseInt(number.getText());
                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{
            public void actionPerformed(ActionEvent e) {
                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
    Moderators pbrockway2's Avatar
    Join Date
    Jan 2012
    Thanked 284 Times in 256 Posts

    Default Re: Stopping the entries in a JFrame

    Also at java-forums.org

  3. #3
    Registered Users javapenguin's Avatar
    Join Date
    May 2010
    North Central Illinois
    My Mood
    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.
    Please mark solved threads as "Solved" by going to threads and clicking "Mark Solved"
    Using code tags

    How To ask questions the smart way
    Java Tutorials
    Java API
    Save Our Snips
    Hit THANKS to thank a person. Hit REP+ to increase reputation (or decrease it). Cannot give to same person for a while.
    Shooting Park Road Baptist Church Site

Similar Threads

  1. Cumulative sum of entries
    By CSUTD in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 1st, 2011, 18:32
  2. PhoneBook entries with ArrayList problem
    By gpelefty9 in forum Collections and Generics
    Replies: 3
    Last Post: October 30th, 2011, 03:32
  3. [SOLVED] Stopping java in terminal
    By Scotty in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2011, 18:53
  4. While loop stopping?
    By Echo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 16th, 2010, 17:40
  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, 04:39