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 7751939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:40:36+00:00 2026-06-01T11:40:36+00:00

When you press the JButton Ny the user input is supposed to be added

  • 0

When you press the JButton Ny the user input is supposed to be added as an object(class Deltagare) in an ArrayList, and when the user later press the JButton Visa, it should show up in the JTextArea.

The problem is that the textArea only displays an empty Arraylist, somehow the input isn’t added to the Arraylist.

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

class Tävling extends JFrame
{   
    public static ArrayList<Deltagare> list = new ArrayList<Deltagare>();

    JTextArea text = new JTextArea();

    JRadioButton rb1 = new JRadioButton("Startnr", true);
    JRadioButton rb2 = new JRadioButton("Namn", false);
    JRadioButton rb3 = new JRadioButton("Ålder", false);
    JRadioButton rb4 = new JRadioButton("Tid", false);

    Tävling()
    {
        super ("DSV Kista Marathon");
        setLayout(new BorderLayout());

        //NORTH
        JPanel north = new JPanel();
        north.add(new JLabel("DSV Kista Marathon"));
        add(north, BorderLayout.NORTH);

        //CENTER
        add(new JScrollPane(text), BorderLayout.CENTER);
        text.setEditable(false);

        //EAST      
        JPanel east = new JPanel();
        east.setLayout(new BoxLayout(east, BoxLayout.Y_AXIS));
        east.add(new JLabel("Sortering"));

        east.add(rb1);
        east.add(rb2);
        east.add(rb3);
        east.add(rb4);

        ButtonGroup group = new ButtonGroup();
        group.add(rb1);
        group.add(rb2);
        group.add(rb3);
        group.add(rb4);

        add(east, BorderLayout.EAST);

        //SOUTH
        JPanel south = new JPanel();

        JButton b1 = new JButton("Ny");
        b1.addActionListener(new B1());
        south.add(b1);  

        JButton b2 = new JButton("Visa");
        b2.addActionListener(new B2());
        south.add(b2);

        JButton b3 = new JButton("Tid");
        b3.addActionListener(new B3());
        south.add(b3);

        add(south, BorderLayout.SOUTH);

        //Set
        setLocation(500,200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300,350);
        setVisible(true);

    }

    //Metod för att skapa startnr
    public int getStartnr()
    {
        int startnr = list.size()+1;
        return startnr;
    }

    public static void main(String[] args) 
    {
        new Tävling();
    }

    //Huvudklass slut ------------------------------------------------------    


    //b1 - Ny
    class B1 implements ActionListener
    {
        public void actionPerformed(ActionEvent ave) 
        {
            new F1();
        }   
    }

    //b2 - Visa
    class B2 implements ActionListener
    {
        public void actionPerformed(ActionEvent ave) 
        {
            if (rb1.isSelected())
                text.append(list.toString() + "\n");
            else if (rb2.isSelected())
                text.setText("Namn");
            else if (rb3.isSelected())
                text.setText("Ålder");
            else if (rb4.isSelected())
                text.setText("Tid");
        }   
    }

    //b3 - Tid
    class B3 implements ActionListener
    {
        public void actionPerformed(ActionEvent ave) 
        {
            new F2();
        }   
    }


    //JOptionPane - Ny (Deltagare)
    class F1 implements ActionListener
    {       
        JTextField nfield = new JTextField(12);
        JTextField cfield = new JTextField(12);
        JTextField afield = new JTextField(3);

        F1()
        {
            JPanel form = new JPanel();
            form.add(new JLabel("Startnr "+ getStartnr()));
            form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));

            JPanel r1 = new JPanel();
            r1.add(new JLabel ("Namn:"));
            r1.add(nfield);
            form.add(r1);
            nfield.addActionListener(this);

            JPanel r2 = new JPanel();
            r2.add(new JLabel ("Land:"));
            r2.add(cfield);
            form.add(r2);
            cfield.addActionListener(this);

            JPanel r3 = new JPanel();
            r3.add(new JLabel ("Ålder:"));
            r3.add(afield);
            form.add(r3);
            afield.addActionListener(this);

            JOptionPane.showConfirmDialog(null, form, "Ny Tävlande"
                                          , JOptionPane.OK_CANCEL_OPTION);
        }

        public void actionPerformed(ActionEvent ave) 
        {
            String name = nfield.getText();
            String country = cfield.getText();
            int age = Integer.parseInt(afield.getText());
            int startnr = getStartnr();

            list.add(new Deltagare(name, country, age, startnr));       
        }   
    }

    //JOptionPane - Tid (Registrera ny)
    class F2
    {       
        JTextField field1 = new JTextField(6);
        JTextField field2 = new JTextField(6);

        F2()
        {
            JPanel form = new JPanel();
            form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));

            JPanel r1 = new JPanel();
            r1.add(new JLabel ("Startnr:"));
            r1.add(field1);
            form.add(r1);

            JPanel r2 = new JPanel();
            r2.add(new JLabel ("Tid:"));
            r2.add(field2);
            form.add(r2);

            JOptionPane.showConfirmDialog(null, form, "Registrera Tid"
                                           , JOptionPane.OK_CANCEL_OPTION);

        }
    }   
}

And the Class Deltagare:

public class Deltagare
{
    public String name,country;
    public int age,startnr;

    public Deltagare(String n, String c, int a, int b)
    {
        this.name = "n";
        this.country = "c";
        this.age = a;   
        this.startnr = b;
    }

    public void setStartnr(int b)
    {
        startnr = b;
    }

    public int getStartnr()
    {
        return startnr; 
    }

    public String getName()
    {
        return name;    
    }

    public String getCountry()
    {
        return country; 
    }

    public int getAge()
    {
        return age; 
    }

    public String toString()
    {
        return startnr + "" + name + " " + country + " " + age;
    }

}
  • 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-06-01T11:40:38+00:00Added an answer on June 1, 2026 at 11:40 am

    First of all the constructor of your Deltagare class is doing wrong thing, replace

    public Deltagare(String n, String c, int a, int b)
    {
        this.name = "n";
        this.country = "c";
        this.age = a;   
        this.startnr = b;
    }
    

    with

    public Deltagare(String n, String c, int a, int b)
    {
        // removed quotes to assign value of n to name and same for country.
        this.name = n;
        this.country = c;
        this.age = a;   
        this.startnr = b;
    }
    

    Then, you had added actionListener to all your JTextField in F1 class, but there is nothing to capture what if user clicked on OK Button or CANCEL Button. For this do something like this, to take input inside the constructor of your F1:

    F1()
    {
        JPanel form = new JPanel();
        form.add(new JLabel("Startnr "+ getStartnr()));
        form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));
    
        JPanel r1 = new JPanel();
        r1.add(new JLabel ("Namn:"));
        r1.add(nfield);
        form.add(r1);
        nfield.addActionListener(this);
    
        JPanel r2 = new JPanel();
        r2.add(new JLabel ("Land:"));
        r2.add(cfield);
        form.add(r2);
        cfield.addActionListener(this);
    
        JPanel r3 = new JPanel();
        r3.add(new JLabel ("Ålder:"));
        r3.add(afield);
        form.add(r3);
        afield.addActionListener(this);
    
        int choice = JOptionPane.showConfirmDialog(null, form, "Ny Tävlande"
                                           , JOptionPane.OK_CANCEL_OPTION);
        // If the value of the user is OK, then do this, else do nothing.                             
        if (choice == JOptionPane.OK_OPTION)
        {
            String name = nfield.getText();
            String country = cfield.getText();
            int age = Integer.parseInt(afield.getText());
            int startnr = getStartnr();
    
            list.add(new Deltagare(name, country, age, startnr));
        }
        else if (choice == JOptionPane.CANCEL_OPTION)
        {
            System.out.println("CANCEL OPTION SELECTED, DO SOMETHING NOW :-)");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When my user press Enter on the virtual android user validate entry! keyboard my
I am a bit of stuck. When I press the button submit is should
If I press Ctrl + B that ought to give me ASCII code 2,
When I press F5 in Visual Studio 2008, I want Google Chrome launched as
When I press the standard Ctrl + E, C (an other variants) in VS2008
when I press the Maximize button on my WPF app, all the controls therein
If you press Shift+Ctrl+T or choose Navigate > Open Type... you get the Open
When I press F5, everything compiles fine, but when the app is to be
When I press Ctrl+other keys in a TextBox in VB 6.0, the system plays
When I press F5 the project compiles, and the new web page launches. However,

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.