473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Here's one that should be a piece of cake for you java masters :)

I have this program right now, but I need to modify it to output to the JTextPane.
What I mean is, when someone clicks on the 1 button, it needs to output to the TextArea a 1. None of the add, subtract, multiply, divide symbols need to....just the numbers and the decimal point. I have no idea how to do this. Can anyone help me? Here's the program.

/*
* Calculator.java
*
* Created on December 12, 2006, 10:00 PM
* Created by Ken Garlitz
*/

/**
*
* @author Erekose
*/
public class Calculator extends javax.swing.JFrame {

/** Creates new form Calculator */
public Calculator() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jScrollPane1.setViewportView(jTextPane1);

jButton1.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton1.setText("7");

jButton2.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton2.setText("8");

jButton3.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton3.setText("9");

jButton4.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton4.setText("/");

jButton5.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton5.setText("4");

jButton6.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton6.setText("5");

jButton7.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton7.setText("6");

jButton8.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton8.setText("*");

jButton9.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton9.setText("1");

jButton10.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton10.setText("2");

jButton11.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton11.setText("3");

jButton12.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton12.setText("-");

jButton13.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton13.setText("0");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
jButton13.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jButton13KeyPressed(evt);
}
});

jButton14.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton14.setText(".");

jButton15.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton15.setText("=");

jButton16.setFont(new java.awt.Font("Tahoma", 1, 12));
jButton16.setText("+");

org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING, false)
.add(jButton13, 0, 0, Short.MAX_VALUE)
.add(jButton5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButton9, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(jButton10, 0, 0, Short.MAX_VALUE)
.add(jButton14, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
.add(jButton6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
.add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(jButton7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 90, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton15, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
.add(jButton11, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 91, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.TRAILING)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(jButton16, 0, 0, Short.MAX_VALUE)
.add(jButton12, 0, 0, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.add(jButton8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED))
.add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 84, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.BASELINE)
.add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 65, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.BASELINE)
.add(jButton5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 65, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.add(layout.createSequentialGroup()
.add(6, 6, 6)
.add(jButton8,
Dec 14 '06 #1
5 2112
Here's the rest of the code:

org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 65, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(jButton11, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
.add(jButton12, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING, false)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jButton9, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, jButton10, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
.add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.BASELINE)
.add(jButton14, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(jButton16, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
.add(jButton13, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>

private void jButton13KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}

private void jButton13ActionPerformed(java.awt.event.ActionEven t evt) {


// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane jTextPane1;
// End of variables declaration

}
Dec 14 '06 #2
Isn't there some way to do like a .append to do this?
My big problem is like entering a number that is more than one digit
Say i want to type in 4562 and have it appear in the screen above
This program doesnt need to actually DO any calculations
Just show the numbers that are pressed on the keypad
Dec 14 '06 #3
OK let's make this easier for everyone :)
I know that doing an OnActionPerformed
and then doing jTextPane1.setText("1") will send a 1 to the TextPane

My question is...how do i send 4563? After pressing the 4 button, the TextPane shows a 4, but then when I press the 5 button the TextPane shows a 5. I want to be able to send the 4, then put the 5 after it then the 6 etc etc.


Any help?
Dec 15 '06 #4
r035198x
13,262 8TB
OK let's make this easier for everyone :)
I know that doing an OnActionPerformed
and then doing jTextPane1.setText("1") will send a 1 to the TextPane

My question is...how do i send 4563? After pressing the 4 button, the TextPane shows a 4, but then when I press the 5 button the TextPane shows a 5. I want to be able to send the 4, then put the 5 after it then the 6 etc etc.


Any help?
Each button should have an actionCommand set for it(Which is simply a String). eg button 1 would have "1" as its command etc.

In the actionPerformed method you then do

Expand|Select|Wrap|Line Numbers
  1.  String command = event.getActionCommand(); 
  2. if(command.equals("1")) {
  3.     String current = pane.getText();
  4.     pane.setText(current + "1");//do the append
  5. }
  6.  
Dec 15 '06 #5
public void actionPerformed(ActionEvent ae) {
//firstTime is boolean. It helps you know if the number is the first
//textPane is a JTextBox(or JTextArea anyhow)
if(firstTime) {
//just display the typed key
textPane.setText(ae.getActionCommand());
firstTime = false;
}
else
{
//get the content of the textPane
String ss = textPane.getText();
//concat it to the typed key
ss = ss + ae.getActionCommand();
//display the concated(if there's anything like that)
textPane.setText(ss);
}
}
please dont reply if this works just lookup to the sky and shout a very big thank you
Dec 21 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

45
by: Market Mutant | last post by:
I just wonder job selections, job openings and salary level of PHP programer or Perl programmer comparing to Java programmers. Is Java programmer's salary has a minimal of 60K in US? Are there...
0
by: Brian | last post by:
I've got one master machine containing several databases and two other machines which I want to be slaves. However, I want each slave to only replicate some of the databases from the master, not...
0
by: melledge | last post by:
The Reliable Source for Everything XML - XML 2005 Update XML 2005 - November 14-18 - Atlanta Hilton Hotel - Atlanta, GA www.xmlconference.org Register today and participate in IDEAlliance's...
3
by: fiNAL.Y | last post by:
Many C++ masters recommend us not to use Macros in C++ but to use inline functions and const . But I think that Macros can do something inline functions and const cannot do. It can "extend" the...
10
by: Ray | last post by:
I am reading Andrei Alexandrescu's book. The ideas presented there sound really good, but I wonder--is there really a lot of people using it? Or it's simply too esoteric for mortals? Cheers Ray
0
by: luffmusse | last post by:
We are two students from the Royal Institute of Technology in Stockholm, Sweden (http://www.kth.se/eng/). We are currently doing our masters thesis in Applied Information Technology where we...
2
by: Andy Dingley | last post by:
http://bingweb.binghamton.edu/~tony/cake.jpg From: http://community.livejournal.com/good_eats/507059.html
3
roula
by: roula | last post by:
Hi, I am using cake php for a website, and it was all working fine my local pc, but when I uploaded it on the host it gave me this error without showing anything: Flash('The Content deleted:...
28
by: RMZ | last post by:
This may come across as a trolling message, it's not ment to be. It's intended to share a recent experience I had with Mac OS in an entertaining way. I'm doing this because I love technology in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.