Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5996971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:11:49+00:00 2026-05-23T00:11:49+00:00

I am having this problem in below code as my ArrayList both name and

  • 0

I am having this problem in below code as my ArrayList both name and values I wish to be displayed on the window is not appearing. It should be displayed at the bottom of the window but I set everything possible to setVisible true but still unable to display it. I think its a minor mistake but I cant see as it is my code. Sorry that the code is such a mess as I am writing this for my own understanding.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;


public class SamplePaper5a {

    static final int width = 500;
    static final int hight = 600;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JFrame frame = new JFrame("Exam");
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(width,hight);
        Container contain = new Container();
        contain = frame.getContentPane();

        contain.setLayout(new GridLayout(2,2));

        final ArrayList<String>names = new ArrayList<String>();
        final ArrayList<String>values = new ArrayList<String>();

        names.add("dian");
        names.add("maze");
        names.add("carl");
        names.add("John");
        names.add("tan");
        names.add("james");

        values.add("11111");
        values.add("2222");
        values.add("3333");
        values.add("4444");
        values.add("5555");
        values.add("6666");

        final JTextArea txtArea = new JTextArea();
        JButton butt = new JButton(" Enter ");
        contain.add(txtArea);
        contain.add(butt);

        butt.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub                                  
                for(int i=0;i<values.size();i++)
                {
                    txtArea.setText( txtArea.getText()+" \n "+values.get(i) +"  "+ names.get(i));                       
                }
            }               
        });

        try
        {
            contain.add(new NamePanel2(names,values,1));

        }

        catch(Size2Exception e)
        {
            System.out.print(e);
        }

        frame.setVisible(true);
    }   
}

class NamePanel2 extends JPanel 
{
    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> values = new ArrayList<String>();
    int cols;

    public NamePanel2(ArrayList<String> nam,ArrayList<String> val,int c) throws Size2Exception
    { 
        this.names = nam;
        this.values =val;
        this.cols = c;
        JPanel panel = new JPanel();

        if(names.size()!= values.size())
            throw new Size2Exception(" wrong sizes");

        panel.setLayout(new GridLayout(names.size(),cols));

        for(int i = 0; i<names.size();i++)
        {
            panel.add(new JLabel(names.get(i)));
            panel.add(new JTextField(values.get(i)));
        }
        panel.setVisible(true); 
    }       
}

class Size2Exception extends Exception
{
    public Size2Exception(String str)
    {
        super(str);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:11:50+00:00Added an answer on May 23, 2026 at 12:11 am

    See the comments for the nature of the fix + a few tips.

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    import javax.swing.*;
    
    
    public class SamplePaper5a {
    
        static final int width = 500;
        static final int hight = 600;
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            JFrame frame = new JFrame("Exam");
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
            frame.setSize(width,hight);
            // don't mix Swing with AWT
            JPanel contain = new JPanel();
            System.out.println(contain.getLayout());
            frame.setContentPane(contain);
    
            contain.setLayout(new GridLayout(2,2));
    
            final ArrayList<String>names = new ArrayList<String>();
            final ArrayList<String>values = new ArrayList<String>();
    
            names.add("dian");
            names.add("maze");
            names.add("carl");
            names.add("John");
            names.add("tan");
            names.add("james");
    
            values.add("11111");
            values.add("2222");
            values.add("3333");
            values.add("4444");
            values.add("5555");
            values.add("6666");
    
            final JTextArea txtArea = new JTextArea();
            JButton butt = new JButton(" Enter ");
            contain.add(txtArea);
            contain.add(butt);
    
            butt.addActionListener(new ActionListener()
            {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    for(int i=0;i<values.size();i++)
                    {
                        txtArea.setText( txtArea.getText()+" \n "+values.get(i) +"  "+ names.get(i));
                    }
                }
            });
    
            try
            {
                contain.add(new NamePanel2(names,values,1));
    
            }
    
            catch(Size2Exception e)
            {
                System.out.print(e);
            }
    
            frame.setVisible(true);
        }
    }
    
    class NamePanel2 extends JPanel
    {
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<String> values = new ArrayList<String>();
        int cols;
    
        public NamePanel2(ArrayList<String> nam,ArrayList<String> val,int c) throws Size2Exception
        {
            this.names = nam;
            this.values =val;
            this.cols = c;
            // unnecessary
            //JPanel panel = new JPanel();
    
            // it would be better to define an Object that contains a name/value pair
            if(names.size()!= values.size())
                throw new Size2Exception(" wrong sizes");
    
            setLayout(new GridLayout(names.size(),cols));
    
            for(int i = 0; i<names.size();i++)
            {
                add(new JLabel(names.get(i)));
                add(new JTextField(values.get(i)));
            }
            // unnecessary
            //panel.setVisible(true);
        }
    }
    
    class Size2Exception extends Exception
    {
        public Size2Exception(String str)
        {
            super(str);
        }
    }
    

    Screen Shot

    enter image description here

    BTW – the GUI looks quite odd. I’ll leave that for you to sort out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert this test code to C# and having a problem
I'm having this problem, same as ever, but never try to find the right
I'm having this problem and I reached a deadlock, I would try anything I've
I have been trying to tackle this problem , but I am having difficulty
I am having virtually the same problem as this: C# Update combobox bound to
I'm new to Ruby, so I'm having some trouble understanding this weird exception problem
This is a really weird problem that I have been having. When I download
This is a weird problem I have started having recently. My team is developing
I'm having a little problem with the following: When I execute this line: echo
I am having a problem with links wrapping. How do I prevent this? ![Wordwrap][1]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.