Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners retaining textbox values
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


letumile
retaining textbox values
2 Answer(s)      2 years and a month ago
Posted in : Java Beginners


i have a calculator program, when i press a button the value displays but disappears when i press another button so how can i keep values to display when i press multiple buttons


Advertisement
View Answers

November 16, 2012 at 5:03 PM


Here is a code that creates calculator using swing components.

      import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

  class Calculator extends JFrame {
  private final Font BIGGER_FONT = new Font("monspaced",  Font.PLAIN, 20);
  private JTextField textfield; 
  private boolean number = true;  
  private String  equalOp  = "=";  
  private CalculatorOp op = new CalculatorOp(); 

  public Calculator() {
  textfield = new JTextField("0", 12);
  textfield.setHorizontalAlignment(JTextField.RIGHT);
  textfield.setFont(BIGGER_FONT);

  ActionListener numberListener = new NumberListener();
  String buttonOrder = "1234567890 ";
  JPanel buttonPanel = new JPanel();
  buttonPanel.setLayout(new GridLayout(4, 4, 4, 4));
  for (int i = 0; i < buttonOrder.length(); i++) {
  String key = buttonOrder.substring(i, i+1);
  if (key.equals(" ")) {
  buttonPanel.add(new JLabel(""));
  } else {
  JButton button = new JButton(key);
  button.addActionListener(numberListener);
  button.setFont(BIGGER_FONT);
  buttonPanel.add(button);
  }
  }
  ActionListener operatorListener = new OperatorListener();
  JPanel panel = new JPanel();
  panel.setLayout(new GridLayout(4, 4, 4, 4));
  String[] opOrder = {"+", "-", "*", "/","=","C"};
 for (int i = 0; i < opOrder.length; i++) {
  JButton button = new JButton(opOrder[i]);
  button.addActionListener(operatorListener);
  button.setFont(BIGGER_FONT);
  panel.add(button);
  }
 JPanel pan = new JPanel();
  pan.setLayout(new BorderLayout(4, 4));
  pan.add(textfield, BorderLayout.NORTH );
  pan.add(buttonPanel , BorderLayout.CENTER);
  pan.add(panel , BorderLayout.EAST  );
  this.setContentPane(pan);
  this.pack();
  this.setTitle("Calculator");
  this.setResizable(false);
  }
  private void action() {
  number = true; 
  textfield.setText("0");
  equalOp  = "=";
  op.setTotal("0");
  }


November 16, 2012 at 5:04 PM


continue....

class OperatorListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
  if (number) {
  action();
  textfield.setText("0");
  } else {
  number = true; 
  String displayText = textfield.getText();
  if (equalOp.equals("=")) {
  op.setTotal(displayText);
  } else if (equalOp.equals("+")) {
  op.add(displayText);
  } else if (equalOp.equals("-")) {
  op.subtract(displayText);
  } else if (equalOp.equals("*")) {
  op.multiply(displayText);
  } else if (equalOp.equals("/")) {
  op.divide(displayText);
  }
 textfield.setText("" + op.getTotalString());
 equalOp = e.getActionCommand();
  }
 }
 }
  class NumberListener implements ActionListener {
  public void actionPerformed(ActionEvent event) {
  String digit = event.getActionCommand(); 
  if (number) {
  textfield.setText(digit);
  number = false;
  } else {
 textfield.setText(textfield.getText() + digit);
  }
  }
  }
  public class CalculatorOp {

private int total; 
public CalculatorOp() {
  total = 0;
  }
 public String getTotalString() {
  return ""+total;
  }
 public void setTotal(String n) {
  total = convertToNumber(n);
  }
 public void add(String n) {
  total += convertToNumber(n);
 }
 public void subtract(String n) {
  total -= convertToNumber(n);
  }
 public void multiply(String n) {
  total *= convertToNumber(n);
  }
   public void divide(String n) {
  total /= convertToNumber(n);
  }
 private int convertToNumber(String n) {
  return Integer.parseInt(n);
  }
}
}
class SwingCalculator {
 public static void main(String[] args) {
  JFrame frame = new Calculator();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
  }
}


Related Tutorials/Questions & Answers:
retaining textbox values
retaining textbox values  i have a calculator program, when i press a button the value displays but disappears when i press another button so how can i keep values to display when i press multiple buttons
Retaining textBox values in java - Java Beginners
Retaining textBox values in java  Hi all, i have a jsp screen where i have two actions . I have a single textbox and two buttons. My textbox value is becoming null once i click on any one button. I want the textbox value
Advertisements
retaining the selected values in html:select multiple="true" in jsp + collection + struts
retaining the selected values in html:select multiple="true" in jsp + collection + struts  Hi, I have a multiple selection box in one of the jsp: <TR> <TD height=30 class="Formblue" valign
get date picker values to a textbox
get date picker values to a textbox  I am using DatePicker to select date in a form in asp.net using c#. I need the picked data to get in to a textbox. Please help me soon
JSP textbox autopopulation on basis of SQL table values
JSP textbox autopopulation on basis of SQL table values  Hi, I need to achieve the following on J2EE platform Could you please help? The following table is created in MySQL DB: Problem type Status Responsible LEGAL
How to insert dynamic textbox values into database using Java?
How to insert dynamic textbox values into database using Java?  Hi I am trying to insert dynamic textbox values to database, my jsp form have 2... these dynamic textbox values to database(Oracle 11g)...Please help me out. I have
How to insert data from a combobox and textbox values into DB using JSP?
How to insert data from a combobox and textbox values into DB using JSP?  hi, How to insert a comb-box and a text box values in to DB using JSP? @DB:student; @table:stu_info; Combobox values:(class1,class2,class3); textbox1
how to get the values from dynamically generated textbox in java?
how to get the values from dynamically generated textbox in java?  I... as per the retrieved data) and I want to store textbox values into the two table... to get and update this textbox values into both the tables(Xray,CTScan
Autopopulate values into textbox from database on pressing tab or on clicking
Autopopulate values into textbox from database on pressing tab or on clicking  Hi, In my project we have to enter a productid which is first need... on the producttype(which is first textbox related to product_id).This is need
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file  Hi am new to java. i need to create... a jsp file which contains a textbox, name issuedescription. when user types
How to show autocomplete textbox values on combo box option selection using database?
How to show autocomplete textbox values on combo box option selection using database?  When I select option(i.e First Year) then it will show list of student names in auto-complete text box
Display values in textbox on selecting checkbox
Display values in textbox on selecting checkbox Here we are going to perform action on selecting the checkbox and display the first five text box values... in first five textbox, the same values will get displayed in next 5 textboxes
Calendar window is not showing the textbox values for the Dynamic rows in html page - Java Interview Questions
Calendar window is not showing the textbox values for the Dynamic rows in html page  My code is here now.When i click on calendar ,value is not showing in textbox please help to me it is very urgent for me Add
Getting mysql table in textbox
Getting mysql table in textbox  how to get mysql table values into textbox in java using ajax and servlets
Sql Server 2008 with textbox
Sql Server 2008 with textbox  **Hi, I tried to insert into DB using textbox in a form using vb by visual studio 2010 but its always catch an error... = "INSERT INTO Table1 (A , B) VALUES ('" + Asd + "','" + Lays + "') " cmd = New
showing the information of database in textbox
showing the information of database in textbox  how to make a information of a database make appear to the user in the textbox
Dynamically adding textbox and labels
Dynamically adding textbox and labels  Sir, In my application I want to insert texbox and labels dynamically and want to insert database field value in that generated label. Plz help me, Thanks in advance
display data from database in textbox when id entered in textbox
display data from database in textbox when id entered in textbox  i wanted to enter data in textbox .depending on entered data in textbox data from database should be displayed dynamically in textbox
listbox and textbox validations
listbox and textbox validations  hi, any one please tell me how to set validations for listbox and textboxes using bean classes? thank you   Please visit the following link: http://www.roseindia.net/jsp/user
date in textbox on page load
date in textbox on page load   i want to insret the current date in textbox but this code dosen't work and the value of text box will be blank... into textbox on page load. <html> <script> function addDate(){ date = new
javascript for textbox - JSP-Servlet
javascript for textbox  Dear sir , How to write a javascript for a textbox ?.I am doing a delet operation.When i clik on a particular record that record should be deleted before deleting it an alert message is shown
Dynamically hide textbox
Dynamically hide textbox  I have a drop down(combo box) with 2 values (reference and file).When refrence is selected from the drop down the components of file(that is displayed when file is selected) should get hidden dynamically
How to create textbox on combo value selection using javacsript in jsp?
How to create textbox on combo value selection using javacsript in jsp?  dynamically create textbox on combo value selection. when select multiple values then create multiple textboxes
dropdown vlaue changes based on input given in textbox uisng AJAX
dropdown vlaue changes based on input given in textbox uisng AJAX  hi, In my app i need one textbox based on textbox data, values in dropdown list have to change and dropdown data retreive data from database thanks KK
Dynamically Update textbox from database
Dynamically Update textbox from database  I have a database as shown... with two text boxes. if I type something in one textbox, the corresponding value... in another textbox. How can I achieve
display he new customer no in textbox
display he new customer no in textbox   i want to add the new customer for that when i click on add butto the new customernumber should be display in textfield . plz ell me the suggestions & code
retaining save dialog box even after clicking save button - Java Beginners
retaining save dialog box even after clicking save button  I am building an apllication.In that i need to save a text typed in text area to a file.so when i open save dialog box and input a file name that is already present
reain values
if it already exits i m using reuest.sendredirect to same jsp but all textbox values are cleard, i want to retain all values in textboxs after request.sendredirect .Please
Javascript change textbox background
Javascript change textbox background In this section, you will learn how to change the background of textbox. Here we have created a textbox and allowed the user to enter the valid name. If the entered name is 'roseindia
binding data with textbox on dropdown click
binding data with textbox on dropdown click  Hello friends,Divyesh here. i have jsp page and i would like to bind data with text box on dropdown click. in dropdown employee id are load.and when we select perticular id than how
How to check text in textbox using JavaScript
How to check text in textbox using JavaScript  How to check text in textbox using JavaScript I have a form in HTML that contains text feilds... the HTML form to be accepted when if has values for all textfeild. Please guide
 

 

 

DMCA.com