Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Clear button doesn't work

  1. #1
    BobbyH is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Clear button doesn't work

    In my program I have a couple jtextfields and jtextarea's. Now I want to clear them with one button. But when the button is clicked I get a error. Can someone say what I'm doing wrong. I use the setText("") but it wont work.

    Java Code:
    package test;
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class menuKaartBeheer
    {
    	JTextField tfdNaamGerecht;
    	JTextField tfdPrijs;
    	JTextArea taIngredienten;
    	JTextArea taRecept;
    	
    	JButton btnNieuw;
    	
    	public static void main (String[] args)
    	{
    		menuKaartBeheer gui = new menuKaartBeheer();
    		gui.go();
    	}
    	
    	public void go()
    	{
    		JFrame frame = new JFrame();
    		
    		JPanel panel = new JPanel();
    		JPanel panel1 = new JPanel();
    		
    		JButton btnNieuw = new JButton("Nieuw");
    		JButton btnVoegToe = new JButton("Voeg toe");
    		JButton btnBewerk = new JButton("Bewerk");
    		JButton btnWis = new JButton("Wis");
    		JButton btnStop = new JButton("Stop");
    		
    		btnNieuw.addActionListener(new nieuwListener());
    		btnVoegToe.addActionListener(new voegToeListener());
    		btnBewerk.addActionListener(new bewerkListener());
    		btnWis.addActionListener(new wisListener());
    		btnStop.addActionListener(new stopListener());
    		
    		JTextField tfdNaamGerecht = new JTextField(50);
    		JTextField tfdPrijs = new JTextField(6);
    		
    		taIngredienten = new JTextArea(10,20);
    		taIngredienten.setText("HOihoi");
    		taIngredienten.setLineWrap(true);
    		taRecept = new JTextArea(10,20);
    		taRecept.setLineWrap(true);
    		
    		JScrollPane scrIngredienten = new JScrollPane(taIngredienten);
    		scrIngredienten.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    		scrIngredienten.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    		
    		JScrollPane scrRecept = new JScrollPane(taRecept);
    		scrRecept.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    		scrRecept.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    		
    		panel.add(tfdNaamGerecht);
    		panel.add(tfdPrijs);
    		panel.add(scrIngredienten);
    		panel.add(scrRecept);
    		
    		panel1.add(btnNieuw);
    		panel1.add(btnVoegToe);
    		panel1.add(btnBewerk);
    		panel1.add(btnWis);
    		panel1.add(btnStop);
    		
    		frame.getContentPane().add(BorderLayout.CENTER, panel);
    		frame.getContentPane().add(BorderLayout.NORTH, panel1);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(600,600);
    		frame.setVisible(true);
    	}
    	
    	public class nieuwListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			tfdNaamGerecht.setText("");
    		}
    	}
    	
    	public class voegToeListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			//taRecept.append("Button Clicked \n");
    			//taIngredienten.setText("");
    			//tfdNaamGerecht.setText(null);
    		}
    	}
    	
    	public class bewerkListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			taRecept.append("Button Clicked \n");
    			taIngredienten.setText("");
    			tfdNaamGerecht.setText("hoi");
    		}
    	}
    	
    	public class wisListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			taRecept.append("Button Clicked \n");
    			taIngredienten.setText("");
    			tfdNaamGerecht.setText("hoi");
    		}
    	}
    	
    	public class stopListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    		}
    	}
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: Clear button doesn't work

    You declared all the JTextFields as fields, but ALSO declared them locally in the constructor. The fields will always be null because you assign the new JTextField to the local variables. Do NOT declare these variables again in the constructor, just assign them:
    Java Code:
    package test;
    import javax.swing.*;
     
    import java.awt.*;
    import java.awt.event.*;
     
    public class menuKaartBeheer
    {
        JTextField tfdNaamGerecht;
        JTextField tfdPrijs;
        JTextArea taIngredienten;
        JTextArea taRecept;
    .....
        public void go()
        {
    .....
              tfdNaamGerecht = new JTextField(50);
    .....
    BobbyH likes this.

Similar Threads

  1. Replies: 11
    Last Post: 03-14-2012, 07:30 PM
  2. Button size doesn't work.
    By rokit boy in forum AWT / Swing
    Replies: 6
    Last Post: 02-11-2012, 09:38 PM
  3. Solving Clear Button
    By Ryan10 in forum New To Java
    Replies: 73
    Last Post: 04-13-2011, 05:04 AM
  4. Clear Radio Button
    By Reborn in forum New To Java
    Replies: 6
    Last Post: 07-25-2010, 05:21 PM
  5. Replies: 6
    Last Post: 12-23-2009, 09:10 AM

Tags for this Thread

Posting Permissions

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