How to clear screen in java if the program runs in a window

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aravind12345
    New Member
    • Jan 2014
    • 22

    How to clear screen in java if the program runs in a window

    I have made a Calculator and i need to add a Clear button but i don't know how to clear the screen in the program can someone tell me how i will be thank full. i have no errors or warnings.

    This is the code.

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    
    public class Calclator extends JFrame{
    	public JButton addbutton;
    	public JButton subtractbutton;
    	public JButton multiplicationbutton;
    	public JButton divisionbutton;
            I will insert button here
    	public JTextField firstNumber;
    	public JLabel firstNumberLabel;
    	public JTextField secondNumber;
    	public JLabel secondNumberLabel;
    	public JTextField answer;
    	public JLabel answerLabel;
    	/*
    
    
    */
    public Calclator(){
    	this.setTitle("Self-Calculator");
    	this.setSize(new Dimension(250,250));
    	this.setLocation(MouseInfo.getPointerInfo().getLocation());
    	this.setLayout(new FlowLayout());
    	this.setResizable(false);
    	this.addComponentListener(new ComponentListener(){
    
    		public void componentHidden(ComponentEvent arg0) {}
    		public void componentMoved(ComponentEvent arg0) {}
    		public void componentResized(ComponentEvent arg0) {}
    		public void componentShown(ComponentEvent arg0) {
    		Calclator_load();	
    		}
    		
    	});
    			
    	this.addbutton = new JButton();
    	this.addbutton.setText("+");
    	this.addbutton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			addbutton_ActionPerformed();
    		}
    		
    	});
    	
    	this.subtractbutton = new JButton();
    	this.subtractbutton.setText("-");
    	this.subtractbutton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			subtractbutton_ActionPerformed();
    		}
    		
    	});
    	
    	this.multiplicationbutton = new JButton();
    	this.multiplicationbutton.setText("*");
    	this.multiplicationbutton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			multiplicationbutton_ActionPerformed();
    		}
    		
    	});
    	
    	this.divisionbutton = new JButton();
    	this.divisionbutton.setText("/");
    	this.divisionbutton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			divisionbutton_ActionPerformed();
    		}
    		
    	});
    	
    	
    	
    	this.firstNumber = new JTextField();
    	this.firstNumber.setText("                                    ");
    	this.firstNumberLabel = new JLabel();
    	this.firstNumberLabel.setText("FirstNumber");
    	
    	this.secondNumber = new JTextField();
    	this.secondNumber.setText("                                   ");
    	this.secondNumberLabel = new JLabel();
    	this.secondNumberLabel.setText("SecondNumber");
    	
    	this.answer = new JTextField();
    	this.answer.setText("                                          ");
    	this.answer.setEditable(false);
    	this.answerLabel = new JLabel();
    	this.answerLabel.setText("Answer");
    	
    	
    	add(addbutton);
    	add(subtractbutton);
    	add(multiplicationbutton);
    	add(divisionbutton);
    	add(firstNumberLabel);
    	add(firstNumber);
    	add(secondNumberLabel);
    	add(secondNumber);
    	add(answerLabel);
    	add(answer);
    }
    private void Calclator_load(){
    	firstNumber.setText("");
    	secondNumber.setText("");
    	answer.setText("");
    	
    }
    private void addbutton_ActionPerformed(){
    	try{
    		int num1, num2;
    		num1 = Integer.parseInt(firstNumber.getText());
    		num2 = Integer.parseInt(secondNumber.getText());
    		int answer2 = num1+num2;
    		answer.setText(String.valueOf(answer2));
    		
    	}catch(Exception ex){
    	JOptionPane.showMessageDialog(null, "Invalid Numbers");
    		
    	}
    	
    }
    private void subtractbutton_ActionPerformed(){
    	try{
    		int num1, num2;
    		num1 = Integer.parseInt(firstNumber.getText());
    		num2 = Integer.parseInt(secondNumber.getText());
    		int answer2 = num1-num2;
    		answer.setText(String.valueOf(answer2));
    		
    	}catch(Exception ex){
    	JOptionPane.showMessageDialog(null, "Invalid Numbers");
    		
    	}
    	
    }
    private void multiplicationbutton_ActionPerformed(){
    	try{
    		int num1, num2;
    		num1 = Integer.parseInt(firstNumber.getText());
    		num2 = Integer.parseInt(secondNumber.getText());
    		int answer2 = num1*num2;
    		answer.setText(String.valueOf(answer2));
    		
    	}catch(Exception ex){
    	JOptionPane.showMessageDialog(null, "Invalid Numbers");
    		
    	}
    	
    }
    private void divisionbutton_ActionPerformed(){
    	try{
    		int num1, num2;
    		num1 = Integer.parseInt(firstNumber.getText());
    		num2 = Integer.parseInt(secondNumber.getText());
    		int answer2 = num1/num2;
    		answer.setText(String.valueOf(answer2));
    		
    	}catch(Exception ex){
    	JOptionPane.showMessageDialog(null, "Invalid Numbers");
    		
    	}
    	
    }
    Clear screen button function goes here
    }
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Just to make sure I understand you correctly: You want to clear some JTextFields, correct? Well, JTextField extends JTextComponent and that has a setText(String) function. So you could do something like this:
    Code:
    answer.setText("");
    As you're setting it to an empty string, it will be empty after that. You'll want to do this in the ActionListener of the Clear button of course.

    Comment

    • aravind12345
      New Member
      • Jan 2014
      • 22

      #3
      Thanks you solved my problem. I would like to make button for the numbers as well but i don't know how can you tell me how?

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Where exactly are you stuck with that?

        Comment

        Working...