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



Home Answers Viewqa Java-Beginners retaining textbox values

Have Programming Question? Ask it here!
 
 
 


letumile
retaining textbox values
2 Answer(s)      a year and 5 months 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
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
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
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
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
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
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
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
store dynamic generated textbox value into database
store dynamic generated textbox value into database  sir, how do i store dynamically generated textbox value into the database thanks in advance
Selecting value from autocomplete textbox using jquery in jsp
not select the suggested values in the textbox,means on clicking particular field...Selecting value from autocomplete textbox using jquery in jsp   hello Sir, I completed ur tutorial on autocompletion textbox from database using
Getting Textbox data from database
Getting Textbox data from database  When i m trying to get data in textbox as readonly from database i m getting following error.and my code is shown below. <% String empid=(String) session.getAttribute("empid
Javascript generate textbox
Javascript generate textbox In this tutorial, you will learn how to generate a textbox on button click using javascript. Here is a javascript example... of textboxes. On clicking the button with respect to each textbox, it will give
Getting Textbox data from database
Getting Textbox data from database  When i m trying to get data in textbox as readonly from database i m getting following error.and my code is shown below. type Exception report message description The server encountered
Textbox allows only numbers in java wicket
Textbox allows only numbers in java wicket  Please provide me wicket code for text box that allows only numbers to type. Thank you
How to show data from database in textbox in jsp
of autocompletebox in JSP. As the user tpe any letter in the textbox, corresponding values...How to show data from database in textbox in jsp   How to show data from database in textbox in jsp   Here is an example that retrieve
get value from multiple textbox in jsp
get value from multiple textbox in jsp  how to get multiple textbox value in another jsp? If i using the following code <%for(int i=0;i<3;i++) {%> <td>name<input type="text" class="name" id="name">
send multiple textbox vaues in to an jsp form to store them in a DB table
send multiple textbox vaues in to an jsp form to store them in a DB table  Hi sir... I am not getting how can i send the multiple input text box values in to an jsp file with additional values to store those values
HOW TO DISPLAY ID IN TEXTBOX BASED ON COMBOBOX SELECTION IN A SAME PAGE
HOW TO DISPLAY ID IN TEXTBOX BASED ON COMBOBOX SELECTION IN A SAME PAGE ... Roleid and Role_name. and values are Roleid Role_name 1... is how to get Roleid in textbox when i select Role_name from combobox in a same
having difficulties in dynamically adding textbox with datepicker
having difficulties in dynamically adding textbox with datepicker  hi... add new row of textbox. And also able to use datepicker for the textbox as well. However, I am able to add and display the textbox but not the datepicker. I
Drop down for search textbox like google search
Drop down for search textbox like google search  I want drop down like google search (ie, when we type one letter then the word start with that are displayed). when the drop down list appear, then we can select one of word as our
How to display current date in textbox - Date Calendar
;% } This is my problem: I want to display current date in textbox... show in textbox. Please help me to solve that problem, I will wait your answer
javascript for textbox or prompt using servlet - JSP-Servlet
javascript for textbox or prompt using servlet  Dear sir I am writing script in servlet,so prompt() is not working here so please help me... ,if they are confirmed to delete the i have to take a reason for deleting a textbox as to come
Autocomplete textbox with database in jsp and follow the MVC model
Autocomplete textbox with database in jsp and follow the MVC model  i have one problem in my project i try to generate the autocomplete textbox with database mysql follow MVC Model but it not perform. So please Help me sir
javascript for textbox or prompt using servlet - JSP-Servlet
javascript for textbox or prompt using servlet  Dear sir, I am using a prompt(),when a delete is confirmed,its working fine ,but when a prompt is closed or cancel ,i have to remain in the same page but here it is going
how to make enable/disable textbox in while(rs.next)
how to make enable/disable textbox in while(rs.next)  Hi, I'm trying to enable/disable the textbox in the while loop. It works but when i want to update my data, the data are not updated correctly. When i remove the enable
how to make enable/disable textbox in while(rs.next)
how to make enable/disable textbox in while(rs.next)  Hi, I'm trying to enable/disable the textbox in the while loop. It works but when i want to update my data, the data are not updated correctly. When i remove the enable
Advertisements
 

 

 

DMCA.com