Results 1 to 4 of 4
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] [newbie] AbstractButton.setText() not setting text within JButton

    I have this calculator program and it does not seem to clear the display text. So when I select the '=' the previous text does not clear.

    :confused:

    Java Code:
    CalculatorPanel.java
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class CalculatorPanel extends JPanel {
    	public CalculatorPanel()
    	{
    		setLayout(new BorderLayout());
    		
    		result =0;
    		lastCommand = "=";
    		start=true;
    		
    		//add the display
    		display = new JButton("0");
    		display.setEnabled(false);
    		add(display, BorderLayout.NORTH);
    		
    		ActionListener insert = new InsertAction();
    		@SuppressWarnings("unused")
    		ActionListener command = new CommandAction();
    		
    		//add the buttons in a 4 X 4 grid
    		panel = new JPanel();
    		panel.setLayout(new GridLayout(4,4));
    		
    		addButton("7", insert);
    		addButton("8", insert);
    		addButton("9", insert);
    		addButton("/", insert);
    		
    		addButton("4", insert);
    		addButton("5", insert);
    		addButton("6", insert);
    		addButton("*", insert);
    		
    		addButton("1", insert);
    		addButton("2", insert);
    		addButton("3", insert);
    		addButton("-", insert);
    		
    		addButton("0", insert);
    		addButton(".", insert);
    		addButton("=", insert);
    		addButton("+", insert);
    		
    		add(panel,BorderLayout.CENTER);
    	}
    	
    		/**
    		 * Adds a button to the center panel.
    		 * @param label: the button label
    		 * @param listener: the button listener
    		 */
    		private void addButton(String label, ActionListener listener)
    		{
    			JButton button = new JButton(label);
    			button.addActionListener(listener);
    			panel.add(button);
    		}
    		
    		/**
    		 * This action inserts the button action string to the end of the display text.
    		 */
    		private class InsertAction implements ActionListener
    		{
    			public void actionPerformed (ActionEvent event)
    			{
    				String input = event.getActionCommand();
    				if (start)
    				{
    					display.setText("");
    					start = false;
    				}
    				display.setText(display.getText() + input);
    			}
    		}
    		
    		/**
    		 * This action executes the command that the button action string denotes.
    		 */
    		private class CommandAction implements ActionListener
    		{
    			public void actionPerformed (ActionEvent event) {
    				String command = event.getActionCommand();
    				if (start)
    				{
    					if (command.equals("-"))
    					{
    						display.setText(command);
    						start=false;
    					}
    					else lastCommand = command;
    				}
    				else 
    				{
    					calculate(Double.parseDouble(display.getText()));
    					lastCommand = command;
    					start = true;
    				}
    			}
    		}
    		
    		/**
    		 * Carries out the pending calculation
    		 */
    		public void calculate(double x)
    		{
    			if (lastCommand.equals("+")) result += x;
    			else if (lastCommand.equals("-")) result -= x;
    			else if (lastCommand.equals("*")) result *= x;
    			else if (lastCommand.equals("/")) result /= x;
    			else if (lastCommand.equals("=")) result = x;
    			[B]display.setText("" + result);[/B]
    			
    		}
    		
    		[B]private JButton display;[/B]
    		private JPanel panel;
    		private double result;
    		private String lastCommand;
    		private boolean start;
    }
    
    CalculatorFrame.java
    package homenetwork.bkr.training;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class CalculatorFrame extends JFrame {
    	public CalculatorFrame()
    	{
    		setTitle("Calculator");
    		CalculatorPanel panel = new CalculatorPanel();
    		add(panel);
    		pack();
    	}
    }
    
    
    CalculatorTest.java
    package homenetwork.bkr.training;
    
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    
    public class CalculatorTest {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable()
    			{
    				public void run()
    				{
    					CalculatorFrame frame = new CalculatorFrame();
    					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    					frame.setVisible(true);
    				}
    			});
    	}
    
    }
    NOTE: It would be nice to have line numbers for code snippets.
    Last edited by jon80; 05-25-2009 at 12:42 AM. Reason: update

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Probably the same old story - you need to call repaint() after changing the GUI.

    Quote Originally Posted by jon80 View Post
    NOTE: It would be nice to have line numbers for code snippets.
    Then put line numbers in your code.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... line numbers would be nice in code snippets...

    @OrangeDog Probably wouldn't be very good for the code to have line numbers entered into it. I think he means line numbers added to it whenever you post.

    @OP Post a thread in the suggestions forum, see what happens
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    repaint() may not help here as you're calling setText on a JTextField, and Swing should update that automatically without a repaint. Rather, I wonder if the problem is that you're giving all of your buttons InsertAction ActionListeners including the non-numeric buttons, the ones that perhaps would work better if assigned CommandAction listeners?

Similar Threads

  1. setText() problem
    By jls7168 in forum New To Java
    Replies: 2
    Last Post: 02-20-2009, 10:34 PM
  2. Replies: 2
    Last Post: 01-23-2009, 04:09 PM
  3. [SOLVED] new2java- gettext settext
    By obdi in forum New To Java
    Replies: 4
    Last Post: 07-21-2008, 09:28 AM
  4. setText in event
    By sniezna.stopa in forum SWT / JFace
    Replies: 0
    Last Post: 06-20-2008, 02:56 PM
  5. Replies: 1
    Last Post: 08-01-2007, 06:10 AM

Posting Permissions

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