Results 1 to 5 of 5

Thread: How to display ArrayList on new line

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    13

    How to display ArrayList on new line

    Hi Guys,

    I have the following bit of code
    Code:
    package Final;
    
    
    import java.awt.event.*;
    import java.util.ArrayList;
    import javax.swing.*;
    
    	
    	// Hieronder staan de  verschillende tekstvakken, buttons en arraylists die ik gebruik.
    	
    class Paneel2 extends JPanel 
    {
    	 private JTextField tekstvak1;
    	 private JTextField tekstvak2;
         private JTextArea uitvoerA;
    	 private JTextArea uitvoerB;
    	 private JButton toernooiButton;
         private JButton weergaveButton;
         private Badminton[] ArrayShuttle;
         private int count = 0;
     	 
     	
     	 
         
         
        
     public  Paneel2 ()
     {
    		// Hieronder staan de textarea's, buttons en texfields gedefineerd
    		
    		JLabel toernooiplek = new JLabel("Toernooi plek:     ");
    		tekstvak1 = new JTextField(20);
    	    JPanel plekPanel = new JPanel();
            plekPanel.add(toernooiplek);
            plekPanel.add(tekstvak1);
            
            JLabel toernooinaam = new JLabel ("Toernooi naam:    ");
            tekstvak2 = new JTextField(20);
            JPanel tekstPanel = new JPanel();
            tekstPanel.add(toernooinaam);
            tekstPanel.add(tekstvak2);
            
            uitvoerA = new JTextArea(10,10);
            tekstPanel.add(uitvoerA);
            uitvoerA.setOpaque(false);
            uitvoerA.setEditable(false);
            
            toernooiButton = new JButton ("Voeg toernooi toe");
            toernooiButton.addActionListener( new KnopHandler() );
            
            weergaveButton = new JButton (" Geef Toernooien weer");
            weergaveButton.addActionListener(new KnopHandler2());
            
            
            uitvoerB = new JTextArea(10,10);
            plekPanel.add(uitvoerB);
            uitvoerB.setOpaque(false);
            uitvoerB.setEditable(false);
              
            
            
            ArrayShuttle = new Badminton[20];
            
            // manier van actionlisteners toevoegen zonder ze nader te definieren 
           
            
            add(tekstPanel);
            add(plekPanel);
            add(weergaveButton);
            add(toernooiButton);
        
           
            
            // voor alle knoppen heb ik if - else gebruikt.
            
    	}
    
    	class KnopHandler implements ActionListener
    	{
    	public void actionPerformed(ActionEvent a)
    	{
    		String setPlek = tekstvak1.getText();
    		String setToernooi = tekstvak2.getText();
    		ArrayShuttle[count] = new Badminton(setPlek, setToernooi);
    		count++;
    		tekstvak1.setText("");
    		tekstvak2.setText("");
    	
    		   
    	       JOptionPane.showMessageDialog(null, "Toernooi Toegevoegd");
    	}
    	
    	}
    	
    	class KnopHandler2 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent a)
    		{
    			for (int i = 0; i<ArrayShuttle.length;i++)
    			{
    				
    				uitvoerA.setText(ArrayShuttle[i].getPlek()+ "\n");
    				uitvoerB.setText(ArrayShuttle[i].getToernooi() + "\n");
    			}
    		}
    	}
    }
    This is the piece of code is important
    Code:
    class KnopHandler2 implements ActionListener
    	{
    		public void actionPerformed(ActionEvent a)
    		{
    			for (int i = 0; i<ArrayShuttle.length;i++)
    			{
    				
    				uitvoerA.setText(ArrayShuttle[i].getPlek()+ "\n");
    				uitvoerB.setText(ArrayShuttle[i].getToernooi() + "\n");
    			}
    		}
    	}
    If i run the program, it overwrites the same line everytime.
    How do i display the line below the other etc etc.

    Thanks guys,

    Tom

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: How to display ArrayList on new line

    You want to use append instead of setText on your JTextArea.

    http://java.sun.com/j2se/1.4.2/docs/...JTextArea.html

    setText will replace what is in there with the whatever you're setting it to.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    13

    Re: How to display ArrayList on new line

    thanks kfcSmitty,

    The only problem now is, if i show to array it will give me all the entries.
    But if i add another entry and press show again it will just put them below the previous ones. So you have duplicates.
    Are there any simple solutions for this issue ( i'm looking at it my self atm )

    Tom

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: How to display ArrayList on new line

    You can add a flag for when entries change then setText to ""
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    13

    Re: How to display ArrayList on new line

    i figured it out , and it worked.
    I indeed used the setText("")

    Thanks for the help again

    Tom

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width