Reputation: 117
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;
}
}
Upvotes: 1
Views: 1237
Reputation: 24616
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 :-)");
}
}
Upvotes: 4
Reputation: 47608
In your class F1, you show a form within a JOptionPane but you don't do anything with the user's response (ok or Cancel). You have a method actionPerformed but which is for an ActionListener, but you never register your ActionListener anywhere. I think you should simply treat the result of the JOptionPane. If OK, then invoke the code in your actionPerformed (which can be renamed).
NB: showConfirmDialog is a blocking call.
Upvotes: 2