Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to add object to ArrayList throught input by jtextField()
    primarykey
    data
    text
    <p>When you press the <code>JButton</code> <kbd>Ny</kbd> the user input is supposed to be added as an object(class <code>Deltagare</code>) in an <code>ArrayList</code>, and when the user later press the <code>JButton</code> <kbd>Visa</kbd>, it should show up in the <code>JTextArea</code>.</p> <p>The problem is that the textArea only displays an empty Arraylist, somehow the input isn't added to the Arraylist.</p> <pre><code>import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; class Tävling extends JFrame { public static ArrayList&lt;Deltagare&gt; list = new ArrayList&lt;Deltagare&gt;(); 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); } } } </code></pre> <p>And the Class Deltagare:</p> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload