By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,611 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,611 IT Pros & Developers. It's quick & easy.

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

P: 23
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
Share this Question
Share on Google+
5 Replies


P: 23
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

P: 23
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

P: 23
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

10K+
P: 13,264
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

P: 4
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

Post your reply

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