Hi Guys,
i'm creating an Java app to add 2 different String values to an ArrayList ( it has to do with my Hobby Badminton )
I now have to following code below:
The problem is that when i want to show the current ArrayList with the "zoekButton" it will give me my last entered input. How can i fix this so that it will list my whole ArrayList?Code:import java.awt.event.*; import java.util.ArrayList; import java.util.Collections; import javax.swing.*; public class Badminton implements ActionListener { JButton knop1; JTextField tekstvak1; JTextField tekstvak2; JTextField tekstvak3; JTextArea uitvoer; JButton toernooiButton; JButton exitButton; JButton zoekButton; JButton wisButton; ArrayList<String> array1; ArrayList<String> array2; private JPanel getContent() { JLabel title = new JLabel ("Badminton Toernooien"); JPanel bannerpanel = new JPanel(); bannerpanel.setSize(300,50); bannerpanel.add(title); JLabel toernooiplek = new JLabel("Toernooi plek: "); tekstvak1 = new JTextField(30); JPanel plekPanel = new JPanel(); plekPanel.setLayout(new BoxLayout(plekPanel, BoxLayout.X_AXIS)); plekPanel.setSize(200, 20); plekPanel.add(toernooiplek); plekPanel.add(tekstvak1); JLabel toernooinaam = new JLabel ("Toernooi naam: "); tekstvak2 = new JTextField(30); JPanel tekstPanel = new JPanel(); tekstPanel.setLayout(new BoxLayout(tekstPanel, BoxLayout.X_AXIS)); tekstPanel.setSize(200,20); tekstPanel.add(toernooinaam); tekstPanel.add(tekstvak2); JPanel blankPanel = new JPanel(); blankPanel.setLayout(new BoxLayout(blankPanel, BoxLayout.X_AXIS)); blankPanel.setSize(200,20); toernooiButton = new JButton("Voeg toernooi toe"); exitButton = new JButton("Afsluiten"); zoekButton = new JButton("Geef Toernooien weer"); wisButton = new JButton("Wis invoer"); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.add(toernooiButton); buttonPanel.add(exitButton); buttonPanel.add(zoekButton); buttonPanel.add(wisButton); toernooiButton.addActionListener(this); exitButton.addActionListener(this); zoekButton.addActionListener(this); wisButton.addActionListener(this); JPanel mainPanel = new JPanel(); System.out.println("JPanel default layout manager = " + mainPanel.getLayout().getClass().getName()); mainPanel.add(title); mainPanel.add(tekstPanel); mainPanel.add(plekPanel); mainPanel.add(blankPanel); mainPanel.add(buttonPanel); return mainPanel; } public void actionPerformed(ActionEvent ae){ if(ae.getSource() == toernooiButton){ String plek = tekstvak1.getText(); String naam = tekstvak2.getText(); tekstvak1.setText(""); tekstvak2.setText(""); array1 = new ArrayList<String>(); array1.add(plek); array2 = new ArrayList<String>(); array2.add(naam); Collections.sort(array1); Collections.sort(array2); JOptionPane.showMessageDialog(null, "Toernooi Toegevoegd"); } if (ae.getSource()== wisButton){ tekstvak1.setText(""); tekstvak2.setText(""); } if (ae.getSource()== zoekButton){ System.out.println(array1); } if(ae.getSource()== exitButton){ System.exit(0); } } public static void main(String[] args) { Badminton app = new Badminton(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(app.getContent()); f.setSize(800,600); f.setLocation(200, 200); } }
Thanks in advance
Tom




Reply With Quote